Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/java.base/macosx/native/libjava/java_props_macosx.c
67769 views
1
/*
2
* Copyright (c) 1998, 2021, 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 <CoreFoundation/CoreFoundation.h>
32
#ifndef TARGET_OS_IOS
33
#include <SystemConfiguration/SystemConfiguration.h>
34
#else
35
#include "OSXSCSchemaDefinitions.h"
36
#include <SystemConfiguration/SCDynamicStore.h>
37
CFDictionaryRef SCDynamicStoreCopyProxies(SCDynamicStoreRef store);
38
#endif
39
#include <Foundation/Foundation.h>
40
41
#include "java_props_macosx.h"
42
43
char *getPosixLocale(int cat) {
44
char *lc = setlocale(cat, NULL);
45
if ((lc == NULL) || (strcmp(lc, "C") == 0)) {
46
lc = getenv("LANG");
47
}
48
if (lc == NULL) return NULL;
49
return strdup(lc);
50
}
51
52
#define LOCALEIDLENGTH 128
53
#ifndef kCFCoreFoundationVersionNumber10_11_Max
54
#define kCFCoreFoundationVersionNumber10_11_Max 1299
55
#endif
56
char *getMacOSXLocale(int cat) {
57
const char* retVal = NULL;
58
char languageString[LOCALEIDLENGTH];
59
char localeString[LOCALEIDLENGTH];
60
61
// Since macOS 10.12, there is no separate language selection for
62
// "format" locale, e.g., date format. Use the preferred language
63
// for all LC_* categories.
64
if (kCFCoreFoundationVersionNumber >
65
kCFCoreFoundationVersionNumber10_11_Max) {
66
cat = LC_MESSAGES;
67
}
68
69
switch (cat) {
70
case LC_MESSAGES:
71
{
72
// get preferred language code
73
CFArrayRef languages = CFLocaleCopyPreferredLanguages();
74
if (languages == NULL) {
75
return NULL;
76
}
77
if (CFArrayGetCount(languages) <= 0) {
78
CFRelease(languages);
79
return NULL;
80
}
81
82
CFStringRef primaryLanguage = (CFStringRef)CFArrayGetValueAtIndex(languages, 0);
83
if (primaryLanguage == NULL) {
84
CFRelease(languages);
85
return NULL;
86
}
87
if (CFStringGetCString(primaryLanguage, languageString,
88
LOCALEIDLENGTH, CFStringGetSystemEncoding()) == false) {
89
CFRelease(languages);
90
return NULL;
91
}
92
CFRelease(languages);
93
94
// Explicitly supply region, if there is none
95
char *hyphenPos = strchr(languageString, '-');
96
int langStrLen = strlen(languageString);
97
98
if (hyphenPos == NULL || // languageString contains ISO639 only, e.g., "en"
99
languageString + langStrLen - hyphenPos == 5) { // ISO639-ScriptCode, e.g., "en-Latn"
100
CFLocaleRef cflocale = CFLocaleCopyCurrent();
101
if (cflocale != NULL) {
102
CFStringGetCString(CFLocaleGetIdentifier(cflocale),
103
localeString, LOCALEIDLENGTH, CFStringGetSystemEncoding());
104
char *underscorePos = strrchr(localeString, '_');
105
char *region = NULL;
106
107
if (underscorePos != NULL) {
108
region = underscorePos + 1;
109
}
110
111
if (region != NULL) {
112
strcat(languageString, "-");
113
strcat(languageString, region);
114
}
115
CFRelease(cflocale);
116
}
117
}
118
119
retVal = languageString;
120
}
121
break;
122
123
default:
124
{
125
CFLocaleRef cflocale = CFLocaleCopyCurrent();
126
if (cflocale != NULL) {
127
if (!CFStringGetCString(CFLocaleGetIdentifier(cflocale),
128
localeString, LOCALEIDLENGTH, CFStringGetSystemEncoding())) {
129
CFRelease(cflocale);
130
return NULL;
131
}
132
133
retVal = localeString;
134
CFRelease(cflocale);
135
} else {
136
return NULL;
137
}
138
}
139
break;
140
}
141
142
if (retVal != NULL) {
143
// convertToPOSIXLocale() does not expect any variant codes, so ignore
144
// '@' and anything following, if present.
145
char* rmAt = strchr(retVal, '@');
146
if (rmAt != NULL) {
147
*rmAt = '\0';
148
}
149
return strdup(convertToPOSIXLocale(retVal));
150
}
151
152
return NULL;
153
}
154
155
/* Language IDs use the language designators and (optional) region
156
* and script designators of BCP 47. So possible formats are:
157
*
158
* "en" (language designator only)
159
* "haw" (3-letter lanuage designator)
160
* "en-GB" (language with alpha-2 region designator)
161
* "es-419" (language with 3-digit UN M.49 area code)
162
* "zh-Hans" (language with ISO 15924 script designator)
163
* "zh-Hans-US" (language with ISO 15924 script designator and region)
164
* "zh-Hans-419" (language with ISO 15924 script designator and UN M.49)
165
*
166
* convert these tags into POSIX conforming locale string, i.e.,
167
* lang{_region}{@script}. e.g., for "zh-Hans-US" into "zh_US@Hans"
168
*/
169
const char * convertToPOSIXLocale(const char* src) {
170
char* scriptRegion = strchr(src, '-');
171
if (scriptRegion != NULL) {
172
int length = strlen(scriptRegion);
173
char* region = strchr(scriptRegion + 1, '-');
174
char* atMark = NULL;
175
176
if (region == NULL) {
177
// CFLocaleGetIdentifier() returns '_' before region
178
region = strchr(scriptRegion + 1, '_');
179
}
180
181
*scriptRegion = '_';
182
if (length > 5) {
183
// Region and script both exist.
184
char tmpScript[4];
185
int regionLength = length - 6;
186
atMark = scriptRegion + 1 + regionLength;
187
memcpy(tmpScript, scriptRegion + 1, 4);
188
memmove(scriptRegion + 1, region + 1, regionLength);
189
memcpy(atMark + 1, tmpScript, 4);
190
} else if (length == 5) {
191
// script only
192
atMark = scriptRegion;
193
}
194
195
if (atMark != NULL) {
196
*atMark = '@';
197
198
// assert script code
199
assert(isalpha(atMark[1]) &&
200
isalpha(atMark[2]) &&
201
isalpha(atMark[3]) &&
202
isalpha(atMark[4]));
203
}
204
205
assert(((length == 3 || length == 8) &&
206
// '_' followed by a 2 character region designator
207
isalpha(scriptRegion[1]) &&
208
isalpha(scriptRegion[2])) ||
209
((length == 4 || length == 9) &&
210
// '_' followed by a 3-digit UN M.49 area code
211
isdigit(scriptRegion[1]) &&
212
isdigit(scriptRegion[2]) &&
213
isdigit(scriptRegion[3])) ||
214
// '@' followed by a 4 character script code (already validated above)
215
(length == 5));
216
}
217
218
return src;
219
}
220
221
char *setupMacOSXLocale(int cat) {
222
char * ret = getMacOSXLocale(cat);
223
224
if (ret == NULL) {
225
return getPosixLocale(cat);
226
} else {
227
return ret;
228
}
229
}
230
231
// 10.9 SDK does not include the NSOperatingSystemVersion struct.
232
// For now, create our own
233
typedef struct {
234
NSInteger majorVersion;
235
NSInteger minorVersion;
236
NSInteger patchVersion;
237
} OSVerStruct;
238
239
void setOSNameAndVersion(java_props_t *sprops) {
240
// Hardcode os_name, and fill in os_version
241
sprops->os_name = strdup("Mac OS X");
242
243
NSString *nsVerStr = NULL;
244
char* osVersionCStr = NULL;
245
// Mac OS 10.9 includes the [NSProcessInfo operatingSystemVersion] function,
246
// but it's not in the 10.9 SDK. So, call it via NSInvocation.
247
if ([[NSProcessInfo processInfo] respondsToSelector:@selector(operatingSystemVersion)]) {
248
OSVerStruct osVer;
249
NSMethodSignature *sig = [[NSProcessInfo processInfo] methodSignatureForSelector:
250
@selector(operatingSystemVersion)];
251
NSInvocation *invoke = [NSInvocation invocationWithMethodSignature:sig];
252
invoke.selector = @selector(operatingSystemVersion);
253
[invoke invokeWithTarget:[NSProcessInfo processInfo]];
254
[invoke getReturnValue:&osVer];
255
256
// Copy out the char* if running on version other than 10.16 Mac OS (10.16 == 11.x)
257
// or explicitly requesting version compatibility
258
if (!((long)osVer.majorVersion == 10 && (long)osVer.minorVersion >= 16) ||
259
(getenv("SYSTEM_VERSION_COMPAT") != NULL)) {
260
if (osVer.patchVersion == 0) { // Omit trailing ".0"
261
nsVerStr = [NSString stringWithFormat:@"%ld.%ld",
262
(long)osVer.majorVersion, (long)osVer.minorVersion];
263
} else {
264
nsVerStr = [NSString stringWithFormat:@"%ld.%ld.%ld",
265
(long)osVer.majorVersion, (long)osVer.minorVersion, (long)osVer.patchVersion];
266
}
267
} else {
268
// Version 10.16, without explicit env setting of SYSTEM_VERSION_COMPAT
269
// AKA 11+ Read the *real* ProductVersion from the hidden link to avoid SYSTEM_VERSION_COMPAT
270
// If not found, fallback below to the SystemVersion.plist
271
NSDictionary *version = [NSDictionary dictionaryWithContentsOfFile :
272
@"/System/Library/CoreServices/.SystemVersionPlatform.plist"];
273
if (version != NULL) {
274
nsVerStr = [version objectForKey : @"ProductVersion"];
275
}
276
}
277
}
278
// Fallback if running on pre-10.9 Mac OS
279
if (nsVerStr == NULL) {
280
NSDictionary *version = [NSDictionary dictionaryWithContentsOfFile :
281
@"/System/Library/CoreServices/SystemVersion.plist"];
282
if (version != NULL) {
283
nsVerStr = [version objectForKey : @"ProductVersion"];
284
}
285
}
286
287
if (nsVerStr != NULL) {
288
// Copy out the char*
289
osVersionCStr = strdup([nsVerStr UTF8String]);
290
}
291
if (osVersionCStr == NULL) {
292
osVersionCStr = strdup("Unknown");
293
}
294
sprops->os_version = osVersionCStr;
295
}
296
297
298
static Boolean getProxyInfoForProtocol(CFDictionaryRef inDict, CFStringRef inEnabledKey,
299
CFStringRef inHostKey, CFStringRef inPortKey,
300
CFStringRef *outProxyHost, int *ioProxyPort) {
301
/* See if the proxy is enabled. */
302
CFNumberRef cf_enabled = CFDictionaryGetValue(inDict, inEnabledKey);
303
if (cf_enabled == NULL) {
304
return false;
305
}
306
307
int isEnabled = false;
308
if (!CFNumberGetValue(cf_enabled, kCFNumberIntType, &isEnabled)) {
309
return isEnabled;
310
}
311
312
if (!isEnabled) return false;
313
*outProxyHost = CFDictionaryGetValue(inDict, inHostKey);
314
315
// If cf_host is null, that means the checkbox is set,
316
// but no host was entered. We'll treat that as NOT ENABLED.
317
// If cf_port is null or cf_port isn't a number, that means
318
// no port number was entered. Treat this as ENABLED with the
319
// protocol's default port.
320
if (*outProxyHost == NULL) {
321
return false;
322
}
323
324
if (CFStringGetLength(*outProxyHost) == 0) {
325
return false;
326
}
327
328
int newPort = 0;
329
CFNumberRef cf_port = NULL;
330
if ((cf_port = CFDictionaryGetValue(inDict, inPortKey)) != NULL &&
331
CFNumberGetValue(cf_port, kCFNumberIntType, &newPort) &&
332
newPort > 0) {
333
*ioProxyPort = newPort;
334
} else {
335
// bad port or no port - leave *ioProxyPort unchanged
336
}
337
338
return true;
339
}
340
341
static char *createUTF8CString(const CFStringRef theString) {
342
if (theString == NULL) return NULL;
343
344
const CFIndex stringLength = CFStringGetLength(theString);
345
const CFIndex bufSize = CFStringGetMaximumSizeForEncoding(stringLength, kCFStringEncodingUTF8) + 1;
346
char *returnVal = (char *)malloc(bufSize);
347
348
if (CFStringGetCString(theString, returnVal, bufSize, kCFStringEncodingUTF8)) {
349
return returnVal;
350
}
351
352
free(returnVal);
353
return NULL;
354
}
355
356
// Return TRUE if str is a syntactically valid IP address.
357
// Using inet_pton() instead of inet_aton() for IPv6 support.
358
// len is only a hint; cstr must still be nul-terminated
359
static int looksLikeIPAddress(char *cstr, size_t len) {
360
if (len == 0 || (len == 1 && cstr[0] == '.')) return FALSE;
361
362
char dst[16]; // big enough for INET6
363
return (1 == inet_pton(AF_INET, cstr, dst) ||
364
1 == inet_pton(AF_INET6, cstr, dst));
365
}
366
367
368
369
// Convert Mac OS X proxy exception entry to Java syntax.
370
// See Radar #3441134 for details.
371
// Returns NULL if this exception should be ignored by Java.
372
// May generate a string with multiple exceptions separated by '|'.
373
static char * createConvertedException(CFStringRef cf_original) {
374
// This is done with char* instead of CFString because inet_pton()
375
// needs a C string.
376
char *c_exception = createUTF8CString(cf_original);
377
if (!c_exception) return NULL;
378
379
int c_len = strlen(c_exception);
380
381
// 1. sanitize exception prefix
382
if (c_len >= 1 && 0 == strncmp(c_exception, ".", 1)) {
383
memmove(c_exception, c_exception+1, c_len);
384
c_len -= 1;
385
} else if (c_len >= 2 && 0 == strncmp(c_exception, "*.", 2)) {
386
memmove(c_exception, c_exception+2, c_len-1);
387
c_len -= 2;
388
}
389
390
// 2. pre-reject other exception wildcards
391
if (strchr(c_exception, '*')) {
392
free(c_exception);
393
return NULL;
394
}
395
396
// 3. no IP wildcarding
397
if (looksLikeIPAddress(c_exception, c_len)) {
398
return c_exception;
399
}
400
401
// 4. allow domain suffixes
402
// c_exception is now "str\0" - change to "str|*.str\0"
403
c_exception = reallocf(c_exception, c_len+3+c_len+1);
404
if (!c_exception) return NULL;
405
406
strncpy(c_exception+c_len, "|*.", 3);
407
strncpy(c_exception+c_len+3, c_exception, c_len);
408
c_exception[c_len+3+c_len] = '\0';
409
return c_exception;
410
}
411
412
/*
413
* Method for fetching the user.home path and storing it in the property list.
414
* For signed .apps running in the Mac App Sandbox, user.home is set to the
415
* app's sandbox container.
416
*/
417
void setUserHome(java_props_t *sprops) {
418
if (sprops == NULL) { return; }
419
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
420
sprops->user_home = createUTF8CString((CFStringRef)NSHomeDirectory());
421
[pool drain];
422
}
423
424
/*
425
* Method for fetching proxy info and storing it in the property list.
426
*/
427
void setProxyProperties(java_props_t *sProps) {
428
if (sProps == NULL) return;
429
430
char buf[16]; /* Used for %d of an int - 16 is plenty */
431
CFStringRef
432
cf_httpHost = NULL,
433
cf_httpsHost = NULL,
434
cf_ftpHost = NULL,
435
cf_socksHost = NULL;
436
int
437
httpPort = 80, // Default proxy port values
438
httpsPort = 443,
439
ftpPort = 21,
440
socksPort = 1080;
441
442
CFDictionaryRef dict = SCDynamicStoreCopyProxies(NULL);
443
if (dict == NULL) return;
444
445
/* Read the proxy exceptions list */
446
CFArrayRef cf_list = CFDictionaryGetValue(dict, kSCPropNetProxiesExceptionsList);
447
448
CFMutableStringRef cf_exceptionList = NULL;
449
if (cf_list != NULL) {
450
CFIndex len = CFArrayGetCount(cf_list), idx;
451
452
cf_exceptionList = CFStringCreateMutable(NULL, 0);
453
for (idx = (CFIndex)0; idx < len; idx++) {
454
CFStringRef cf_ehost;
455
if ((cf_ehost = CFArrayGetValueAtIndex(cf_list, idx))) {
456
/* Convert this exception from Mac OS X syntax to Java syntax.
457
See Radar #3441134 for details. This may generate a string
458
with multiple Java exceptions separated by '|'. */
459
char *c_exception = createConvertedException(cf_ehost);
460
if (c_exception) {
461
/* Append the host to the list of exclusions. */
462
if (CFStringGetLength(cf_exceptionList) > 0) {
463
CFStringAppendCString(cf_exceptionList, "|", kCFStringEncodingMacRoman);
464
}
465
CFStringAppendCString(cf_exceptionList, c_exception, kCFStringEncodingMacRoman);
466
free(c_exception);
467
}
468
}
469
}
470
}
471
472
if (cf_exceptionList != NULL) {
473
if (CFStringGetLength(cf_exceptionList) > 0) {
474
sProps->exceptionList = createUTF8CString(cf_exceptionList);
475
}
476
CFRelease(cf_exceptionList);
477
}
478
479
#define CHECK_PROXY(protocol, PROTOCOL) \
480
sProps->protocol##ProxyEnabled = \
481
getProxyInfoForProtocol(dict, kSCPropNetProxies##PROTOCOL##Enable, \
482
kSCPropNetProxies##PROTOCOL##Proxy, \
483
kSCPropNetProxies##PROTOCOL##Port, \
484
&cf_##protocol##Host, &protocol##Port); \
485
if (sProps->protocol##ProxyEnabled) { \
486
sProps->protocol##Host = createUTF8CString(cf_##protocol##Host); \
487
snprintf(buf, sizeof(buf), "%d", protocol##Port); \
488
sProps->protocol##Port = malloc(strlen(buf) + 1); \
489
strcpy(sProps->protocol##Port, buf); \
490
}
491
492
CHECK_PROXY(http, HTTP);
493
CHECK_PROXY(https, HTTPS);
494
CHECK_PROXY(ftp, FTP);
495
CHECK_PROXY(socks, SOCKS);
496
497
#undef CHECK_PROXY
498
499
CFRelease(dict);
500
}
501
502