AppDelegate.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // AppDelegate.m
  3. // MacGap
  4. //
  5. // Created by Alex MacCaw on 08/01/2012.
  6. // Copyright (c) 2012 Twitter. All rights reserved.
  7. //
  8. #import "AppDelegate.h"
  9. #include <sys/stat.h>
  10. #include <sys/types.h>
  11. @implementation AppDelegate
  12. @synthesize windowController;
  13. - (void) applicationWillFinishLaunching:(NSNotification *)aNotification
  14. {
  15. }
  16. -(BOOL)applicationShouldHandleReopen:(NSApplication*)application
  17. hasVisibleWindows:(BOOL)visibleWindows{
  18. if(!visibleWindows){
  19. [self.windowController.window makeKeyAndOrderFront: nil];
  20. }
  21. return YES;
  22. }
  23. - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication {
  24. return YES;
  25. }
  26. - (void) applicationDidFinishLaunching:(NSNotification *)aNotification {
  27. char buf[16384],userAuthTokenPath[4096];
  28. struct stat systemAuthTokenStat,userAuthTokenStat;
  29. FILE *pf = fopen("/Library/Application Support/ZeroTier/One/zerotier-one.port","r");
  30. long port = 9993; // default
  31. if (pf) {
  32. long n = fread(buf,1,sizeof(buf)-1,pf);
  33. if (n > 0) {
  34. buf[n] = (char)0;
  35. port = strtol(buf,(char **)0,10);
  36. }
  37. fclose(pf);
  38. }
  39. char url[16384];
  40. memset(url,0,sizeof(url));
  41. const char *homeDir = getenv("HOME");
  42. if (homeDir) {
  43. snprintf(userAuthTokenPath,sizeof(userAuthTokenPath),"%s/Library/Application Support/ZeroTier/One/authtoken.secret",homeDir);
  44. bool userAuthTokenOutOfDate = false;
  45. memset(&systemAuthTokenStat,0,sizeof(systemAuthTokenStat));
  46. memset(&userAuthTokenStat,0,sizeof(userAuthTokenStat));
  47. if (stat("/Library/Application Support/ZeroTier/One/authtoken.secret",&systemAuthTokenStat) == 0) {
  48. if (stat(userAuthTokenPath,&userAuthTokenStat) == 0) {
  49. if (userAuthTokenStat.st_mtimespec.tv_sec < systemAuthTokenStat.st_mtimespec.tv_sec)
  50. userAuthTokenOutOfDate = true;
  51. }
  52. }
  53. if (!userAuthTokenOutOfDate) {
  54. pf = fopen(userAuthTokenPath,"r");
  55. if (pf) {
  56. long n = fread(buf,1,sizeof(buf)-1,pf);
  57. if (n > 0) {
  58. buf[n] = (char)0;
  59. snprintf(url,sizeof(url),"http://127.0.0.1:%ld/index.html?authToken=%s",port,buf);
  60. }
  61. fclose(pf);
  62. }
  63. }
  64. }
  65. if (!url[0]) {
  66. // Create authorization reference
  67. OSStatus status;
  68. AuthorizationRef authorizationRef;
  69. // AuthorizationCreate and pass NULL as the initial
  70. // AuthorizationRights set so that the AuthorizationRef gets created
  71. // successfully, and then later call AuthorizationCopyRights to
  72. // determine or extend the allowable rights.
  73. // http://developer.apple.com/qa/qa2001/qa1172.html
  74. status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &authorizationRef);
  75. if (status != errAuthorizationSuccess)
  76. {
  77. NSLog(@"Error Creating Initial Authorization: %d", status);
  78. return;
  79. }
  80. // kAuthorizationRightExecute == "system.privilege.admin"
  81. AuthorizationItem right = {kAuthorizationRightExecute, 0, NULL, 0};
  82. AuthorizationRights rights = {1, &right};
  83. AuthorizationFlags flags = kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed |
  84. kAuthorizationFlagPreAuthorize | kAuthorizationFlagExtendRights;
  85. // Call AuthorizationCopyRights to determine or extend the allowable rights.
  86. status = AuthorizationCopyRights(authorizationRef, &rights, NULL, flags, NULL);
  87. if (status != errAuthorizationSuccess)
  88. {
  89. NSLog(@"Copy Rights Unsuccessful: %d", status);
  90. return;
  91. }
  92. // use rm tool with -rf
  93. char *tool = "/bin/cat";
  94. char *args[] = {"/Library/Application Support/ZeroTier/One/authtoken.secret", NULL};
  95. FILE *pipe = NULL;
  96. status = AuthorizationExecuteWithPrivileges(authorizationRef, tool, kAuthorizationFlagDefaults, args, &pipe);
  97. if (status != errAuthorizationSuccess)
  98. {
  99. NSLog(@"Error: %d", status);
  100. }
  101. if (pipe) {
  102. long n = (long)fread(buf,1,sizeof(buf)-1,pipe);
  103. if (n > 0) {
  104. buf[n] = (char)0;
  105. snprintf(url,sizeof(url),"http://127.0.0.1:%ld/index.html?authToken=%s",port,buf);
  106. if (homeDir) {
  107. snprintf(userAuthTokenPath,sizeof(userAuthTokenPath),"%s/Library/Application Support/ZeroTier",homeDir);
  108. mkdir(userAuthTokenPath,0755);
  109. snprintf(userAuthTokenPath,sizeof(userAuthTokenPath),"%s/Library/Application Support/ZeroTier/One",homeDir);
  110. mkdir(userAuthTokenPath,0755);
  111. snprintf(userAuthTokenPath,sizeof(userAuthTokenPath),"%s/Library/Application Support/ZeroTier/One/authtoken.secret",homeDir);
  112. pf = fopen(userAuthTokenPath,"w");
  113. if (pf) {
  114. fwrite(buf,1,strlen(buf),pf);
  115. fclose(pf);
  116. chmod(userAuthTokenPath,0600);
  117. }
  118. }
  119. }
  120. fclose(pipe);
  121. }
  122. // The only way to guarantee that a credential acquired when you
  123. // request a right is not shared with other authorization instances is
  124. // to destroy the credential. To do so, call the AuthorizationFree
  125. // function with the flag kAuthorizationFlagDestroyRights.
  126. // http://developer.apple.com/documentation/Security/Conceptual/authorization_concepts/02authconcepts/chapter_2_section_7.html
  127. status = AuthorizationFree(authorizationRef, kAuthorizationFlagDestroyRights);
  128. }
  129. NSString *urlStr = [[NSString alloc] initWithCString:url];
  130. self.windowController = [[WindowController alloc] initWithURL: urlStr];
  131. [self.windowController showWindow: [NSApplication sharedApplication].delegate];
  132. self.windowController.contentView.webView.alphaValue = 1.0;
  133. self.windowController.contentView.alphaValue = 1.0;
  134. [self.windowController showWindow:self];
  135. }
  136. @end