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