forked from BOINC/boinc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuninstall.cpp
1722 lines (1456 loc) · 60.5 KB
/
uninstall.cpp
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2017 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
/* uninstall.cpp */
#define TESTING 0 /* for debugging */
#define VERBOSE_TEST 0 /* for debugging callPosixSpawn */
#include <Carbon/Carbon.h>
#include <grp.h>
#include <unistd.h> // geteuid, seteuid
#include <pwd.h> // passwd, getpwnam
#include <dirent.h>
#include <sys/param.h> // for MAXPATHLEN
#include <sys/stat.h> // For stat()
#include <sys/time.h>
#include <string.h>
#include <vector>
#include <string>
using std::vector;
using std::string;
// WARNING -- SEARCHFORALLBOINCMANAGERS CODE HAS NOT BEEN TESTED
#define SEARCHFORALLBOINCMANAGERS 0
#define MAX_LANGUAGES_TO_TRY 5
#define MANIPULATE_LOGINITEM_PLIST_FILE 0
#include "mac_util.h"
#include "translate.h"
static OSStatus DoUninstall(void);
static OSStatus CleanupAllVisibleUsers(void);
static OSStatus DeleteOurBundlesFromDirectory(CFStringRef bundleID, char *extension, char *dirPath);
static void DeleteLoginItemOSAScript(char* user, char* appName);
static char * PersistentFGets(char *buf, size_t buflen, FILE *f);
OSErr GetCurrentScreenSaverSelection(char *moduleName, size_t maxLen);
OSErr SetScreenSaverSelection(char *moduleName, char *modulePath, int type);
static pid_t FindProcessPID(char* name, pid_t thePID);
static int KillOneProcess(char* name);
static double dtime(void);
static void SleepSeconds(double seconds);
static void GetPreferredLanguages();
static void LoadPreferredLanguages();
static Boolean ShowMessage(Boolean allowCancel, Boolean continueButton, Boolean yesNoButtons, const char *format, ...);
int callPosixSpawn(const char *cmd, bool delayForResult=false);
void print_to_log_file(const char *format, ...);
#if MANIPULATE_LOGINITEM_PLIST_FILE
static void DeleteLoginItemFromPListFile(void);
int GetCountOfLoginItemsFromPlistFile(void);
OSErr GetLoginItemNameAtIndexFromPlistFile(int index, char *name, size_t maxLen);
OSErr DeleteLoginItemNameAtIndexFromPlistFile(int index);
#endif
static char gAppName[256];
static char gBrandName[256];
static char gCatalogsDir[MAXPATHLEN];
static char * gCatalog_Name = (char *)"BOINC-Setup";
/* BEGIN TEMPORARY ITEMS TO ALLOW TRANSLATORS TO START WORK */
void notused() {
ShowMessage(true, false, false, (char *)_("OK"));
}
/* END TEMPORARY ITEMS TO ALLOW TRANSLATORS TO START WORK */
int main(int argc, char *argv[])
{
char pathToSelf[MAXPATHLEN], pathToVBoxUninstallTool[MAXPATHLEN], *p;
char cmd[MAXPATHLEN+64];
Boolean cancelled = false;
pid_t activeAppPID = 0;
struct stat sbuf;
OSStatus err = noErr;
pathToSelf[0] = '\0';
// Get the full path to our executable inside this application's bundle
getPathToThisApp(pathToSelf, sizeof(pathToSelf));
if (!pathToSelf[0]) {
ShowMessage(false, false, false, "Couldn't get path to self.");
return err;
}
strlcpy(pathToVBoxUninstallTool, pathToSelf, sizeof(pathToVBoxUninstallTool));
strlcat(pathToVBoxUninstallTool, "/Contents/Resources/VirtualBox_Uninstall.tool", sizeof(pathToVBoxUninstallTool));
// To allow for branding, assume name of executable inside bundle is same as name of bundle
p = strrchr(pathToSelf, '/'); // Assume name of executable inside bundle is same as name of bundle
if (p == NULL)
p = pathToSelf - 1;
strlcpy(gAppName, p+1, sizeof(gAppName));
p = strrchr(gAppName, '.'); // Strip off bundle extension (".app")
if (p)
*p = '\0';
strlcpy(gCatalogsDir, pathToSelf, sizeof(gCatalogsDir));
strlcat(gCatalogsDir, "/Contents/Resources/locale/", sizeof(gCatalogsDir));
strlcat(pathToSelf, "/Contents/MacOS/", sizeof(pathToSelf));
strlcat(pathToSelf, gAppName, sizeof(pathToSelf));
p = strchr(gAppName, ' ');
p += 1; // Point to brand name following "Uninstall "
strlcpy(gBrandName, p, sizeof(gBrandName));
// Determine whether this is the intial launch or the relaunch with privileges
if ( (argc == 3) && (strcmp(argv[1], "--privileged") == 0) ) {
// Prevent displaying "OSAScript" in menu bar on newer versions of OS X
activeAppPID = (pid_t)atol(argv[2]);
if (activeAppPID > 0) {
BringAppWithPidToFront(activeAppPID); // Usually Finder
}
// Give the run loop a chance to handle the BringAppWithPidToFront call
// CFRunLoopRunInMode(kCFRunLoopCommonModes, (CFTimeInterval)0.5, false);
// Apparently, usleep() lets run loop run
usleep(100000);
LoadPreferredLanguages();
if (geteuid() != 0) { // Confirm that we are running as root
ShowMessage(false, false, false, (char *)_("Permission error after relaunch"));
BOINCTranslationCleanup();
return permErr;
}
ShowMessage(false, true, false, (char *)_("Removal may take several minutes.\nPlease be patient."));
err = DoUninstall();
BOINCTranslationCleanup();
return err;
}
// This is the initial launch. Authenticate and relaunch ourselves with privileges.
GetPreferredLanguages(); // We must do this before switching to root user
LoadPreferredLanguages();
// Grid Republic uses generic dialog with Uninstall application's icon
cancelled = ! ShowMessage(true, true, false, (char *)_(
"Are you sure you want to completely remove %s from your computer?\n\n"
"This will remove the executables but will not touch %s data files."), p, p);
if (! cancelled) {
// Prevent displaying "OSAScript" in menu bar on newer versions of OS X
activeAppPID = getActiveAppPid();
// ShowMessage(false, true, false, "active app = %d", activeAppPID); // for debugging
// The "activate" command brings the password dialog to the front and makes it the active window.
// "with administrator privileges" launches the helper application as user root.
sprintf(cmd, "osascript -e 'activate' -e 'do shell script \"sudo \\\"%s\\\" --privileged %d\" with administrator privileges'", pathToSelf, activeAppPID);
err = callPosixSpawn(cmd, true);
}
if (cancelled || (err != noErr)) {
ShowMessage(false, false, false, (char *)_("Canceled: %s has not been touched."), p);
BOINCTranslationCleanup();
return err;
}
CFStringRef CFBOINCDataPath, CFUserPrefsPath;
char BOINCDataPath[MAXPATHLEN], temp[MAXPATHLEN], PathToPrefs[MAXPATHLEN];
Boolean success = false;
char * loginName = getlogin();
CFURLRef urlref = CFURLCreateWithFileSystemPath(NULL, CFSTR("/Library"),
kCFURLPOSIXPathStyle, true);
success = CFURLCopyResourcePropertyForKey(urlref, kCFURLLocalizedNameKey,
&CFBOINCDataPath, NULL);
CFRelease(urlref);
if (success) {
success = CFStringGetCString(CFBOINCDataPath, temp,
sizeof(temp), kCFStringEncodingUTF8);
CFRelease(CFBOINCDataPath);
}
if (success) {
success = false;
strlcpy(BOINCDataPath, "/", sizeof(BOINCDataPath));
strlcat(BOINCDataPath, temp, sizeof(BOINCDataPath));
strlcat(BOINCDataPath, "/", sizeof(BOINCDataPath));
urlref = CFURLCreateWithFileSystemPath(NULL, CFSTR("/Library/Application Support"),
kCFURLPOSIXPathStyle, true);
success = CFURLCopyResourcePropertyForKey(urlref, kCFURLLocalizedNameKey,
&CFBOINCDataPath, NULL);
CFRelease(urlref);
}
if (success) {
success = CFStringGetCString(CFBOINCDataPath, temp,
sizeof(temp), kCFStringEncodingUTF8);
CFRelease(CFBOINCDataPath);
}
if (success) {
strlcat(BOINCDataPath, temp, sizeof(BOINCDataPath));
strlcat(BOINCDataPath, "/BOINC Data", sizeof(BOINCDataPath));
} else {
strlcpy(BOINCDataPath,
"/Library/Application Support/BOINC Data",
sizeof(BOINCDataPath));
}
success = false;
urlref = CFURLCreateWithFileSystemPath(NULL, CFSTR("/Users"),
kCFURLPOSIXPathStyle, true);
success = CFURLCopyResourcePropertyForKey(urlref, kCFURLLocalizedNameKey,
&CFUserPrefsPath, NULL);
CFRelease(urlref);
if (success) {
success = CFStringGetCString(CFUserPrefsPath, temp, sizeof(temp),
kCFStringEncodingUTF8);
CFRelease(CFUserPrefsPath);
}
if (success) {
success = false;
strlcpy(PathToPrefs, "/", sizeof(PathToPrefs));
strlcat(PathToPrefs, temp, sizeof(PathToPrefs));
strlcat(PathToPrefs, "/[", sizeof(PathToPrefs));
strlcat(PathToPrefs, (char *)_("name of user"), sizeof(PathToPrefs));
strlcat(PathToPrefs, "]/", sizeof(PathToPrefs));
sprintf(temp, "/Users/%s/Library", loginName);
CFUserPrefsPath = CFStringCreateWithCString(kCFAllocatorDefault, temp,
kCFStringEncodingUTF8);
urlref = CFURLCreateWithFileSystemPath(NULL, CFUserPrefsPath,
kCFURLPOSIXPathStyle, true);
CFRelease(CFUserPrefsPath);
success = CFURLCopyResourcePropertyForKey(urlref, kCFURLLocalizedNameKey,
&CFUserPrefsPath, NULL);
CFRelease(urlref);
}
if (success) {
success = CFStringGetCString(CFUserPrefsPath, temp, sizeof(temp),
kCFStringEncodingUTF8);
CFRelease(CFUserPrefsPath);
}
if (success) {
success = false;
strlcat(PathToPrefs, temp, sizeof(PathToPrefs));
strlcat(PathToPrefs, "/", sizeof(PathToPrefs));
sprintf(temp, "/Users/%s/Library/Preferences", loginName);
CFUserPrefsPath = CFStringCreateWithCString(kCFAllocatorDefault, temp,
kCFStringEncodingUTF8);
urlref = CFURLCreateWithFileSystemPath(NULL, CFUserPrefsPath,
kCFURLPOSIXPathStyle, true);
CFRelease(CFUserPrefsPath);
success = CFURLCopyResourcePropertyForKey(urlref, kCFURLLocalizedNameKey,
&CFUserPrefsPath, NULL);
CFRelease(urlref);
}
if (success) {
success = CFStringGetCString(CFUserPrefsPath, temp, sizeof(temp),
kCFStringEncodingUTF8);
CFRelease(CFUserPrefsPath);
}
if (success) {
strlcat(PathToPrefs, temp, sizeof(PathToPrefs));
strlcat(PathToPrefs, "/BOINC Manager Preferences", sizeof(PathToPrefs));
} else {
strlcpy(PathToPrefs,
"/Users/[username]/Library/Preferences/BOINC Manager Preferences",
sizeof(PathToPrefs));
}
// stat() returns zero on success
if (stat(pathToVBoxUninstallTool, &sbuf) == 0) {
char cmd[MAXPATHLEN+30];
cancelled = ! ShowMessage(true, false, true, (char *)_(
"Do you also want to remove VirtualBox from your computer?\n"
"(VirtualBox was installed along with BOINC.)"));
if (! cancelled) {
if (KillOneProcess("VirtualBox")) {
sleep(5);
}
// List of processes to kill taken from my_processes
// array in VirtualBox_Uninstall.tool script:
KillOneProcess("VirtualBox-amd64");
KillOneProcess("VirtualBox-x86");
KillOneProcess("VirtualBoxVM");
KillOneProcess("VirtualBoxVM-amd64");
KillOneProcess("VirtualBoxVM-x86");
KillOneProcess("VBoxManage");
KillOneProcess("VBoxManage-amd64");
KillOneProcess("VBoxManage-x86");
KillOneProcess("VBoxHeadless");
KillOneProcess("VBoxHeadless-amd64");
KillOneProcess("VBoxHeadless-x86");
KillOneProcess("vboxwebsrv");
KillOneProcess("vboxwebsrv-amd64");
KillOneProcess("vboxwebsrv-x86");
KillOneProcess("VBoxXPCOMIPCD");
KillOneProcess("VBoxXPCOMIPCD-amd64");
KillOneProcess("VBoxXPCOMIPCD-x86");
KillOneProcess("VBoxSVC");
KillOneProcess("VBoxSVC-amd64");
KillOneProcess("VBoxSVC-x86");
KillOneProcess("VBoxNetDHCP");
KillOneProcess("VBoxNetDHCP-amd64");
KillOneProcess("VBoxNetDHCP-x86");
sleep(2);
snprintf(cmd, sizeof(cmd), "source \"%s\" --unattended", pathToVBoxUninstallTool);
callPosixSpawn(cmd);
}
}
ShowMessage(false, false, false, (char *)_("Removal completed.\n\n You may want to remove the following remaining items using the Finder: \n"
"the directory \"%s\"\n\nfor each user, the file\n"
"\"%s\"."), BOINCDataPath, PathToPrefs);
BOINCTranslationCleanup();
return err;
}
static OSStatus DoUninstall(void) {
pid_t coreClientPID = 0;
pid_t BOINCManagerPID = 0;
char cmd[1024];
char *p;
passwd *pw;
OSStatus err = noErr;
#if SEARCHFORALLBOINCMANAGERS
char myRmCommand[MAXPATHLEN+10], plistRmCommand[MAXPATHLEN+10];
char notBoot[] = "/Volumes/";
CFStringRef cfPath;
CFURLRef appURL;
int pathOffset, i;
#endif
#if TESTING
ShowMessage(false, false, false, "Permission OK after relaunch");
#endif
//TODO: It would be nice to get the app name from the bundle ID or signature
// so we don't have to try all 4 and to allow for future branded versions
for (;;) {
BOINCManagerPID = FindProcessPID("BOINCManager", 0);
if (BOINCManagerPID == 0) break;
kill(BOINCManagerPID, SIGTERM);
sleep(2);
}
for (;;) {
BOINCManagerPID = FindProcessPID("GridRepublic Desktop", 0);
if (BOINCManagerPID == 0) break;
kill(BOINCManagerPID, SIGTERM);
sleep(2);
}
for (;;) {
BOINCManagerPID = FindProcessPID("Progress Thru Processors Desktop", 0);
if (BOINCManagerPID == 0) break;
kill(BOINCManagerPID, SIGTERM);
sleep(2);
}
for (;;) {
BOINCManagerPID = FindProcessPID("Charity Engine Desktop", 0);
if (BOINCManagerPID == 0) break;
kill(BOINCManagerPID, SIGTERM);
sleep(2);
}
// Core Client may still be running if it was started without Manager
coreClientPID = FindProcessPID("boinc", 0);
if (coreClientPID)
kill(coreClientPID, SIGTERM); // boinc catches SIGTERM & exits gracefully
#if SEARCHFORALLBOINCMANAGERS
// WARNING -- SEARCHFORALLBOINCMANAGERS CODE HAS NOT BEEN TESTED
// Phase 1: try to find all our applications using LaunchServices
for (i=0; i<100; i++) {
strlcpy(myRmCommand, "rm -rf \"", 10);
pathOffset = strlen(myRmCommand);
err = GetPathToAppFromID('BNC!', CFSTR("edu.berkeley.boinc"), myRmCommand+pathOffset, MAXPATHLEN);
if (err) {
break;
}
strlcat(myRmCommand, "\"", sizeof(myRmCommand));
#if TESTING
ShowMessage(false, false, false, "manager: %s", myRmCommand);
#endif
p = strstr(myRmCommand, notBoot);
if (p == myRmCommand+pathOffset) {
#if TESTING
ShowMessage(false, false, false, "Not on boot volume: %s", myRmCommand);
#endif
break;
} else {
// First delete just the application's info.plist file and update the
// LaunchServices Database; otherwise GetPathToAppFromID might return
// this application again after it's been deleted.
strlcpy(plistRmCommand, myRmCommand, sizeof(plistRmCommand));
strlcat(plistRmCommand, "/Contents/info.plist", sizeof(plistRmCommand));
#if TESTING
ShowMessage(false, false, false, "Deleting info.plist: %s", plistRmCommand);
#endif
callPosixSpawn(plistRmCommand);
cfPath = CFStringCreateWithCString(NULL, myRmCommand+pathOffset, kCFStringEncodingUTF8);
appURL = CFURLCreateWithFileSystemPath(NULL, CFStringRef filePath, kCFURLPOSIXPathStyle, true);
if (cfPath) {
CFRelease(cfPath);
}
if (appURL) {
CFRelease(appURL);
}
err = LSRegisterURL, true);
#if TESTING
if (err)
ShowMessage(false, false, false, "LSRegisterFSRef returned error %d", err);
#endif
callPosixSpawn(myRmCommand);
}
}
#endif // SEARCHFORALLBOINCMANAGERS
// Phase 2: step through default Applications directory searching for our applications
err = DeleteOurBundlesFromDirectory(CFSTR("edu.berkeley.boinc"), "app", "/Applications");
// Phase 3: step through default Screen Savers directory searching for our screen savers
err = DeleteOurBundlesFromDirectory(CFSTR("edu.berkeley.boincsaver"), "saver", "/Library/Screen Savers");
// Phase 4: Delete our files and directories at our installer's default locations
// Remove everything we've installed, whether BOINC, GridRepublic, Progress Thru Processors or
// Charity Engine
//TODO: It would be nice to get the app name from the bundle ID or signature
// so we don't have to try all 4 and to allow for future branded versions
// These first 4 should already have been deleted by the above code, but do them anyway for safety
callPosixSpawn ("rm -rf /Applications/BOINCManager.app");
callPosixSpawn ("rm -rf \"/Library/Screen Savers/BOINCSaver.saver\"");
callPosixSpawn ("rm -rf \"/Applications/GridRepublic Desktop.app\"");
callPosixSpawn ("rm -rf \"/Library/Screen Savers/GridRepublic.saver\"");
callPosixSpawn ("rm -rf \"/Applications/Progress Thru Processors Desktop.app\"");
callPosixSpawn ("rm -rf \"/Library/Screen Savers/Progress Thru Processors.saver\"");
callPosixSpawn ("rm -rf \"/Applications/Charity Engine Desktop.app\"");
callPosixSpawn ("rm -rf \"/Library/Screen Savers/Charity Engine.saver\"");
// Delete any receipt from an older installer (which had
// a wrapper application around the installer package.)
callPosixSpawn ("rm -rf /Library/Receipts/GridRepublic.pkg");
callPosixSpawn ("rm -rf /Library/Receipts/Progress\\ Thru\\ Processors.pkg");
callPosixSpawn ("rm -rf /Library/Receipts/Charity\\ Engine.pkg");
callPosixSpawn ("rm -rf /Library/Receipts/BOINC.pkg");
// Delete any receipt from a newer installer (a bare package.)
callPosixSpawn ("rm -rf /Library/Receipts/GridRepublic\\ Installer.pkg");
callPosixSpawn ("rm -rf /Library/Receipts/Progress\\ Thru\\ Processors\\ Installer.pkg");
callPosixSpawn ("rm -rf /Library/Receipts/Charity\\ Engine\\ Installer.pkg");
callPosixSpawn ("rm -rf /Library/Receipts/BOINC\\ Installer.pkg");
// Phase 5: Set BOINC Data owner and group to logged in user
// We don't customize BOINC Data directory name for branding
// callPosixSpawn ("rm -rf \"/Library/Application Support/BOINC Data\"");
p = getlogin();
pw = getpwnam(p);
sprintf(cmd, "chown -R %d:%d \"/Library/Application Support/BOINC Data\"", pw->pw_uid, pw->pw_gid);
callPosixSpawn (cmd);
callPosixSpawn("chmod -R u+rw-s,g+r-w-s,o+r-w \"/Library/Application Support/BOINC Data\"");
callPosixSpawn("chmod 600 \"/Library/Application Support/BOINC Data/gui_rpc_auth.cfg\"");
// Phase 6: step through all users and do user-specific cleanup
CleanupAllVisibleUsers();
callPosixSpawn ("dscl . -delete /users/boinc_master");
callPosixSpawn ("dscl . -delete /groups/boinc_master");
callPosixSpawn ("dscl . -delete /users/boinc_project");
callPosixSpawn ("dscl . -delete /groups/boinc_project");
return 0;
}
static OSStatus DeleteOurBundlesFromDirectory(CFStringRef bundleID, char *extension, char *dirPath) {
DIR *dirp;
dirent *dp;
CFStringRef urlStringRef = NULL;
int index;
CFStringRef thisID = NULL;
CFBundleRef thisBundle = NULL;
CFURLRef bundleURLRef = NULL;
char myRmCommand[MAXPATHLEN+10], *p;
int pathOffset;
dirp = opendir(dirPath);
if (dirp == NULL) { // Should never happen
ShowMessage(false, false, false, "Error: opendir(\"%s\") failed", dirPath);
return -1;
}
index = -1;
while (true) {
index++;
dp = readdir(dirp);
if (dp == NULL)
break; // End of list
p = strrchr(dp->d_name, '.');
if (p == NULL)
continue;
if (strcmp(p+1, extension))
continue;
strlcpy(myRmCommand, "rm -rf \"", 10);
pathOffset = strlen(myRmCommand);
strlcat(myRmCommand, dirPath, sizeof(myRmCommand));
strlcat(myRmCommand, "/", sizeof(myRmCommand));
strlcat(myRmCommand, dp->d_name, sizeof(myRmCommand));
urlStringRef = CFStringCreateWithCString(kCFAllocatorDefault, myRmCommand+pathOffset, CFStringGetSystemEncoding());
if (urlStringRef) {
bundleURLRef = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, urlStringRef, kCFURLPOSIXPathStyle, false);
if (bundleURLRef) {
thisBundle = CFBundleCreate( kCFAllocatorDefault, bundleURLRef );
if (thisBundle) {
thisID = CFBundleGetIdentifier(thisBundle);
if (thisID) {
strlcat(myRmCommand, "\"", sizeof(myRmCommand));
if (CFStringCompare(thisID, bundleID, 0) == kCFCompareEqualTo) {
#if TESTING
ShowMessage(false, false, false, "Bundles: %s", myRmCommand);
#endif
callPosixSpawn(myRmCommand);
} else {
#if TESTING
// ShowMessage(false, false, false, "Bundles: Not deleting %s", myRmCommand+pathOffset);
#endif
}
} // if (thisID)
#if TESTING
else
ShowMessage(false, false, false, "CFBundleGetIdentifier failed for index %d", index);
#endif
CFRelease(thisBundle);
} //if (thisBundle)
#if TESTING
else
ShowMessage(false, false, false, "CFBundleCreate failed for index %d", index);
#endif
CFRelease(bundleURLRef);
} // if (bundleURLRef)
#if TESTING
else
ShowMessage(false, false, false, "CFURLCreateWithFileSystemPath failed");
#endif
CFRelease(urlStringRef);
} // if (urlStringRef)
#if TESTING
else
ShowMessage(false, false, false, "CFStringCreateWithCString failed");
#endif
} // while true
closedir(dirp);
return noErr;
}
enum {
kSystemEventsCreator = 'sevs'
};
CFStringRef kSystemEventsBundleID = CFSTR("com.apple.systemevents");
char *systemEventsAppName = "System Events";
// Find all visible users and delete their login item to launch BOINC Manager.
// Remove each user from groups boinc_master and boinc_project.
// For now, don't delete user's BOINC Preferences file.
static OSStatus CleanupAllVisibleUsers(void)
{
passwd *pw;
vector<string> human_user_names;
vector<uid_t> human_user_IDs;
uid_t saved_uid, saved_euid;
char human_user_name[256];
int i;
int userIndex;
int flag;
char buf[256];
char s[1024];
char cmd[2048];
char systemEventsPath[1024];
pid_t systemEventsPID;
FILE *f;
char *p;
int id;
OSStatus err;
Boolean changeSaver;
saved_uid = getuid();
saved_euid = geteuid();
err = noErr;
systemEventsPath[0] = '\0';
err = GetPathToAppFromID(kSystemEventsCreator, kSystemEventsBundleID, systemEventsPath, sizeof(systemEventsPath));
#if TESTING
if (err == noErr) {
ShowMessage(false, false, false, "SystemEvents is at %s", systemEventsPath);
} else {
ShowMessage(false, false, false, "GetPathToAppFromID(kSystemEventsCreator, kSystemEventsBundleID) returned error %d ", (int) err);
}
#endif
// First, find all users on system
f = popen("dscl . list /Users UniqueID", "r");
if (f) {
while (PersistentFGets(buf, sizeof(buf), f)) {
p = strrchr(buf, ' ');
if (p) {
id = atoi(p+1);
if (id < 501) {
#if TESTING
// printf("skipping user ID %d\n", id);
// fflush(stdout);
#endif
continue;
}
while (p > buf) {
if (*p != ' ') break;
--p;
}
*(p+1) = '\0';
human_user_names.push_back(string(buf));
human_user_IDs.push_back((uid_t)id);
#if TESTING
ShowMessage(false, false, false, "user ID %d: %s\n", id, buf);
#endif
*(p+1) = ' ';
}
}
pclose(f);
}
for (userIndex=human_user_names.size(); userIndex>0; --userIndex) {
flag = 0;
strlcpy(human_user_name, human_user_names[userIndex-1].c_str(), sizeof(human_user_name));
// Check whether this user is a login (human) user
sprintf(s, "dscl . -read \"/Users/%s\" NFSHomeDirectory", human_user_name);
f = popen(s, "r");
if (f) {
while (PersistentFGets(buf, sizeof(buf), f)) {
p = strrchr(buf, ' ');
if (p) {
if (strstr(p, "/var/empty") != NULL) flag = 1;
}
}
pclose(f);
}
sprintf(s, "dscl . -read \"/Users/%s\" UserShell", human_user_name);
f = popen(s, "r");
if (f) {
while (PersistentFGets(buf, sizeof(buf), f)) {
p = strrchr(buf, ' ');
if (p) {
if (strstr(p, "/usr/bin/false") != NULL) flag |= 2;
}
}
pclose(f);
}
// Skip all non-human (non-login) users
if (flag == 3) { // if (Home Directory == "/var/empty") && (UserShell == "/usr/bin/false")
#if TESTING
ShowMessage(false, false, false, "Flag=3: skipping user ID %d: %s", human_user_IDs[userIndex-1], buf);
#endif
continue;
}
pw = getpwnam(human_user_name);
if (pw == NULL)
continue;
#if TESTING
ShowMessage(false, false, false, "Deleting login item for user %s: Posix name=%s, Full name=%s, UID=%d",
human_user_name, pw->pw_name, pw->pw_gecos, pw->pw_uid);
#endif
// Remove user from groups boinc_master and boinc_project
sprintf(s, "dscl . -delete /groups/boinc_master users \"%s\"", human_user_name);
callPosixSpawn (s);
sprintf(s, "dscl . -delete /groups/boinc_project users \"%s\"", human_user_name);
callPosixSpawn (s);
#if TESTING
// ShowMessage(false, false, false, "Before seteuid(%d) for user %s, euid = %d", pw->pw_uid, human_user_name, geteuid());
#endif
setuid(0);
// Delete our login item(s) for this user
#if MANIPULATE_LOGINITEM_PLIST_FILE
if (compareOSVersionTo(10, 8) >= 0) {
seteuid(pw->pw_uid); // Temporarily set effective uid to this user
DeleteLoginItemFromPListFile();
seteuid(saved_euid); // Set effective uid back to privileged user
} else { // OS 10.7.x
#endif
// We must leave effective user ID as privileged user (root)
// because the target user may not be in the sudoers file.
// We must launch the System Events application for the target user
#if TESTING
ShowMessage(false, false, false, "Telling System Events to quit (before DeleteLoginItemOSAScript)");
#endif
// Find SystemEvents process. If found, quit it in case
// it is running under a different user.
systemEventsPID = FindProcessPID(systemEventsAppName, 0);
if (systemEventsPID != 0) {
err = kill(systemEventsPID, SIGKILL);
}
#if TESTING
if (err != noErr) {
ShowMessage(false, false, false, "kill(systemEventsPID, SIGKILL) returned error %d ", (int) err);
}
#endif
// Wait for the process to be gone
for (i=0; i<50; ++i) { // 5 seconds max delay
SleepSeconds(0.1); // 1/10 second
systemEventsPID = FindProcessPID(systemEventsAppName, 0);
if (systemEventsPID == 0) break;
}
#if TESTING
if (i >= 50) {
ShowMessage(false, false, false, "Failed to make System Events quit");
}
#endif
sleep(2);
if (systemEventsPath[0] != '\0') {
#if TESTING
ShowMessage(false, false, false, "Launching SystemEvents for user %s", pw->pw_name);
#endif
sprintf(cmd, "sudo -u \"%s\" -b \"%s/Contents/MacOS/System Events\"", pw->pw_name, systemEventsPath);
err = callPosixSpawn(cmd);
if (err == noErr) {
// Wait for the process to start
for (i=0; i<50; ++i) { // 5 seconds max delay
SleepSeconds(0.1); // 1/10 second
systemEventsPID = FindProcessPID(systemEventsAppName, 0);
if (systemEventsPID != 0) break;
}
#if TESTING
if (i >= 50) {
ShowMessage(false, false, false, "Failed to launch System Events for user %s", pw->pw_name);
}
#endif
sleep(2);
DeleteLoginItemOSAScript(pw->pw_name, "BOINCManager");
DeleteLoginItemOSAScript(pw->pw_name, "GridRepublic Desktop");
DeleteLoginItemOSAScript(pw->pw_name, "Progress Thru Processors Desktop");
DeleteLoginItemOSAScript(pw->pw_name, "Charity Engine Desktop");
#if TESTING
} else {
ShowMessage(false, false, false, "[2] Command: %s returned error %d", cmd, (int) err);
#endif
}
}
#if MANIPULATE_LOGINITEM_PLIST_FILE
}
#endif
// We don't delete the user's BOINC Manager preferences
// sprintf(s, "rm -f \"/Users/%s/Library/Preferences/BOINC Manager Preferences\"", human_user_name);
// callPosixSpawn (s);
// Delete per-user BOINC Manager and screensaver files
sprintf(s, "rm -fR \"/Users/%s/Library/Application Support/BOINC\"", human_user_name);
callPosixSpawn (s);
// Set screensaver to "Computer Name" default screensaver only
// if it was BOINC, GridRepublic, Progress Thru Processors or Charity Engine.
changeSaver = false;
seteuid(pw->pw_uid); // Temporarily set effective uid to this user
if (compareOSVersionTo(10, 6) < 0) {
f = popen("defaults -currentHost read com.apple.screensaver moduleName", "r");
if (f) {
while (PersistentFGets(s, sizeof(s), f)) {
if (strstr(s, "BOINCSaver")) {
changeSaver = true;
break;
}
if (strstr(s, "GridRepublic")) {
changeSaver = true;
break;
}
if (strstr(s, "Progress Thru Processors")) {
changeSaver = true;
break;
}
if (strstr(s, "Charity Engine")) {
changeSaver = true;
break;
}
}
pclose(f);
}
} else {
err = GetCurrentScreenSaverSelection(s, sizeof(s) -1);
if (err == noErr) {
if (strstr(s, "BOINCSaver")) {
changeSaver = true;
}
if (strstr(s, "GridRepublic")) {
changeSaver = true;
}
if (strstr(s, "Progress Thru Processors")) {
changeSaver = true;
}
if (strstr(s, "Charity Engine")) {
changeSaver = true;
}
}
}
if (changeSaver) {
if (compareOSVersionTo(10, 6) < 0) {
callPosixSpawn ("defaults -currentHost write com.apple.screensaver moduleName \"Computer Name\"");
callPosixSpawn ("defaults -currentHost write com.apple.screensaver modulePath \"/System/Library/Frameworks/ScreenSaver.framework/Versions/A/Resources/Computer Name.saver\"");
} else {
err = SetScreenSaverSelection("Computer Name",
"/System/Library/Frameworks/ScreenSaver.framework/Versions/A/Resources/Computer Name.saver", 0);
}
}
seteuid(saved_euid); // Set effective uid back to privileged user
#if TESTING
// ShowMessage(false, false, false, "After seteuid(%d) for user %s, euid = %d, saved_uid = %d", pw->pw_uid, human_user_name, geteuid(), saved_uid);
#endif
} // End userIndex loop
sleep(1);
#if TESTING
ShowMessage(false, false, false, "Telling System Events to quit (at end)");
#endif
systemEventsPID = FindProcessPID(systemEventsAppName, 0);
if (systemEventsPID != 0) {
err = kill(systemEventsPID, SIGKILL);
}
#if TESTING
if (err != noErr) {
ShowMessage(false, false, false, "kill(systemEventsPID, SIGKILL) returned error %d ", (int) err);
}
#endif
return noErr;
}
// Used for OS <= 10.7
static void DeleteLoginItemOSAScript(char* user, char* appName)
{
char cmd[2048];
OSErr err;
sprintf(cmd, "sudo -u \"%s\" osascript -e 'tell application \"System Events\"' -e 'delete (every login item whose name contains \"%s\")' -e 'end tell'", user, appName);
err = callPosixSpawn(cmd);
#if TESTING
if (err) {
ShowMessage(false, false, false, "Command: %s returned error %d", cmd, err);
}
#endif
}
#if MANIPULATE_LOGINITEM_PLIST_FILE
// Used for OS >= 10.8
static void DeleteLoginItemFromPListFile(void)
{
Boolean success;
int numberOfLoginItems, counter;
char theName[256], *q;
success = false;
numberOfLoginItems = GetCountOfLoginItemsFromPlistFile();
// Search existing login items in reverse order, deleting ours
for (counter = numberOfLoginItems ; counter > 0 ; counter--)
{
GetLoginItemNameAtIndexFromPlistFile(counter-1, theName, sizeof(theName));
q = theName;
while (*q)
{
// It is OK to modify the returned string because we "own" it
*q = toupper(*q); // Make it case-insensitive
q++;
}
if (strstr(theName, "BOINCMANAGER"))
success = DeleteLoginItemNameAtIndexFromPlistFile(counter-1);
if (strstr(theName, "GRIDREPUBLIC DESKTOP"))
success = DeleteLoginItemNameAtIndexFromPlistFile(counter-1);
if (strstr(theName, "PROGRESS THRU PROCESSORS DESKTOP"))
success = DeleteLoginItemNameAtIndexFromPlistFile(counter-1);
if (strstr(theName, "CHARITY ENGINE DESKTOP"))
success = DeleteLoginItemNameAtIndexFromPlistFile(counter-1);
}
}
#endif // MANIPULATE_LOGINITEM_PLIST_FILE
static char * PersistentFGets(char *buf, size_t buflen, FILE *f) {
char *p = buf;
size_t len = buflen;
size_t datalen = 0;
*buf = '\0';
while (datalen < (buflen - 1)) {
fgets(p, len, f);
if (feof(f)) break;
if (ferror(f) && (errno != EINTR)) break;
if (strchr(buf, '\n')) break;
datalen = strlen(buf);
p = buf + datalen;
len -= datalen;
}
return (buf[0] ? buf : NULL);
}
OSErr GetCurrentScreenSaverSelection(char *moduleName, size_t maxLen) {
OSErr err = noErr;
CFStringRef nameKey = CFStringCreateWithCString(NULL,"moduleName",kCFStringEncodingASCII);
CFStringRef moduleNameAsCFString;
CFDictionaryRef theData;
theData = (CFDictionaryRef)CFPreferencesCopyValue(CFSTR("moduleDict"),
CFSTR("com.apple.screensaver"),
kCFPreferencesCurrentUser,
kCFPreferencesCurrentHost
);
if (theData == NULL) {
CFRelease(nameKey);
return (-1);
}
if (CFDictionaryContainsKey(theData, nameKey) == false)
{
moduleName[0] = 0;
CFRelease(nameKey);
CFRelease(theData);