pEpObjCAdapter/PEPObjCAdapter.m
author Dirk Zimmermann <dz@pep.security>
Wed, 09 Oct 2019 09:30:57 +0200
branchIOSAD-150
changeset 1303 b18cc896c191
parent 1231 097a0c418892
child 1304 0f6add4c3cce
permissions -rw-r--r--
IOSAD-150 Change docs.
     1 //
     2 //  pEpObjCAdapter.m
     3 //  pEpObjCAdapter
     4 //
     5 //  Created by Volker Birk on 28.04.15.
     6 //  Copyright (c) 2015 p≡p. All rights reserved.
     7 //
     8 
     9 @import Foundation;
    10 
    11 #import "PEPObjCAdapter.h"
    12 #import "PEPObjCAdapter+Internal.h"
    13 #import "PEPMessageUtil.h"
    14 #import "NSError+PEP.h"
    15 
    16 #import "keymanagement.h"
    17 #import "mime.h"
    18 #import "message.h"
    19 
    20 const PEP_decrypt_flags PEP_decrypt_flag_none = 0x0;
    21 
    22 #if TARGET_OS_IPHONE
    23 // marked for iOS to think about what we want on macOS
    24 const char* _Nullable perMachineDirectory = NULL;
    25 #endif
    26 
    27 NSURL *s_homeURL;
    28 
    29 static BOOL s_unEncryptedSubjectEnabled = NO;
    30 static BOOL s_passiveModeEnabled = NO;
    31 
    32 @implementation PEPObjCAdapter
    33 
    34 #pragma mark - SUBJECT PROTECTION
    35 
    36 + (BOOL)unEncryptedSubjectEnabled;
    37 {
    38     return s_unEncryptedSubjectEnabled;
    39 }
    40 
    41 + (void)setUnEncryptedSubjectEnabled:(BOOL)enabled;
    42 {
    43     s_unEncryptedSubjectEnabled = enabled;
    44 }
    45 
    46 #pragma mark - Passive Mode
    47 
    48 + (BOOL)passiveModeEnabled
    49 {
    50     return s_passiveModeEnabled;
    51 }
    52 
    53 + (void)setPassiveModeEnabled:(BOOL)enabled
    54 {
    55     s_passiveModeEnabled = enabled;
    56 }
    57 
    58 #pragma mark - DB PATHS
    59 
    60 + (void)initialize
    61 {
    62     s_homeURL = [self createApplicationDirectory];
    63 
    64     // The engine will put its per_user_directory under this directory.
    65     setenv("HOME", [[s_homeURL path] cStringUsingEncoding:NSUTF8StringEncoding], 1);
    66 
    67     // This sets the engine's per_machine_directory under iOS.
    68     [self setPerMachineDirectory:s_homeURL];
    69 }
    70 
    71 + (NSURL *)homeURL
    72 {
    73     return s_homeURL;
    74 }
    75 
    76 /**
    77  Looks up the shared directory for pEp apps under iOS and makes sure it exists.
    78 
    79  Derived settings:
    80 
    81  * $HOME (the engine uses that).
    82  * The engine's per_user_directory (which is placed under $HOME).
    83  * The engine's per_machine_directory (see [PEPObjCAdapter setPerMachineDirectory:]).
    84 
    85  @return A URL pointing to as app-specific directory under the OS defined
    86          application support directory for the current user.
    87  */
    88 + (NSURL *)createApplicationDirectory
    89 {
    90     NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
    91     if (!bundleID) {
    92         // This can happen in unit tests
    93         bundleID = @"test";
    94     }
    95     NSFileManager *fm = [NSFileManager defaultManager];
    96     NSURL *dirPath = nil;
    97     
    98     // Find the application support directory in the home directory.
    99     NSArray *appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory
   100                                         inDomains:NSUserDomainMask];
   101     if ([appSupportDir count] > 0)
   102     {
   103         // Append the bundle ID to the URL for the
   104         // Application Support directory.
   105         // Mainly needed for OS X, but doesn't do any harm on iOS
   106         dirPath = [[appSupportDir objectAtIndex:0] URLByAppendingPathComponent:bundleID];
   107         
   108         // If the directory does not exist, this method creates it.
   109         // This method is only available in OS X v10.7 and iOS 5.0 or later.
   110         NSError *theError = nil;
   111         if (![fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES
   112                            attributes:nil error:&theError])
   113         {
   114             // Handle the error.
   115             return nil;
   116         }
   117     }
   118     
   119     return dirPath;
   120 }
   121 
   122 /**
   123  Sets the directory that will be fed into the engine's per_machine_directory.
   124 
   125  Does not handle macOS. For macOS, either PER_MACHINE_DIRECTORY has to be defined
   126  (if constant), or this method has to be extended to handle it.
   127 
   128  @param perMachineDir The url to use as the per_machine_directory directory.
   129  */
   130 + (void)setPerMachineDirectory:(NSURL *)perMachineDir
   131 {
   132 #if TARGET_OS_IPHONE
   133     if (perMachineDirectory) {
   134         free((void *) perMachineDirectory);
   135     }
   136     perMachineDirectory = strdup([perMachineDir path].UTF8String);
   137 #endif
   138 }
   139 
   140 + (NSString *)getBundlePathFor: (NSString *) filename
   141 {
   142     return nil;
   143 }
   144 
   145 + (void)copyAssetsIntoDocumentsDirectory:(NSBundle *)rootBundle
   146                                     bundleName:(NSString *)bundleName
   147                                       fileName:(NSString *)fileName {
   148 
   149     NSString *systemDir = [NSString stringWithUTF8String:perMachineDirectory];
   150     
   151     if(!(systemDir && bundleName && fileName))
   152         return;
   153     
   154     // Check if the database file exists in the documents directory.
   155     NSString *destinationPath = [systemDir stringByAppendingPathComponent:fileName];
   156     if (![[NSFileManager defaultManager] fileExistsAtPath:destinationPath]) {
   157         // The file does not exist in the documents directory, so copy it from bundle now.
   158         NSBundle *bundleObj = [NSBundle bundleWithPath:
   159                                [[rootBundle resourcePath]
   160                                 stringByAppendingPathComponent: bundleName]];
   161         if (!bundleObj)
   162             return;
   163         
   164         NSString *sourcePath =[[bundleObj resourcePath] stringByAppendingPathComponent: fileName];
   165         
   166         NSError *error;
   167         [[NSFileManager defaultManager]
   168          copyItemAtPath:sourcePath toPath:destinationPath error:&error];
   169         
   170         // Check if any error occurred during copying and display it.
   171         if (error != nil) {
   172             NSLog(@"%@", [error localizedDescription]);
   173         }
   174     }
   175 }
   176 
   177 + (void)setupTrustWordsDB:(NSBundle *)rootBundle {
   178 // iOS to force us to think about macOS
   179 #if TARGET_OS_IPHONE
   180     [PEPObjCAdapter copyAssetsIntoDocumentsDirectory:rootBundle
   181                                           bundleName:@"pEpTrustWords.bundle"
   182                                             fileName:@"system.db"];
   183 
   184 #endif
   185 }
   186 
   187 + (void)setupTrustWordsDB
   188 {
   189     [PEPObjCAdapter setupTrustWordsDB:[NSBundle mainBundle]];
   190 }
   191 
   192 
   193 + (NSString * _Nonnull)perUserDirectoryString
   194 {
   195     return [NSString stringWithCString:per_user_directory() encoding:NSUTF8StringEncoding];
   196 }
   197 
   198 + (NSString * _Nonnull)perMachineDirectoryString
   199 {
   200     return [NSString stringWithCString:per_machine_directory() encoding:NSUTF8StringEncoding];
   201 }
   202 
   203 @end