Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-aarch32-jdk8u
Path: blob/jdk8u272-b10-aarch32-20201026/jdk/src/solaris/native/java/lang/java_props_macosx.c
48795 views
1
/*
2
* Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
#include <sys/socket.h>
27
#include <netinet/in.h>
28
#include <arpa/inet.h>
29
#include <objc/objc-runtime.h>
30
31
#include <Security/AuthSession.h>
32
#include <CoreFoundation/CoreFoundation.h>
33
#include <SystemConfiguration/SystemConfiguration.h>
34
#include <Foundation/Foundation.h>
35
36
#include "java_props_macosx.h"
37
38
char *getPosixLocale(int cat) {
39
char *lc = setlocale(cat, NULL);
40
if ((lc == NULL) || (strcmp(lc, "C") == 0)) {
41
lc = getenv("LANG");
42
}
43
if (lc == NULL) return NULL;
44
return strdup(lc);
45
}
46
47
#define LOCALEIDLENGTH 128
48
char *getMacOSXLocale(int cat) {
49
const char* retVal = NULL;
50
char languageString[LOCALEIDLENGTH];
51
char localeString[LOCALEIDLENGTH];
52
53
switch (cat) {
54
case LC_MESSAGES:
55
{
56
// get preferred language code
57
CFArrayRef languages = CFLocaleCopyPreferredLanguages();
58
if (languages == NULL) {
59
return NULL;
60
}
61
if (CFArrayGetCount(languages) <= 0) {
62
CFRelease(languages);
63
return NULL;
64
}
65
66
CFStringRef primaryLanguage = (CFStringRef)CFArrayGetValueAtIndex(languages, 0);
67
if (primaryLanguage == NULL) {
68
CFRelease(languages);
69
return NULL;
70
}
71
if (CFStringGetCString(primaryLanguage, languageString,
72
LOCALEIDLENGTH, CFStringGetSystemEncoding()) == false) {
73
CFRelease(languages);
74
return NULL;
75
}
76
CFRelease(languages);
77
78
retVal = languageString;
79
80
// Special case for Portuguese in Brazil:
81
// The language code needs the "_BR" region code (to distinguish it
82
// from Portuguese in Portugal), but this is missing when using the
83
// "Portuguese (Brazil)" language.
84
// If language is "pt" and the current locale is pt_BR, return pt_BR.
85
if (strcmp(retVal, "pt") == 0 &&
86
CFStringGetCString(CFLocaleGetIdentifier(CFLocaleCopyCurrent()),
87
localeString, LOCALEIDLENGTH, CFStringGetSystemEncoding()) &&
88
strcmp(localeString, "pt_BR") == 0) {
89
retVal = localeString;
90
}
91
}
92
break;
93
default:
94
{
95
if (!CFStringGetCString(CFLocaleGetIdentifier(CFLocaleCopyCurrent()),
96
localeString, LOCALEIDLENGTH, CFStringGetSystemEncoding())) {
97
return NULL;
98
}
99
retVal = localeString;
100
}
101
break;
102
}
103
104
if (retVal != NULL) {
105
// Language IDs use the language designators and (optional) region
106
// and script designators of BCP 47. So possible formats are:
107
//
108
// "en" (language designator only)
109
// "haw" (3-letter lanuage designator)
110
// "en-GB" (language with alpha-2 region designator)
111
// "es-419" (language with 3-digit UN M.49 area code)
112
// "zh-Hans" (language with ISO 15924 script designator)
113
// "zh-Hans-US" (language with ISO 15924 script designator and region)
114
// "zh-Hans-419" (language with ISO 15924 script designator and UN M.49)
115
//
116
// In the case of region designators (alpha-2 and/or UN M.49), we convert
117
// to our locale string format by changing '-' to '_'. That is, if
118
// the '-' is followed by fewer than 4 chars.
119
char* scriptOrRegion = strchr(retVal, '-');
120
if (scriptOrRegion != NULL) {
121
int length = strlen(scriptOrRegion);
122
if (length > 5) {
123
// Region and script both exist. Honor the script for now
124
scriptOrRegion[5] = '\0';
125
} else if (length < 5) {
126
*scriptOrRegion = '_';
127
128
assert((length == 3 &&
129
// '-' followed by a 2 character region designator
130
isalpha(scriptOrRegion[1]) &&
131
isalpha(scriptOrRegion[2])) ||
132
(length == 4 &&
133
// '-' followed by a 3-digit UN M.49 area code
134
isdigit(scriptOrRegion[1]) &&
135
isdigit(scriptOrRegion[2]) &&
136
isdigit(scriptOrRegion[3])));
137
}
138
}
139
140
return strdup(retVal);
141
}
142
return NULL;
143
}
144
145
char *setupMacOSXLocale(int cat) {
146
char * ret = getMacOSXLocale(cat);
147
148
if (ret == NULL) {
149
return getPosixLocale(cat);
150
} else {
151
return ret;
152
}
153
}
154
155
int isInAquaSession() {
156
// environment variable to bypass the aqua session check
157
char *ev = getenv("AWT_FORCE_HEADFUL");
158
if (ev && (strncasecmp(ev, "true", 4) == 0)) {
159
// if "true" then tell the caller we're in an Aqua session without actually checking
160
return 1;
161
}
162
// Is the WindowServer available?
163
SecuritySessionId session_id;
164
SessionAttributeBits session_info;
165
OSStatus status = SessionGetInfo(callerSecuritySession, &session_id, &session_info);
166
if (status == noErr) {
167
if (session_info & sessionHasGraphicAccess) {
168
return 1;
169
}
170
}
171
return 0;
172
}
173
174
// 10.9 SDK does not include the NSOperatingSystemVersion struct.
175
// For now, create our own
176
typedef struct {
177
NSInteger majorVersion;
178
NSInteger minorVersion;
179
NSInteger patchVersion;
180
} OSVerStruct;
181
182
void setOSNameAndVersion(java_props_t *sprops) {
183
// Hardcode os_name, and fill in os_version
184
sprops->os_name = strdup("Mac OS X");
185
186
char* osVersionCStr = NULL;
187
// Mac OS 10.9 includes the [NSProcessInfo operatingSystemVersion] function,
188
// but it's not in the 10.9 SDK. So, call it via objc_msgSend_stret.
189
if ([[NSProcessInfo processInfo] respondsToSelector:@selector(operatingSystemVersion)]) {
190
OSVerStruct (*procInfoFn)(id rec, SEL sel) = (OSVerStruct(*)(id, SEL))objc_msgSend_stret;
191
OSVerStruct osVer = procInfoFn([NSProcessInfo processInfo],
192
@selector(operatingSystemVersion));
193
NSString *nsVerStr;
194
if (osVer.patchVersion == 0) { // Omit trailing ".0"
195
nsVerStr = [NSString stringWithFormat:@"%ld.%ld",
196
(long)osVer.majorVersion, (long)osVer.minorVersion];
197
} else {
198
nsVerStr = [NSString stringWithFormat:@"%ld.%ld.%ld",
199
(long)osVer.majorVersion, (long)osVer.minorVersion, (long)osVer.patchVersion];
200
}
201
// Copy out the char*
202
osVersionCStr = strdup([nsVerStr UTF8String]);
203
}
204
// Fallback if running on pre-10.9 Mac OS
205
if (osVersionCStr == NULL) {
206
NSDictionary *version = [NSDictionary dictionaryWithContentsOfFile :
207
@"/System/Library/CoreServices/SystemVersion.plist"];
208
if (version != NULL) {
209
NSString *nsVerStr = [version objectForKey : @"ProductVersion"];
210
if (nsVerStr != NULL) {
211
osVersionCStr = strdup([nsVerStr UTF8String]);
212
}
213
}
214
}
215
if (osVersionCStr == NULL) {
216
osVersionCStr = strdup("Unknown");
217
}
218
sprops->os_version = osVersionCStr;
219
}
220
221
222
static Boolean getProxyInfoForProtocol(CFDictionaryRef inDict, CFStringRef inEnabledKey,
223
CFStringRef inHostKey, CFStringRef inPortKey,
224
CFStringRef *outProxyHost, int *ioProxyPort) {
225
/* See if the proxy is enabled. */
226
CFNumberRef cf_enabled = CFDictionaryGetValue(inDict, inEnabledKey);
227
if (cf_enabled == NULL) {
228
return false;
229
}
230
231
int isEnabled = false;
232
if (!CFNumberGetValue(cf_enabled, kCFNumberIntType, &isEnabled)) {
233
return isEnabled;
234
}
235
236
if (!isEnabled) return false;
237
*outProxyHost = CFDictionaryGetValue(inDict, inHostKey);
238
239
// If cf_host is null, that means the checkbox is set,
240
// but no host was entered. We'll treat that as NOT ENABLED.
241
// If cf_port is null or cf_port isn't a number, that means
242
// no port number was entered. Treat this as ENABLED with the
243
// protocol's default port.
244
if (*outProxyHost == NULL) {
245
return false;
246
}
247
248
if (CFStringGetLength(*outProxyHost) == 0) {
249
return false;
250
}
251
252
int newPort = 0;
253
CFNumberRef cf_port = NULL;
254
if ((cf_port = CFDictionaryGetValue(inDict, inPortKey)) != NULL &&
255
CFNumberGetValue(cf_port, kCFNumberIntType, &newPort) &&
256
newPort > 0) {
257
*ioProxyPort = newPort;
258
} else {
259
// bad port or no port - leave *ioProxyPort unchanged
260
}
261
262
return true;
263
}
264
265
static char *createUTF8CString(const CFStringRef theString) {
266
if (theString == NULL) return NULL;
267
268
const CFIndex stringLength = CFStringGetLength(theString);
269
const CFIndex bufSize = CFStringGetMaximumSizeForEncoding(stringLength, kCFStringEncodingUTF8) + 1;
270
char *returnVal = (char *)malloc(bufSize);
271
272
if (CFStringGetCString(theString, returnVal, bufSize, kCFStringEncodingUTF8)) {
273
return returnVal;
274
}
275
276
free(returnVal);
277
return NULL;
278
}
279
280
// Return TRUE if str is a syntactically valid IP address.
281
// Using inet_pton() instead of inet_aton() for IPv6 support.
282
// len is only a hint; cstr must still be nul-terminated
283
static int looksLikeIPAddress(char *cstr, size_t len) {
284
if (len == 0 || (len == 1 && cstr[0] == '.')) return FALSE;
285
286
char dst[16]; // big enough for INET6
287
return (1 == inet_pton(AF_INET, cstr, dst) ||
288
1 == inet_pton(AF_INET6, cstr, dst));
289
}
290
291
292
293
// Convert Mac OS X proxy exception entry to Java syntax.
294
// See Radar #3441134 for details.
295
// Returns NULL if this exception should be ignored by Java.
296
// May generate a string with multiple exceptions separated by '|'.
297
static char * createConvertedException(CFStringRef cf_original) {
298
// This is done with char* instead of CFString because inet_pton()
299
// needs a C string.
300
char *c_exception = createUTF8CString(cf_original);
301
if (!c_exception) return NULL;
302
303
int c_len = strlen(c_exception);
304
305
// 1. sanitize exception prefix
306
if (c_len >= 1 && 0 == strncmp(c_exception, ".", 1)) {
307
memmove(c_exception, c_exception+1, c_len);
308
c_len -= 1;
309
} else if (c_len >= 2 && 0 == strncmp(c_exception, "*.", 2)) {
310
memmove(c_exception, c_exception+2, c_len-1);
311
c_len -= 2;
312
}
313
314
// 2. pre-reject other exception wildcards
315
if (strchr(c_exception, '*')) {
316
free(c_exception);
317
return NULL;
318
}
319
320
// 3. no IP wildcarding
321
if (looksLikeIPAddress(c_exception, c_len)) {
322
return c_exception;
323
}
324
325
// 4. allow domain suffixes
326
// c_exception is now "str\0" - change to "str|*.str\0"
327
c_exception = reallocf(c_exception, c_len+3+c_len+1);
328
if (!c_exception) return NULL;
329
330
strncpy(c_exception+c_len, "|*.", 3);
331
strncpy(c_exception+c_len+3, c_exception, c_len);
332
c_exception[c_len+3+c_len] = '\0';
333
return c_exception;
334
}
335
336
/*
337
* Method for fetching the user.home path and storing it in the property list.
338
* For signed .apps running in the Mac App Sandbox, user.home is set to the
339
* app's sandbox container.
340
*/
341
void setUserHome(java_props_t *sprops) {
342
if (sprops == NULL) { return; }
343
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
344
sprops->user_home = createUTF8CString((CFStringRef)NSHomeDirectory());
345
[pool drain];
346
}
347
348
/*
349
* Method for fetching proxy info and storing it in the property list.
350
*/
351
void setProxyProperties(java_props_t *sProps) {
352
if (sProps == NULL) return;
353
354
char buf[16]; /* Used for %d of an int - 16 is plenty */
355
CFStringRef
356
cf_httpHost = NULL,
357
cf_httpsHost = NULL,
358
cf_ftpHost = NULL,
359
cf_socksHost = NULL,
360
cf_gopherHost = NULL;
361
int
362
httpPort = 80, // Default proxy port values
363
httpsPort = 443,
364
ftpPort = 21,
365
socksPort = 1080,
366
gopherPort = 70;
367
368
CFDictionaryRef dict = SCDynamicStoreCopyProxies(NULL);
369
if (dict == NULL) return;
370
371
/* Read the proxy exceptions list */
372
CFArrayRef cf_list = CFDictionaryGetValue(dict, kSCPropNetProxiesExceptionsList);
373
374
CFMutableStringRef cf_exceptionList = NULL;
375
if (cf_list != NULL) {
376
CFIndex len = CFArrayGetCount(cf_list), idx;
377
378
cf_exceptionList = CFStringCreateMutable(NULL, 0);
379
for (idx = (CFIndex)0; idx < len; idx++) {
380
CFStringRef cf_ehost;
381
if ((cf_ehost = CFArrayGetValueAtIndex(cf_list, idx))) {
382
/* Convert this exception from Mac OS X syntax to Java syntax.
383
See Radar #3441134 for details. This may generate a string
384
with multiple Java exceptions separated by '|'. */
385
char *c_exception = createConvertedException(cf_ehost);
386
if (c_exception) {
387
/* Append the host to the list of exclusions. */
388
if (CFStringGetLength(cf_exceptionList) > 0) {
389
CFStringAppendCString(cf_exceptionList, "|", kCFStringEncodingMacRoman);
390
}
391
CFStringAppendCString(cf_exceptionList, c_exception, kCFStringEncodingMacRoman);
392
free(c_exception);
393
}
394
}
395
}
396
}
397
398
if (cf_exceptionList != NULL) {
399
if (CFStringGetLength(cf_exceptionList) > 0) {
400
sProps->exceptionList = createUTF8CString(cf_exceptionList);
401
}
402
CFRelease(cf_exceptionList);
403
}
404
405
#define CHECK_PROXY(protocol, PROTOCOL) \
406
sProps->protocol##ProxyEnabled = \
407
getProxyInfoForProtocol(dict, kSCPropNetProxies##PROTOCOL##Enable, \
408
kSCPropNetProxies##PROTOCOL##Proxy, \
409
kSCPropNetProxies##PROTOCOL##Port, \
410
&cf_##protocol##Host, &protocol##Port); \
411
if (sProps->protocol##ProxyEnabled) { \
412
sProps->protocol##Host = createUTF8CString(cf_##protocol##Host); \
413
snprintf(buf, sizeof(buf), "%d", protocol##Port); \
414
sProps->protocol##Port = malloc(strlen(buf) + 1); \
415
strcpy(sProps->protocol##Port, buf); \
416
}
417
418
CHECK_PROXY(http, HTTP);
419
CHECK_PROXY(https, HTTPS);
420
CHECK_PROXY(ftp, FTP);
421
CHECK_PROXY(socks, SOCKS);
422
CHECK_PROXY(gopher, Gopher);
423
424
#undef CHECK_PROXY
425
426
CFRelease(dict);
427
}
428
429