1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/installer/gcapi_mac/gcapi.h"
#import <Cocoa/Cocoa.h>
#include <grp.h>
#include <pwd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/utsname.h>
namespace {
// The "~~" prefixes are replaced with the home directory of the
// console owner (i.e. not the home directory of the euid).
NSString* const kChromeInstallPath = @"/Applications/Google Chrome.app";
NSString* const kBrandKey = @"KSBrandID";
NSString* const kUserBrandPath = @"~~/Library/Google/Google Chrome Brand.plist";
NSString* const kSystemKsadminPath =
@"/Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/"
"Contents/MacOS/ksadmin";
NSString* const kUserKsadminPath =
@"~~/Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/"
"Contents/MacOS/ksadmin";
NSString* const kSystemMasterPrefsPath =
@"/Library/Google/Google Chrome Master Preferences";
NSString* const kUserMasterPrefsPath =
@"~~/Library/Application Support/Google/Chrome/"
"Google Chrome Master Preferences";
NSString* const kChannelKey = @"KSChannelID";
NSString* const kVersionKey = @"KSVersion";
// Condensed from chromium's base/mac/mac_util.mm.
bool IsOSXVersionSupported() {
// On 10.6, Gestalt() was observed to be able to spawn threads (see
// http://crbug.com/53200). Don't call Gestalt().
struct utsname uname_info;
if (uname(&uname_info) != 0)
return false;
if (strcmp(uname_info.sysname, "Darwin") != 0)
return false;
char* dot = strchr(uname_info.release, '.');
if (!dot)
return false;
int darwin_major_version = atoi(uname_info.release);
if (darwin_major_version < 6)
return false;
// The Darwin major version is always 4 greater than the Mac OS X minor
// version for Darwin versions beginning with 6, corresponding to Mac OS X
// 10.2.
int mac_os_x_minor_version = darwin_major_version - 4;
// Chrome is known to work on 10.6 - 10.8.
return mac_os_x_minor_version >= 6 && mac_os_x_minor_version <= 8;
}
// Returns the pid/gid of the logged-in user, even if getuid() claims that the
// current user is root.
// Returns NULL on error.
passwd* GetRealUserId() {
CFDictionaryRef session_info_dict = CGSessionCopyCurrentDictionary();
[NSMakeCollectable(session_info_dict) autorelease];
if (!session_info_dict)
return NULL; // Possibly no screen plugged in.
CFNumberRef ns_uid = (CFNumberRef)CFDictionaryGetValue(session_info_dict,
kCGSessionUserIDKey);
if (CFGetTypeID(ns_uid) != CFNumberGetTypeID())
return NULL;
uid_t uid;
BOOL success = CFNumberGetValue(ns_uid, kCFNumberSInt32Type, &uid);
if (!success)
return NULL;
return getpwuid(uid);
}
enum TicketKind {
kSystemTicket, kUserTicket
};
// Replaces "~~" with |home_dir|.
NSString* AdjustHomedir(NSString* s, const char* home_dir) {
if (![s hasPrefix:@"~~"])
return s;
NSString* ns_home_dir = [NSString stringWithUTF8String:home_dir];
return [ns_home_dir stringByAppendingString:[s substringFromIndex:2]];
}
// If |chrome_path| is not 0, |*chrome_path| is set to the path where chrome
// is according to keystone. It's only set if that path exists on disk.
BOOL FindChromeTicket(TicketKind kind, const passwd* user,
NSString** chrome_path) {
if (chrome_path)
*chrome_path = nil;
// Don't use Objective-C 2 loop syntax, in case an installer runs on 10.4.
NSMutableArray* keystone_paths =
[NSMutableArray arrayWithObject:kSystemKsadminPath];
if (kind == kUserTicket) {
[keystone_paths insertObject:AdjustHomedir(kUserKsadminPath, user->pw_dir)
atIndex:0];
}
NSEnumerator* e = [keystone_paths objectEnumerator];
id ks_path;
while ((ks_path = [e nextObject])) {
if (![[NSFileManager defaultManager] fileExistsAtPath:ks_path])
continue;
NSTask* task = nil;
NSString* string = nil;
bool ksadmin_ran_successfully = false;
@try {
task = [[NSTask alloc] init];
[task setLaunchPath:ks_path];
NSArray* arguments = @[
kind == kUserTicket ? @"--user-store" : @"--system-store",
@"--print-tickets",
@"--productid",
@"com.google.Chrome",
];
if (geteuid() == 0 && kind == kUserTicket) {
NSString* run_as = [NSString stringWithUTF8String:user->pw_name];
[task setLaunchPath:@"/usr/bin/sudo"];
arguments = [@[@"-u", run_as, ks_path]
arrayByAddingObjectsFromArray:arguments];
}
[task setArguments:arguments];
NSPipe* pipe = [NSPipe pipe];
[task setStandardOutput:pipe];
NSFileHandle* file = [pipe fileHandleForReading];
[task launch];
NSData* data = [file readDataToEndOfFile];
[task waitUntilExit];
ksadmin_ran_successfully = [task terminationStatus] == 0;
string = [[[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding] autorelease];
}
@catch (id exception) {
// Most likely, ks_path didn't exist.
}
[task release];
if (ksadmin_ran_successfully && [string length] > 0) {
// If the user deleted chrome, it doesn't get unregistered in keystone.
// Check if the path keystone thinks chrome is at still exists, and if not
// treat this as "chrome isn't installed". Sniff for
// xc=<KSPathExistenceChecker:1234 path=/Applications/Google Chrome.app>
// in the output. But don't mess with system tickets, since reinstalling
// a user chrome on top of a system ticket produces a non-autoupdating
// chrome.
NSRange start = [string rangeOfString:@"\n\txc=<KSPathExistenceChecker:"];
if (start.location == NSNotFound && start.length == 0)
return YES; // Err on the cautious side.
string = [string substringFromIndex:start.location];
start = [string rangeOfString:@"path="];
if (start.location == NSNotFound && start.length == 0)
return YES; // Err on the cautious side.
string = [string substringFromIndex:start.location];
NSRange end = [string rangeOfString:@".app>\n\t"];
if (end.location == NSNotFound && end.length == 0)
return YES;
string = [string substringToIndex:NSMaxRange(end) - [@">\n\t" length]];
string = [string substringFromIndex:start.length];
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:string];
if (exists && chrome_path)
*chrome_path = string;
// Don't allow reinstallation over a system ticket, even if chrome doesn't
// exist on disk.
if (kind == kSystemTicket)
return YES;
return exists;
}
}
return NO;
}
// File permission mask for files created by gcapi.
const mode_t kUserPermissions = 0755;
const mode_t kAdminPermissions = 0775;
BOOL CreatePathToFile(NSString* path, const passwd* user) {
path = [path stringByDeletingLastPathComponent];
// Default owner, group, permissions:
// * Permissions are set according to the umask of the current process. For
// more information, see umask.
// * The owner ID is set to the effective user ID of the process.
// * The group ID is set to that of the parent directory.
// The default group ID is fine. Owner ID is fine if creating a system path,
// but when creating a user path explicitly set the owner in case euid is 0.
// Do set permissions explicitly; for admin paths all admins can write, for
// user paths just the owner may.
NSMutableDictionary* attributes = [NSMutableDictionary dictionary];
if (user) {
[attributes setObject:[NSNumber numberWithShort:kUserPermissions]
forKey:NSFilePosixPermissions];
[attributes setObject:[NSNumber numberWithInt:user->pw_uid]
forKey:NSFileOwnerAccountID];
} else {
[attributes setObject:[NSNumber numberWithShort:kAdminPermissions]
forKey:NSFilePosixPermissions];
[attributes setObject:@"admin" forKey:NSFileGroupOwnerAccountName];
}
NSFileManager* manager = [NSFileManager defaultManager];
return [manager createDirectoryAtPath:path
withIntermediateDirectories:YES
attributes:attributes
error:nil];
}
// Tries to write |data| at |user_path|.
// Returns the path where it wrote, or nil on failure.
NSString* WriteUserData(NSData* data,
NSString* user_path,
const passwd* user) {
user_path = AdjustHomedir(user_path, user->pw_dir);
if (CreatePathToFile(user_path, user) &&
[data writeToFile:user_path atomically:YES]) {
chmod([user_path fileSystemRepresentation], kUserPermissions & ~0111);
chown([user_path fileSystemRepresentation], user->pw_uid, user->pw_gid);
return user_path;
}
return nil;
}
// Tries to write |data| at |system_path| or if that fails at |user_path|.
// Returns the path where it wrote, or nil on failure.
NSString* WriteData(NSData* data,
NSString* system_path,
NSString* user_path,
const passwd* user) {
// Try system first.
if (CreatePathToFile(system_path, NULL) &&
[data writeToFile:system_path atomically:YES]) {
chmod([system_path fileSystemRepresentation], kAdminPermissions & ~0111);
// Make sure the file is owned by group admin.
if (group* group = getgrnam("admin"))
chown([system_path fileSystemRepresentation], 0, group->gr_gid);
return system_path;
}
// Failed, try user.
return WriteUserData(data, user_path, user);
}
NSString* WriteBrandCode(const char* brand_code, const passwd* user) {
NSDictionary* brand_dict = @{
kBrandKey: [NSString stringWithUTF8String:brand_code],
};
NSData* contents = [NSPropertyListSerialization
dataFromPropertyList:brand_dict
format:NSPropertyListBinaryFormat_v1_0
errorDescription:nil];
return WriteUserData(contents, kUserBrandPath, user);
}
BOOL WriteMasterPrefs(const char* master_prefs_contents,
size_t master_prefs_contents_size,
const passwd* user) {
NSData* contents = [NSData dataWithBytes:master_prefs_contents
length:master_prefs_contents_size];
return WriteData(
contents, kSystemMasterPrefsPath, kUserMasterPrefsPath, user) != nil;
}
NSString* PathToFramework(NSString* app_path, NSDictionary* info_plist) {
NSString* version = [info_plist objectForKey:@"CFBundleShortVersionString"];
if (!version)
return nil;
return [[[app_path
stringByAppendingPathComponent:@"Contents/Versions"]
stringByAppendingPathComponent:version]
stringByAppendingPathComponent:@"Google Chrome Framework.framework"];
}
NSString* PathToInstallScript(NSString* app_path, NSDictionary* info_plist) {
return [PathToFramework(app_path, info_plist) stringByAppendingPathComponent:
@"Resources/install.sh"];
}
bool isbrandchar(int c) {
// Always four upper-case alpha chars.
return c >= 'A' && c <= 'Z';
}
} // namespace
int GoogleChromeCompatibilityCheck(unsigned* reasons) {
unsigned local_reasons = 0;
@autoreleasepool {
passwd* user = GetRealUserId();
if (!IsOSXVersionSupported())
local_reasons |= GCCC_ERROR_OSNOTSUPPORTED;
if (FindChromeTicket(kSystemTicket, NULL, NULL))
local_reasons |= GCCC_ERROR_ALREADYPRESENT;
if (FindChromeTicket(kUserTicket, user, NULL))
local_reasons |= GCCC_ERROR_ALREADYPRESENT;
if (![[NSFileManager defaultManager] isWritableFileAtPath:@"/Applications"])
local_reasons |= GCCC_ERROR_ACCESSDENIED;
if ([[NSFileManager defaultManager] fileExistsAtPath:kChromeInstallPath])
local_reasons |= GCCC_ERROR_ALREADYPRESENT;
}
if (reasons != NULL)
*reasons = local_reasons;
return local_reasons == 0;
}
int InstallGoogleChrome(const char* source_path,
const char* brand_code,
const char* master_prefs_contents,
unsigned master_prefs_contents_size) {
if (!GoogleChromeCompatibilityCheck(NULL))
return 0;
@autoreleasepool {
passwd* user = GetRealUserId();
NSString* app_path = [NSString stringWithUTF8String:source_path];
NSString* info_plist_path =
[app_path stringByAppendingPathComponent:@"Contents/Info.plist"];
NSDictionary* info_plist =
[NSDictionary dictionaryWithContentsOfFile:info_plist_path];
// Use install.sh from the Chrome app bundle to copy Chrome to its
// destination.
NSString* install_script = PathToInstallScript(app_path, info_plist);
if (!install_script) {
return 0;
}
@try {
// install.sh tries to make the installed app admin-writable, but
// only when it's not run as root.
NSString* run_as = [NSString stringWithUTF8String:user->pw_name];
NSTask* task = [[[NSTask alloc] init] autorelease];
[task setLaunchPath:@"/usr/bin/sudo"];
[task setArguments:
@[@"-u", run_as, install_script, app_path, kChromeInstallPath]];
[task launch];
[task waitUntilExit];
if ([task terminationStatus] != 0) {
return 0;
}
}
@catch (id exception) {
return 0;
}
// Set brand code. If Chrome's Info.plist contains a brand code, use that.
NSString* info_plist_brand = [info_plist objectForKey:kBrandKey];
if (info_plist_brand &&
[info_plist_brand respondsToSelector:@selector(UTF8String)])
brand_code = [info_plist_brand UTF8String];
BOOL valid_brand_code = brand_code && strlen(brand_code) == 4 &&
isbrandchar(brand_code[0]) && isbrandchar(brand_code[1]) &&
isbrandchar(brand_code[2]) && isbrandchar(brand_code[3]);
NSString* brand_path = nil;
if (valid_brand_code)
brand_path = WriteBrandCode(brand_code, user);
// Write master prefs.
if (master_prefs_contents)
WriteMasterPrefs(master_prefs_contents, master_prefs_contents_size, user);
// TODO Set default browser if requested.
}
return 1;
}
int LaunchGoogleChrome() {
@autoreleasepool {
passwd* user = GetRealUserId();
NSString* app_path;
NSString* path;
if (FindChromeTicket(kUserTicket, user, &path) && path)
app_path = path;
else if (FindChromeTicket(kSystemTicket, NULL, &path) && path)
app_path = path;
else
app_path = kChromeInstallPath;
// NSWorkspace launches processes as the current console owner,
// even when running with euid of 0.
return [[NSWorkspace sharedWorkspace] launchApplication:app_path];
}
}
|