Private
Public Access
1
0

Basic authentication support

This commit is contained in:
James Magahern
2019-01-23 20:26:35 -08:00
parent e6314b0f80
commit de852a926d
5 changed files with 133 additions and 3 deletions

View File

@@ -12,20 +12,83 @@
void printUsage()
{
fprintf(stderr, "Usage: kordophoned [-h] [-s | -c (certificate.p12)]\n");
fprintf(stderr, "Usage: kordophoned [-h] [-s | -c (certificate.p12)] [-a (access control file)\n");
fprintf(stderr, "\t-h \t Show this help message\n");
fprintf(stderr, "\t-s \t Use SSL (requires -c option)\n");
fprintf(stderr, "\t-c \t SSL certificate path encoded as pkcs12\n");
fprintf(stderr, "\t-a \t Optional GPG encrypted access control file\n");
}
BOOL acquireCredentials(const char *accessFile, NSString **out_username, NSString **out_password)
{
NSPipe *stdoutPipe = [NSPipe pipe];
NSPipe *stderrPipe = [NSPipe pipe];
NSTask *task = [[NSTask alloc] init];
task.launchPath = @"/usr/local/bin/gpg";
task.arguments = @[ @"-q", @"-d", [NSString stringWithUTF8String:accessFile] ];
task.standardOutput = stdoutPipe;
task.standardError = stderrPipe;
NSError *launchError = nil;
BOOL success = [task launchAndReturnError:&launchError];
[task waitUntilExit];
if (success) {
NSFileHandle *stdoutFile = stdoutPipe.fileHandleForReading;
NSData *data = [stdoutFile readDataToEndOfFile]; // blocks
[stdoutFile closeFile];
if ([task terminationStatus] != 0) {
NSData *stderrData = [[stderrPipe fileHandleForReading] readDataToEndOfFile];
MBIMLogFatal(@"GPG error when decrypting access file: %@", [[NSString alloc] initWithData:stderrData encoding:NSUTF8StringEncoding]);
return NO;
}
NSString *asString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSScanner *scanner = [NSScanner scannerWithString:asString];
BOOL scannerSuccess = NO;
NSString *username = nil;
scannerSuccess = [scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet]
intoString:&username];
if (!scannerSuccess) {
MBIMLogFatal(@"Error parsing username from access file");
return NO;
}
NSString *password = nil;
scannerSuccess = [scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet]
intoString:&password];
if (!scannerSuccess) {
MBIMLogFatal(@"Error parsing password from access file");
return NO;
}
if ([username length] && [password length]) {
*out_username = username;
*out_password = password;
} else {
MBIMLogFatal(@"Error parsing username or password from access file");
return NO;
}
} else {
MBIMLogFatal(@"Unable to launch GPG executable to decrypt access file: %@", [launchError localizedDescription]);
return NO;
}
return YES;
}
int main(int argc, char *const argv[]) {
@autoreleasepool {
BOOL usesSSL = NO;
BOOL showHelp = NO;
BOOL usesAccessControl = NO;
const char *certPath = NULL;
const char *accessFilePath = NULL;
int c = -1;
while ( (c = getopt(argc, argv, "hsc:")) != -1 ) {
while ( (c = getopt(argc, argv, "hsc:a:")) != -1 ) {
switch (c) {
case 's':
usesSSL = YES;
@@ -33,6 +96,10 @@ int main(int argc, char *const argv[]) {
case 'c':
certPath = optarg;
break;
case 'a':
usesAccessControl = YES;
accessFilePath = optarg;
break;
case 'h':
showHelp = YES;
break;
@@ -62,6 +129,33 @@ int main(int argc, char *const argv[]) {
MBIMBridge *bridge = [MBIMBridge sharedInstance];
if (usesAccessControl) {
NSString *username = nil;
NSString *password = nil;
BOOL success = acquireCredentials(accessFilePath, &username, &password);
if (!success) {
MBIMLogInfo(
@"Access file must be a GPG encrypted file (encrypted with your private key, to your pub key) "
"with the follwing format: \n"
"(username)\n"
"(password)"
);
return 1;
} else {
const ssize_t ast_len = 55;
const unichar asterisks[ast_len] = u"*******************************************************";
NSString *obscuredPassword = [NSString stringWithCharacters:asterisks
length:MIN([password length], ast_len)];
MBIMLogNotify(@"Using access control credentials: username(%@) password(%@)", username, obscuredPassword);
bridge.usesAccessControl = YES;
bridge.authUsername = username;
bridge.authPassword = password;
}
}
if (usesSSL && certPath != NULL) {
bridge.usesSSL = YES;
bridge.sslCertPath = [NSString stringWithCString:certPath encoding:NSASCIIStringEncoding];