forked from DanTheMan827/ios-app-signer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProvisioningProfile.swift
133 lines (118 loc) · 6.18 KB
/
ProvisioningProfile.swift
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
//
// provisioningProfile.swift
// AppSigner
//
// Created by Daniel Radtke on 11/4/15.
// Copyright © 2015 Daniel Radtke. All rights reserved.
//
import Foundation
import AppKit
struct ProvisioningProfile {
var filename: String,
name: String,
created:Date,
expires: Date,
appID: String,
teamID: String,
rawXML: String,
entitlements: AnyObject?
fileprivate let delegate = NSApplication.shared.delegate as! AppDelegate
static func getProfiles() -> [ProvisioningProfile] {
var output: [ProvisioningProfile] = []
let fileManager = FileManager()
if let libraryDirectory = fileManager.urls(for: .libraryDirectory, in: .userDomainMask).first {
let provisioningProfilesPath = libraryDirectory.path.stringByAppendingPathComponent("MobileDevice/Provisioning Profiles") as NSString
if let provisioningProfiles = try? fileManager.contentsOfDirectory(atPath: provisioningProfilesPath as String) {
for provFile in provisioningProfiles {
if provFile.pathExtension == "mobileprovision" {
let profileFilename = provisioningProfilesPath.appendingPathComponent(provFile)
if let profile = ProvisioningProfile(filename: profileFilename) {
output.append(profile)
}
}
}
}
}
// distinct
output = output.sorted(by: {
$0.created.timeIntervalSince1970 > $1.created.timeIntervalSince1970
})
var newProfiles = [ProvisioningProfile]()
var names = [String]()
for profile in output {
if !names.contains("\(profile.name)\(profile.appID)") {
newProfiles.append(profile)
names.append("\(profile.name)\(profile.appID)")
NSLog("\(profile.name), \(profile.created)")
}
}
return newProfiles;
}
init?(filename: String, skipGetTaskAllow: Bool = false){
let securityArgs = ["cms","-D","-i", filename]
let taskOutput = Process().execute("/usr/bin/security", workingDirectory: nil, arguments: securityArgs)
if taskOutput.status == 0 {
if let xmlIndex = taskOutput.output.range(of: "<?xml") {
self.rawXML = taskOutput.output.substring(from: xmlIndex.lowerBound)
} else {
Log.write("Unable to find xml start tag in profile")
self.rawXML = taskOutput.output
}
if skipGetTaskAllow {
Log.write("Skipping get-task-allow entitlement...");
if let results = try? PropertyListSerialization.propertyList(from: self.rawXML.data(using: String.Encoding.utf8)!, options: PropertyListSerialization.MutabilityOptions(), format: nil) {
var resultsdict = results as! Dictionary<String, AnyObject>
var entitlements = resultsdict["Entitlements"] as! Dictionary<String, AnyObject>
entitlements.removeValue(forKey: "get-task-allow")
resultsdict["Entitlements"] = entitlements as AnyObject
let data = PropertyListSerialization.dataFromPropertyList(resultsdict, format: PropertyListSerialization.PropertyListFormat.xml, errorDescription: nil)!
self.rawXML = String(data: data, encoding: .utf8)!
Log.write("Skipped get-task-allow entitlement!");
}
}
if let results = try? PropertyListSerialization.propertyList(from: self.rawXML.data(using: String.Encoding.utf8)!, options: PropertyListSerialization.MutabilityOptions(), format: nil) {
if let expirationDate = (results as AnyObject).value(forKey: "ExpirationDate") as? Date,
let creationDate = (results as AnyObject).value(forKey: "CreationDate") as? Date,
let name = (results as AnyObject).value(forKey: "Name") as? String,
let entitlements = (results as AnyObject).value(forKey: "Entitlements"),
let applicationIdentifier = (entitlements as AnyObject).value(forKey: "application-identifier") as? String,
let periodIndex = applicationIdentifier.firstIndex(of: ".") {
self.filename = filename
self.expires = expirationDate
self.created = creationDate
self.appID = applicationIdentifier.substring(from: applicationIdentifier.index(periodIndex, offsetBy: 1))
self.teamID = applicationIdentifier.substring(to: periodIndex)
self.name = name
self.entitlements = entitlements as AnyObject?
} else {
Log.write("Error processing \(filename.lastPathComponent)")
return nil
}
} else {
Log.write("Error parsing \(filename.lastPathComponent)")
return nil
}
} else {
Log.write("Error reading \(filename.lastPathComponent)")
return nil
}
}
func getEntitlementsPlist(_ tempFolder: String) -> NSString? {
let mobileProvisionPlist = tempFolder.stringByAppendingPathComponent("mobileprovision.plist")
do {
try self.rawXML.write(toFile: mobileProvisionPlist, atomically: false, encoding: String.Encoding.utf8)
let plistBuddy = Process().execute("/usr/libexec/PlistBuddy", workingDirectory: nil, arguments: ["-c", "Print :Entitlements",mobileProvisionPlist, "-x"])
if plistBuddy.status == 0 {
return plistBuddy.output as NSString?
} else {
Log.write("PlistBuddy Failed")
Log.write(plistBuddy.output)
return nil
}
} catch let error as NSError {
Log.write("Error writing mobileprovision.plist")
Log.write(error.localizedDescription)
return nil
}
}
}