-
Notifications
You must be signed in to change notification settings - Fork 4
/
GPGEngine.m
403 lines (322 loc) · 14.2 KB
/
GPGEngine.m
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//
// GPGEngine.m
// MacGPGME
//
// Created by davelopper at users.sourceforge.net on Tue Aug 14 2001.
//
//
// Copyright (C) 2001-2006 Mac GPG Project.
//
// This code is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 2.1 of the License, or (at your option)
// any later version.
//
// This code is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, visit <http://www.gnu.org/> or write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.
//
// More info at <http://macgpg.sourceforge.net/>
//
#include <MacGPGME/GPGEngine.h>
#include <MacGPGME/GPGObject.h>
#include <MacGPGME/GPGPrettyInfo.h>
#include <MacGPGME/GPGContext.h>
#include <MacGPGME/GPGInternals.h>
#include <MacGPGME/GPGOptions.h>
#include <Foundation/Foundation.h>
#include <gpgme.h>
#include "GPGEngineHelper.h"
#define _engine ((gpgme_engine_info_t)_internalRepresentation)
#define INVALID_CONTEXT ((GPGContext *)-1)
@interface GPGEngine(Private)
+ (NSString *) executablePathDefaultsKeyForProtocol:(GPGProtocol)protocol;
@end
@implementation GPGEngine
+ (BOOL) needsPointerUniquing
{
return YES;
}
+ (GPGError) checkVersionForProtocol:(GPGProtocol)protocol
{
return gpgme_engine_check_version(protocol);
}
+ (NSString *) checkFrameworkVersion:(NSString *)requiredVersion
{
const char *aCString;
aCString = gpgme_check_version(requiredVersion == nil ? NULL:[requiredVersion UTF8String]); // statically allocated string or NULL
return GPGStringFromChars(aCString);
}
+ (NSArray *) availableEngines
{
gpgme_engine_info_t anEngine = NULL;
gpgme_error_t anError = gpgme_get_engine_info(&anEngine); // The memory for the info structures is allocated the first time this function is invoked, and must not be freed by the caller.
if(anError != GPG_ERR_NO_ERROR)
[[NSException exceptionWithGPGError:anError userInfo:nil] raise];
return [self enginesFromEngineInfo:anEngine context:nil];
}
+ (GPGEngine *) engineForProtocol:(GPGProtocol)protocol
{
NSEnumerator *engineEnum = [[self availableEngines] objectEnumerator];
GPGEngine *anEngine;
while((anEngine = [engineEnum nextObject]) != nil)
if([anEngine engineProtocol] == protocol)
return anEngine;
return nil;
}
+ (NSString *) defaultHomeDirectoryForProtocol:(GPGProtocol)protocol
{
static NSMutableDictionary *defaultHomeDirectoryPerProtocol = nil;
NSNumber *aProtocol = [NSNumber numberWithInt:protocol];
if(protocol != GPGOpenPGPProtocol)
[[NSException exceptionWithGPGError:gpgme_err_make(GPG_MacGPGMEFrameworkErrorSource, GPGErrorNotImplemented) userInfo:nil] raise];
if(defaultHomeDirectoryPerProtocol == nil){
NSString *gnupgHome = [[[NSProcessInfo processInfo] environment] objectForKey:@"GNUPGHOME"];
NSString *defaultHomeDirectory;
defaultHomeDirectoryPerProtocol = [[NSMutableDictionary alloc] initWithCapacity:2];
if(gnupgHome != nil)
defaultHomeDirectory = gnupgHome;
else
defaultHomeDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@".gnupg"];
[defaultHomeDirectoryPerProtocol setObject:defaultHomeDirectory forKey:aProtocol];
}
return [defaultHomeDirectoryPerProtocol objectForKey:aProtocol];
}
+ (void) setDefaultExecutablePath:(NSString *)path forProtocol:(GPGProtocol)protocol
{
if(protocol != GPGOpenPGPProtocol)
[[NSException exceptionWithGPGError:gpgme_err_make(GPG_MacGPGMEFrameworkErrorSource, GPGErrorNotImplemented) userInfo:nil] raise];
[GPGOptions setDefaultValue:path forKey:GPGOpenPGPExecutablePathKey];
}
+ (NSString *) _defaultExecutablePathForProtocol:(GPGProtocol)protocol
{
NSString *exePath = [self executablePathDefaultsKeyForProtocol:protocol];
if (!exePath) {
return nil;
}
NSString *aPath = [[NSUserDefaults standardUserDefaults] stringForKey:[self executablePathDefaultsKeyForProtocol:protocol]];
return aPath;
}
- (GPGProtocol) engineProtocol
{
NSAssert(_context != INVALID_CONTEXT, @"### GPGEngine instance was associated to a GPGContext that has been freed.");
return _engine->protocol;
}
- (NSString *) executablePath
{
NSAssert(_context != INVALID_CONTEXT, @"### GPGEngine instance was associated to a GPGContext that has been freed.");
return GPGStringFromChars(_engine->file_name);
}
- (void) setExecutablePath:(NSString *)executablePath
{
NSParameterAssert(executablePath != nil);
if(![[self executablePath] isEqualToString:executablePath]){
const char *aCString = [executablePath fileSystemRepresentation];
gpgme_error_t anError;
// Different implementation when default or context's
if(_context != nil)
anError = gpgme_ctx_set_engine_info([_context gpgmeContext], [self engineProtocol], aCString, _engine->home_dir); // Will duplicate strings
else
anError = gpgme_set_engine_info([self engineProtocol], aCString, _engine->home_dir);
if(anError != GPGErrorNoError)
[[NSException exceptionWithGPGError:anError userInfo:(_context != nil ? [NSDictionary dictionaryWithObject:_context forKey:GPGContextKey] : nil)] raise];
if(_context != nil){
// Previous gpgme_engine_info_t struct is now invalid; we need to retrieve new one
[self reloadContextEngineInfo];
}
// Calling gpgme_(ctx_)set_engine_info() will retrieve engine version. If NULL, engine
// has not been found or is invalid.
if([self version] == nil)
[[NSException exceptionWithGPGError:gpgme_err_make(GPG_MacGPGMEFrameworkErrorSource, GPGErrorInvalidEngine) userInfo:nil] raise];
}
}
- (NSString *) version
{
const char *aCString;
NSAssert(_context != INVALID_CONTEXT, @"### GPGEngine instance was associated to a GPGContext that has been freed.");
aCString = _engine->version;
return GPGStringFromChars(aCString);
}
- (NSString *) requestedVersion
{
const char *aCString;
NSAssert(_context != INVALID_CONTEXT, @"### GPGEngine instance was associated to a GPGContext that has been freed.");
aCString = _engine->req_version;
return GPGStringFromChars(aCString);
}
- (NSString *) homeDirectory
{
NSString *homeDirectory = [self customHomeDirectory];
return (homeDirectory != nil ? homeDirectory : [[self class] defaultHomeDirectoryForProtocol:[self engineProtocol]]);
}
- (NSString *) customHomeDirectory
{
const char *aCString;
NSAssert(_context != INVALID_CONTEXT, @"### GPGEngine instance was associated to a GPGContext that has been freed.");
aCString = _engine->home_dir;
return GPGStringFromChars(aCString);
}
- (void) setCustomHomeDirectory:(NSString *)homeDirectory
{
NSString *myHomeDirectory = [self customHomeDirectory];
if(myHomeDirectory != homeDirectory && ![myHomeDirectory isEqualToString:homeDirectory]){
const char *aCString = [homeDirectory fileSystemRepresentation];
gpgme_error_t anError;
const char *aPathCString = [[self executablePath] fileSystemRepresentation]; // Due to a bug in gpgme 1.1, we cannot pass the same pointer already in engine; we need to pass a copy of it. Reported to gpgme people.
// different implementation when default or context's
if(_context)
anError = gpgme_ctx_set_engine_info([_context gpgmeContext], [self engineProtocol], aPathCString, aCString); // Will duplicate strings
else
anError = gpgme_set_engine_info([self engineProtocol], aPathCString, aCString); // Will duplicate strings
if(anError != GPGErrorNoError)
[[NSException exceptionWithGPGError:anError userInfo:(_context != nil ? [NSDictionary dictionaryWithObject:_context forKey:GPGContextKey] : nil)] raise];
if(_context != nil){
// Previous gpgme_engine_info_t struct is now invalid; we need to retrieve new one
[self reloadContextEngineInfo];
}
}
}
- (NSString *) debugDescription
{
if(_context == INVALID_CONTEXT)
return [NSString stringWithFormat:@"<%@ %p> [freed context]", NSStringFromClass([self class]), self];
else
return [NSString stringWithFormat:@"<%@ %p> %@ (min. %@), %@ (%@), %@ - %@", NSStringFromClass([self class]), self, GPGProtocolDescription([self engineProtocol]), [self requestedVersion], [self executablePath], [self version], [self homeDirectory], (_context != nil ? [_context description] : @"global")];
}
+ (NSString *) executablePathDefaultsKeyForProtocol:(GPGProtocol)protocol
{
switch(protocol){
case GPGOpenPGPProtocol:
return GPGOpenPGPExecutablePathKey;
case GPGCMSProtocol:
default:
return nil;
}
}
- (NSString *) executablePathDefaultsKey
{
NSString *aKey = [[self class] executablePathDefaultsKeyForProtocol:[self engineProtocol]];
if(aKey == nil)
[[NSException exceptionWithGPGError:gpgme_err_make(GPG_MacGPGMEFrameworkErrorSource, GPGErrorNotImplemented) userInfo:nil] raise];
return aKey;
}
- (BOOL) userHasSelectedExecutablePath
{
return [[NSUserDefaults standardUserDefaults] stringForKey:[self executablePathDefaultsKey]] != nil;
}
- (NSArray *) knownExecutablePaths
{
switch([self engineProtocol]){
case GPGOpenPGPProtocol:
return [NSArray arrayWithObjects:@"/usr/local/MacGPG2/bin/gpg2", @"/usr/local/bin/gpg2", @"/opt/local/bin/gpg2", @"/sw/bin/gpg2", @"/usr/local/bin/gpg", @"/opt/local/bin/gpg", @"/sw/bin/gpg", nil];
case GPGCMSProtocol:
default:
[[NSException exceptionWithGPGError:gpgme_err_make(GPG_MacGPGMEFrameworkErrorSource, GPGErrorNotImplemented) userInfo:nil] raise];
}
return nil;
}
- (NSArray *) availableExecutablePaths
{
switch([self engineProtocol]){
case GPGOpenPGPProtocol:{
NSFileManager *fm = [NSFileManager defaultManager];
NSEnumerator *pathEnum = [[self knownExecutablePaths] objectEnumerator];
NSMutableArray *validPaths = [NSMutableArray array];
NSString *eachPath;
while(eachPath = [pathEnum nextObject]){
eachPath = [eachPath stringByResolvingSymlinksInPath];
if(![validPaths containsObject:eachPath] && [fm isExecutableFileAtPath:eachPath])
[validPaths addObject:eachPath];
}
return validPaths;
}
case GPGCMSProtocol:
default:
[[NSException exceptionWithGPGError:gpgme_err_make(GPG_MacGPGMEFrameworkErrorSource, GPGErrorNotImplemented) userInfo:nil] raise];
}
return nil;
}
- (NSString *) executeWithArguments:(NSArray *)arguments localizedOutput:(BOOL)localizedOutput error:(NSError **)errorPtr
{
return [GPGEngineHelper executeEngine:self withArguments:arguments localizedOutput:localizedOutput error:errorPtr];
}
- (NSString *) optionsFilename
{
switch([self engineProtocol]){
case GPGOpenPGPProtocol:{
NSString *aVersion = [self version];
if(aVersion != nil && [aVersion rangeOfString:@"1.0."].length > 0)
return [[self homeDirectory] stringByAppendingPathComponent:@"options"];
else
return [[self homeDirectory] stringByAppendingPathComponent:@"gpg.conf"];
}
case GPGCMSProtocol:
default:
[[NSException exceptionWithGPGError:gpgme_err_make(GPG_MacGPGMEFrameworkErrorSource, GPGErrorNotImplemented) userInfo:nil] raise];
return nil; // Never reached
}
}
- (NSString *) extensionsPath
{
NSString *extensionsPath = nil;
if(![self engineProtocol] == GPGOpenPGPProtocol)
[[NSException exceptionWithGPGError:gpgme_err_make(GPG_MacGPGMEFrameworkErrorSource, GPGErrorNotImplemented) userInfo:nil] raise];
extensionsPath = [[[[self executablePath] stringByDeletingLastPathComponent] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"lib/gnupg"]; // E.g. from /usr/local/bin/gpg to /usr/local/lib/gnupg
return extensionsPath;
}
@end
@implementation GPGEngine(GPGInternals)
+ (NSArray *) enginesFromEngineInfo:(gpgme_engine_info_t)engineInfo context:(GPGContext *)context
{
NSMutableArray *engines = [NSMutableArray arrayWithCapacity:2];
while(engineInfo != NULL){
GPGEngine *newEngine = [[GPGEngine alloc] initWithInternalRepresentation:engineInfo];
NSString *defaultExecutablePath;
[engines addObject:newEngine];
[newEngine setContext:context];
defaultExecutablePath = [GPGEngine _defaultExecutablePathForProtocol:[newEngine engineProtocol]];
if(defaultExecutablePath != nil){
NS_DURING
[newEngine setExecutablePath:defaultExecutablePath];
NS_HANDLER
// Ignore error and log it
NSLog(@"No valid gpg engine at '%@'; you need to change default engine path", defaultExecutablePath);
NS_ENDHANDLER
}
engineInfo = engineInfo->next;
[newEngine release];
}
return engines;
}
- (void) setContext:(GPGContext *)context
{
_context = context; // Not retained
}
- (void) invalidateContext
{
[self setContext:INVALID_CONTEXT];
}
- (void) reloadContextEngineInfo
{
GPGProtocol myProtocol = [self engineProtocol];
gpgme_engine_info_t anEngineInfo;
anEngineInfo = gpgme_ctx_get_engine_info([_context gpgmeContext]);
while(anEngineInfo != NULL){
if(anEngineInfo->protocol == myProtocol)
break;
anEngineInfo = anEngineInfo->next;
}
NSAssert1(anEngineInfo != NULL, @"### Unable to refresh engine for protocol %@", GPGProtocolDescription(myProtocol));
[[[self class] pointerUniquingTableLock] lock];
[self unregisterUniquePointer];
_internalRepresentation = anEngineInfo;
[self registerUniquePointer];
[[[self class] pointerUniquingTableLock] unlock];
}
@end