1313#import < MPMessagePack/MPXPCProtocol.h>
1414#include < pwd.h>
1515#include < grp.h>
16+ #include < unistd.h>
1617
1718@interface KBHelper ()
1819@property NSTask *redirector;
@@ -73,9 +74,11 @@ - (void)_handleRequestWithMethod:(NSString *)method params:(NSArray *)params mes
7374 return ;
7475 }
7576
76- if (![self _checkRemoteIsAdmin: remote]) {
77- KBLog (@" Remote caller into Helper process didn't have admin permissions" );
78- completion (KBMakeError (MPXPCErrorCodeInvalidRequest, @" Invalid admin permissions" ), nil );
77+ uid_t uid = xpc_connection_get_euid (remote);
78+
79+ if (![self _checkUID: uid inGroup: " staff" ]) {
80+ KBLog (@" Remote caller into Helper process didn't have staff permissions" );
81+ completion (KBMakeError (MPXPCErrorCodeInvalidRequest, @" Invalid staff permissions" ), nil );
7982 return ;
8083 }
8184
@@ -100,8 +103,8 @@ - (void)_handleRequestWithMethod:(NSString *)method params:(NSArray *)params mes
100103 [KBKext copyWithSource: args[@" source" ] destination: args[@" destination" ] removeExisting: YES completion: completion];
101104 } else if ([method isEqualToString: @" remove" ]) {
102105 [self remove: args[@" path" ] completion: completion];
103- } else if ([method isEqualToString: @" move " ]) {
104- [self moveFromSource: args[ @" source " ] destination: args[ @" destination " ] overwriteDestination: YES completion : completion];
106+ } else if ([method isEqualToString: @" uninstallAppBundle " ]) {
107+ [self uninstallAppBundle : completion];
105108 } else if ([method isEqualToString: @" createDirectory" ]) {
106109 [self createDirectory: args[@" directory" ] uid: args[@" uid" ] gid: args[@" gid" ] permissions: args[@" permissions" ] excludeFromBackup: [args[@" excludeFromBackup" ] boolValue ] completion: completion];
107110 } else if ([method isEqualToString: @" addToPath" ]) {
@@ -117,33 +120,45 @@ - (void)_handleRequestWithMethod:(NSString *)method params:(NSArray *)params mes
117120 }
118121}
119122
120- - (BOOL )_checkRemoteIsAdmin : (xpc_connection_t )remote {
123+ - (BOOL )_checkRemote : (xpc_connection_t )remote inGroup : (char *)group {
124+ uid_t uid = xpc_connection_get_euid (remote);
125+ return [self _checkUID: uid inGroup: group];
126+ }
127+
128+ - (BOOL )_checkUID : (uid_t )uid inGroup : (char *)group {
121129 char getpwuid_buf[1024 ];
122130 char getgrnam_buf[8192 ];
131+ int gids[1024 ];
132+ int n_gids = 1024 ;
123133
124- uid_t uid = xpc_connection_get_euid (remote);
125134 struct passwd pwentry;
126135 struct passwd *pwentryp;
127136 int tmp = getpwuid_r (uid, &pwentry, getpwuid_buf, sizeof (getpwuid_buf), &pwentryp);
128137 if (tmp != 0 || pwentryp == NULL ) {
129138 KBLog (@" Failed to get PW entry for uid %@ " , uid);
130139 return NO ;
131140 }
132- struct group admin_group ;
133- struct group *admin_group_p ;
134- tmp = getgrnam_r (" admin " , &admin_group , getgrnam_buf, sizeof (getgrnam_buf), &admin_group_p );
135- if (tmp != 0 || admin_group_p == NULL ) {
136- KBLog (@" Could not call getgrnam on the 'admin ' group, so can't tell if remote is an admin " );
141+ struct group wanted_group ;
142+ struct group *wanted_group_p ;
143+ tmp = getgrnam_r (group , &wanted_group , getgrnam_buf, sizeof (getgrnam_buf), &wanted_group_p );
144+ if (tmp != 0 || wanted_group_p == NULL ) {
145+ KBLog (@" Could not call getgrnam on the '%s ' group, so can't tell if uid is in it " , group );
137146 return NO ;
138147 }
139- int n = 0 ;
140- for (char **p = admin_group_p->gr_mem ; *p; p++, n++) {
141- if (strcmp (pwentryp->pw_name , *p) == 0 ) {
142- KBLog (@" Yes, the calling user '%s ' is an 'admin'" , pwentryp->pw_name );
148+
149+ tmp = getgrouplist (pwentryp->pw_name , pwentryp->pw_gid , gids, &n_gids);
150+ if (tmp != 0 ) {
151+ KBLog (@" Could not call getgrouplist for '%s '" , pwentryp->pw_name );
152+ return NO ;
153+ }
154+
155+ for (int i = 0 ; i < n_gids; i++) {
156+ if (gids[i] == wanted_group_p->gr_gid ) {
157+ KBLog (@" Checked that the calling user '%s ' (%d ) is in '%s ' (%d )" , pwentryp->pw_name , uid, group, wanted_group_p);
143158 return YES ;
144159 }
145160 }
146- KBLog (@" The 'admin' group had %d members but '%s ' wasn't one " , n, pwentryp->pw_name );
161+ KBLog (@" The user ' %s ' was in %d groups but not in '%s '" , pwentryp->pw_name , n_gids, group );
147162 return NO ;
148163}
149164
@@ -423,19 +438,27 @@ - (void)removeFromPath:(NSString *)directory name:(NSString *)name appName:(NSSt
423438 }
424439}
425440
426- - (void ) moveFromSource : (NSString *)source destination : (NSString *)destination overwriteDestination : ( BOOL ) overwriteDestination completion : ( void (^)( NSError *error, id value)) completion {
441+ - (NSError *) _moveFromSource : (NSString *)source destination : (NSString *)destination {
427442 NSError *error = nil ;
428443 if ([NSFileManager .defaultManager fileExistsAtPath: destination isDirectory: NULL ] && ![NSFileManager .defaultManager removeItemAtPath: destination error: &error]) {
429- completion (error, nil );
430- return ;
444+ return error;
431445 }
432-
433446 if (![NSFileManager .defaultManager moveItemAtPath: source toPath: destination error: &error]) {
434- completion (error, nil );
435- return ;
447+ return error;
436448 }
449+ return nil ;
450+ }
437451
438- completion (nil , @{});
452+ - (void )uninstallAppBundle : (void (^)(NSError *error, id value))completion {
453+ NSError *error = nil ;
454+ NSString *source = @" /Applications/Keybase.app" ;
455+ NSString *destination = @" /tmp/Keybase.app" ;
456+ error = [self _moveFromSource: source destination: destination];
457+ if (error == nil ) {
458+ completion (nil , @{});
459+ } else {
460+ completion (error, nil );
461+ }
439462}
440463
441464- (void )remove : (NSString *)path completion : (void (^)(NSError *error, id value))completion {
0 commit comments