pEpObjCAdapter/PEPObjCAdapter.m
author Dirk Zimmermann <dz@pep.security>
Thu, 05 Sep 2019 13:43:24 +0200
branchIOS-1784
changeset 1215 9a76f6d64677
parent 1211 7bf9f33c3cf6
child 1216 9b09bbf24a3e
permissions -rw-r--r--
IOS-1781 Public adapter API: perUserDirectory, perMachineDirectory.
dirk@187
     1
//
edouard@191
     2
//  pEpObjCAdapter.m
edouard@191
     3
//  pEpObjCAdapter
dirk@187
     4
//
dirk@187
     5
//  Created by Volker Birk on 28.04.15.
dirk@187
     6
//  Copyright (c) 2015 p≡p. All rights reserved.
dirk@187
     7
//
dirk@187
     8
dirk@187
     9
@import Foundation;
dirk@187
    10
dirk@187
    11
#import "PEPObjCAdapter.h"
dirk@187
    12
#import "PEPObjCAdapter+Internal.h"
dirk@367
    13
#import "PEPMessageUtil.h"
dirk@484
    14
#import "NSError+PEP.h"
dirk@484
    15
dirk@484
    16
#import "keymanagement.h"
dirk@484
    17
#import "mime.h"
dirk@484
    18
#import "message.h"
dirk@187
    19
andreas@635
    20
const PEP_decrypt_flags PEP_decrypt_flag_none = 0x0;
andreas@635
    21
andreas@1173
    22
#if TARGET_OS_IPHONE
andreas@1175
    23
// marked for iOS to think about what we want on macOS
andreas@1173
    24
const char* _Nullable perMachineDirectory = NULL;
andreas@1173
    25
#endif
andreas@1173
    26
dirk@187
    27
NSURL *s_homeURL;
dirk@625
    28
dirk@601
    29
static BOOL s_unEncryptedSubjectEnabled = NO;
dirk@625
    30
static BOOL s_passiveModeEnabled = NO;
dirk@187
    31
dirk@187
    32
@implementation PEPObjCAdapter
dirk@187
    33
andreas@416
    34
#pragma mark - SUBJECT PROTECTION
andreas@416
    35
dirk@601
    36
+ (BOOL)unEncryptedSubjectEnabled;
andreas@416
    37
{
dirk@601
    38
    return s_unEncryptedSubjectEnabled;
andreas@416
    39
}
andreas@416
    40
dirk@601
    41
+ (void)setUnEncryptedSubjectEnabled:(BOOL)enabled;
andreas@416
    42
{
dirk@601
    43
    s_unEncryptedSubjectEnabled = enabled;
andreas@416
    44
}
andreas@416
    45
dirk@625
    46
#pragma mark - Passive Mode
dirk@625
    47
dirk@625
    48
+ (BOOL)passiveModeEnabled
dirk@625
    49
{
dirk@625
    50
    return s_passiveModeEnabled;
dirk@625
    51
}
dirk@625
    52
dirk@625
    53
+ (void)setPassiveModeEnabled:(BOOL)enabled
dirk@625
    54
{
dirk@625
    55
    s_passiveModeEnabled = enabled;
dirk@625
    56
}
dirk@625
    57
andreas@416
    58
#pragma mark - DB PATHS
andreas@416
    59
dirk@187
    60
+ (void)initialize
dirk@187
    61
{
dirk@187
    62
    s_homeURL = [self createApplicationDirectory];
dz@1211
    63
    [self setPerMachineDirectory:s_homeURL]; // Important, defines $HOME for the engine
dirk@187
    64
}
dirk@187
    65
dirk@187
    66
+ (NSURL *)homeURL
dirk@187
    67
{
dirk@187
    68
    return s_homeURL;
dirk@187
    69
}
dirk@187
    70
dirk@187
    71
+ (NSURL *)createApplicationDirectory
dirk@187
    72
{
dirk@187
    73
    NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
dirk@187
    74
    if (!bundleID) {
dirk@187
    75
        // This can happen in unit tests
dirk@187
    76
        bundleID = @"test";
dirk@187
    77
    }
dirk@187
    78
    NSFileManager *fm = [NSFileManager defaultManager];
dirk@187
    79
    NSURL *dirPath = nil;
andreas@268
    80
    
dirk@187
    81
    // Find the application support directory in the home directory.
dirk@187
    82
    NSArray *appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory
dirk@187
    83
                                        inDomains:NSUserDomainMask];
dirk@187
    84
    if ([appSupportDir count] > 0)
dirk@187
    85
    {
dirk@187
    86
        // Append the bundle ID to the URL for the
dirk@187
    87
        // Application Support directory.
dirk@187
    88
        // Mainly needed for OS X, but doesn't do any harm on iOS
dirk@187
    89
        dirPath = [[appSupportDir objectAtIndex:0] URLByAppendingPathComponent:bundleID];
andreas@268
    90
        
dirk@187
    91
        // If the directory does not exist, this method creates it.
dirk@187
    92
        // This method is only available in OS X v10.7 and iOS 5.0 or later.
dirk@187
    93
        NSError *theError = nil;
dirk@187
    94
        if (![fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES
dirk@187
    95
                           attributes:nil error:&theError])
dirk@187
    96
        {
dirk@187
    97
            // Handle the error.
dirk@187
    98
            return nil;
dirk@187
    99
        }
dirk@187
   100
    }
andreas@268
   101
    
dirk@187
   102
    return dirPath;
dirk@187
   103
}
dirk@187
   104
andreas@1175
   105
+ (void)setPerMachineDirectory:(NSURL *)homeDir
dirk@187
   106
{
andreas@1173
   107
#if TARGET_OS_IPHONE
dz@1211
   108
    // Set HOME, which is also important for the perUser directory later
dz@1211
   109
    setenv("HOME", [[homeDir path] cStringUsingEncoding:NSUTF8StringEncoding], 1);
dz@1211
   110
andreas@1175
   111
    if (perMachineDirectory) {
andreas@1175
   112
        free((void *) perMachineDirectory);
andreas@1173
   113
    }
andreas@1175
   114
    perMachineDirectory = strdup([homeDir path].UTF8String);
andreas@1173
   115
#endif
dirk@187
   116
}
dirk@187
   117
andreas@276
   118
+ (NSString *)getBundlePathFor: (NSString *) filename
dirk@187
   119
{
dirk@187
   120
    return nil;
dirk@187
   121
}
dirk@187
   122
andreas@1175
   123
+ (void)copyAssetsIntoDocumentsDirectory:(NSBundle *)rootBundle
dirk@442
   124
                                    bundleName:(NSString *)bundleName
dirk@442
   125
                                      fileName:(NSString *)fileName {
andreas@1175
   126
andreas@1175
   127
    NSString *systemDir = [NSString stringWithUTF8String:perMachineDirectory];
andreas@268
   128
    
andreas@1175
   129
    if(!(systemDir && bundleName && fileName))
andreas@1175
   130
        return;
dirk@187
   131
    
dirk@187
   132
    // Check if the database file exists in the documents directory.
andreas@1175
   133
    NSString *destinationPath = [systemDir stringByAppendingPathComponent:fileName];
dirk@187
   134
    if (![[NSFileManager defaultManager] fileExistsAtPath:destinationPath]) {
dirk@187
   135
        // The file does not exist in the documents directory, so copy it from bundle now.
dirk@187
   136
        NSBundle *bundleObj = [NSBundle bundleWithPath:
dirk@187
   137
                               [[rootBundle resourcePath]
dirk@187
   138
                                stringByAppendingPathComponent: bundleName]];
dirk@187
   139
        if (!bundleObj)
andreas@1175
   140
            return;
dirk@187
   141
        
dirk@187
   142
        NSString *sourcePath =[[bundleObj resourcePath] stringByAppendingPathComponent: fileName];
dirk@187
   143
        
dirk@187
   144
        NSError *error;
dirk@187
   145
        [[NSFileManager defaultManager]
dirk@187
   146
         copyItemAtPath:sourcePath toPath:destinationPath error:&error];
dirk@187
   147
        
dirk@187
   148
        // Check if any error occurred during copying and display it.
dirk@187
   149
        if (error != nil) {
dirk@187
   150
            NSLog(@"%@", [error localizedDescription]);
dirk@187
   151
        }
dirk@187
   152
    }
dirk@187
   153
}
dirk@187
   154
andreas@1173
   155
+ (void)setupTrustWordsDB:(NSBundle *)rootBundle {
andreas@1175
   156
// iOS to force us to think about macOS
andreas@1173
   157
#if TARGET_OS_IPHONE
andreas@1175
   158
    [PEPObjCAdapter copyAssetsIntoDocumentsDirectory:rootBundle
andreas@1175
   159
                                          bundleName:@"pEpTrustWords.bundle"
andreas@1175
   160
                                            fileName:@"system.db"];
andreas@1175
   161
andreas@1173
   162
#endif
dirk@187
   163
}
dirk@187
   164
dirk@187
   165
+ (void)setupTrustWordsDB
dirk@187
   166
{
dirk@187
   167
    [PEPObjCAdapter setupTrustWordsDB:[NSBundle mainBundle]];
dirk@187
   168
}
dirk@187
   169
dz@1215
   170
dz@1215
   171
+ (NSString * _Nonnull)perUserDirectory
dz@1215
   172
{
dz@1215
   173
    return [NSString stringWithCString:per_user_directory() encoding:NSUTF8StringEncoding];
dz@1215
   174
}
dz@1215
   175
dz@1215
   176
+ (NSString * _Nonnull)perMachineDirectory
dz@1215
   177
{
dz@1215
   178
    return [NSString stringWithCString:per_machine_directory() encoding:NSUTF8StringEncoding];
dz@1215
   179
}
dz@1215
   180
dirk@187
   181
@end