Private
Public Access
1
0
Files
Kordophone/kordophone/main.m

174 lines
6.1 KiB
Mathematica
Raw Normal View History

//
// main.m
// kordophone
//
// Created by James Magahern on 11/12/18.
// Copyright © 2018 James Magahern. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "MBIMBridge.h"
2019-01-22 23:31:36 -08:00
void printUsage()
{
2019-01-23 20:26:35 -08:00
fprintf(stderr, "Usage: kordophoned [-h] [-s | -c (certificate.p12)] [-a (access control file)\n");
2019-01-22 23:31:36 -08:00
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");
2019-01-23 20:26:35 -08:00
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;
2019-01-22 23:31:36 -08:00
}
int main(int argc, char *const argv[]) {
@autoreleasepool {
2019-01-22 23:31:36 -08:00
BOOL usesSSL = NO;
BOOL showHelp = NO;
2019-01-23 20:26:35 -08:00
BOOL usesAccessControl = NO;
2019-01-22 23:31:36 -08:00
const char *certPath = NULL;
2019-01-23 20:26:35 -08:00
const char *accessFilePath = NULL;
2018-11-13 22:39:03 -08:00
2019-01-22 23:31:36 -08:00
int c = -1;
2019-01-23 20:26:35 -08:00
while ( (c = getopt(argc, argv, "hsc:a:")) != -1 ) {
2019-01-22 23:31:36 -08:00
switch (c) {
case 's':
usesSSL = YES;
break;
case 'c':
certPath = optarg;
break;
2019-01-23 20:26:35 -08:00
case 'a':
usesAccessControl = YES;
accessFilePath = optarg;
break;
2019-01-22 23:31:36 -08:00
case 'h':
showHelp = YES;
break;
case '?':
if (optopt == 'c') {
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
} else if (isprint(optopt)) {
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
} else {
fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
return 1;
}
default:
abort ();
}
}
if (showHelp) {
printUsage();
2018-11-13 22:39:03 -08:00
return 1;
}
2019-01-22 23:31:36 -08:00
if (usesSSL && certPath == NULL) {
fprintf(stderr, "Error: wants SSL (-s) but no ssl certificate path (-c) provided\n");
return 1;
}
MBIMBridge *bridge = [MBIMBridge sharedInstance];
2019-01-23 20:26:35 -08:00
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;
}
}
2019-01-22 23:31:36 -08:00
if (usesSSL && certPath != NULL) {
bridge.usesSSL = YES;
bridge.sslCertPath = [NSString stringWithCString:certPath encoding:NSASCIIStringEncoding];
}
2018-11-13 22:39:03 -08:00
[bridge connect];
BOOL running = YES;
while (running) {
[[NSRunLoop currentRunLoop] run];
}
}
2019-01-22 23:31:36 -08:00
return 0;
}