AuthtokenCopy.m 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #import <Foundation/Foundation.h>
  19. #import "AuthtokenCopy.h"
  20. NSString* getAdminAuthToken(AuthorizationRef authRef) {
  21. char *tool = "/bin/cat";
  22. char *args[] = { "/Library/Application Support/ZeroTier/One/authtoken.secret", NULL};
  23. FILE *pipe = nil;
  24. char token[25];
  25. memset(token, 0, sizeof(char)*25);
  26. OSStatus status = AuthorizationExecuteWithPrivileges(authRef, tool, kAuthorizationFlagDefaults, args, &pipe);
  27. if (status != errAuthorizationSuccess) {
  28. NSLog(@"Reading authtoken failed!");
  29. switch(status) {
  30. case errAuthorizationDenied:
  31. NSLog(@"Autorization Denied");
  32. break;
  33. case errAuthorizationCanceled:
  34. NSLog(@"Authorization Canceled");
  35. break;
  36. case errAuthorizationInternal:
  37. NSLog(@"Authorization Internal");
  38. break;
  39. case errAuthorizationBadAddress:
  40. NSLog(@"Bad Address");
  41. break;
  42. case errAuthorizationInvalidRef:
  43. NSLog(@"Invalid Ref");
  44. break;
  45. case errAuthorizationInvalidSet:
  46. NSLog(@"Invalid Set");
  47. break;
  48. case errAuthorizationInvalidTag:
  49. NSLog(@"Invalid Tag");
  50. break;
  51. case errAuthorizationInvalidFlags:
  52. NSLog(@"Invalid Flags");
  53. break;
  54. case errAuthorizationInvalidPointer:
  55. NSLog(@"Invalid Pointer");
  56. break;
  57. case errAuthorizationToolExecuteFailure:
  58. NSLog(@"Tool Execute Failure");
  59. break;
  60. case errAuthorizationToolEnvironmentError:
  61. NSLog(@"Tool Environment Failure");
  62. break;
  63. case errAuthorizationExternalizeNotAllowed:
  64. NSLog(@"Externalize Not Allowed");
  65. break;
  66. case errAuthorizationInteractionNotAllowed:
  67. NSLog(@"Interaction Not Allowed");
  68. break;
  69. case errAuthorizationInternalizeNotAllowed:
  70. NSLog(@"Internalize Not Allowed");
  71. break;
  72. default:
  73. NSLog(@"Unknown Error");
  74. break;
  75. }
  76. return @"";
  77. }
  78. if(pipe != nil) {
  79. fread(&token, sizeof(char), 24, pipe);
  80. fclose(pipe);
  81. return [NSString stringWithUTF8String:token];
  82. }
  83. return @"";
  84. }