|
Line 0
Link Here
|
|
|
1 |
/* |
| 2 |
* Copyright (c) 2007 Apple Inc. All rights reserved. |
| 3 |
* |
| 4 |
* @APPLE_BSD_LICENSE_HEADER_START@ |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in the |
| 14 |
* documentation and/or other materials provided with the distribution. |
| 15 |
* 3. Neither the name of Apple Inc. ("Apple") nor the names of its |
| 16 |
* contributors may be used to endorse or promote products derived from |
| 17 |
* this software without specific prior written permission. |
| 18 |
* |
| 19 |
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 20 |
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 |
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 23 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 24 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 26 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 28 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
* |
| 30 |
* @APPLE_BSD_LICENSE_HEADER_END@ |
| 31 |
*/ |
| 32 |
|
| 33 |
#include "includes.h" |
| 34 |
|
| 35 |
#include <stdio.h> |
| 36 |
#include <string.h> |
| 37 |
|
| 38 |
#include "xmalloc.h" |
| 39 |
#include "key.h" |
| 40 |
#include "authfd.h" |
| 41 |
|
| 42 |
#if defined(__APPLE_KEYCHAIN__) |
| 43 |
|
| 44 |
#include <CoreFoundation/CoreFoundation.h> |
| 45 |
#include <Security/Security.h> |
| 46 |
#include <Security/SecPassword.h> |
| 47 |
|
| 48 |
#endif |
| 49 |
|
| 50 |
/* |
| 51 |
* Platform-specific helper functions. |
| 52 |
*/ |
| 53 |
|
| 54 |
#if defined(__APPLE_KEYCHAIN__) |
| 55 |
|
| 56 |
static int get_boolean_preference(const char *key, int default_value, |
| 57 |
int foreground) |
| 58 |
{ |
| 59 |
int value = default_value; |
| 60 |
CFStringRef keyRef = NULL; |
| 61 |
CFPropertyListRef valueRef = NULL; |
| 62 |
|
| 63 |
keyRef = CFStringCreateWithCString(NULL, key, kCFStringEncodingUTF8); |
| 64 |
if (keyRef != NULL) |
| 65 |
valueRef = CFPreferencesCopyAppValue(keyRef, |
| 66 |
CFSTR("org.openbsd.openssh")); |
| 67 |
if (valueRef != NULL) |
| 68 |
if (CFGetTypeID(valueRef) == CFBooleanGetTypeID()) |
| 69 |
value = CFBooleanGetValue(valueRef); |
| 70 |
else if (foreground) |
| 71 |
fprintf(stderr, "Ignoring nonboolean %s preference.\n", key); |
| 72 |
|
| 73 |
if (keyRef) |
| 74 |
CFRelease(keyRef); |
| 75 |
if (valueRef) |
| 76 |
CFRelease(valueRef); |
| 77 |
|
| 78 |
return value; |
| 79 |
} |
| 80 |
|
| 81 |
#endif |
| 82 |
|
| 83 |
/* |
| 84 |
* Store the passphrase for a given identity in the keychain. |
| 85 |
*/ |
| 86 |
void |
| 87 |
store_in_keychain(const char *filename, const char *passphrase) |
| 88 |
{ |
| 89 |
|
| 90 |
#if defined(__APPLE_KEYCHAIN__) |
| 91 |
|
| 92 |
/* |
| 93 |
* store_in_keychain |
| 94 |
* Mac OS X implementation |
| 95 |
*/ |
| 96 |
|
| 97 |
CFStringRef cfstr_relative_filename = NULL; |
| 98 |
CFURLRef cfurl_relative_filename = NULL, cfurl_filename = NULL; |
| 99 |
CFStringRef cfstr_filename; |
| 100 |
CFDataRef cfdata_filename = NULL; |
| 101 |
CFIndex filename_len; |
| 102 |
UInt8 *label = NULL; |
| 103 |
UInt8 *utf8_filename; |
| 104 |
OSStatus rv; |
| 105 |
SecKeychainItemRef itemRef = NULL; |
| 106 |
SecTrustedApplicationRef apps[] = {NULL, NULL, NULL}; |
| 107 |
CFArrayRef trustedlist = NULL; |
| 108 |
SecAccessRef initialAccess = NULL; |
| 109 |
|
| 110 |
/* Bail out if KeychainIntegration preference is -bool NO */ |
| 111 |
if (get_boolean_preference("KeychainIntegration", 1, 1) == 0) { |
| 112 |
fprintf(stderr, "Keychain integration is disabled.\n"); |
| 113 |
goto err; |
| 114 |
} |
| 115 |
|
| 116 |
/* Interpret filename with the correct encoding. */ |
| 117 |
if ((cfstr_relative_filename = |
| 118 |
CFStringCreateWithFileSystemRepresentation(NULL, filename)) == NULL) |
| 119 |
{ |
| 120 |
fprintf(stderr, "CFStringCreateWithFileSystemRepresentation failed\n"); |
| 121 |
goto err; |
| 122 |
} |
| 123 |
if ((cfurl_relative_filename = CFURLCreateWithFileSystemPath(NULL, |
| 124 |
cfstr_relative_filename, kCFURLPOSIXPathStyle, false)) == NULL) { |
| 125 |
fprintf(stderr, "CFURLCreateWithFileSystemPath failed\n"); |
| 126 |
goto err; |
| 127 |
} |
| 128 |
if ((cfurl_filename = CFURLCopyAbsoluteURL(cfurl_relative_filename)) == |
| 129 |
NULL) { |
| 130 |
fprintf(stderr, "CFURLCopyAbsoluteURL failed\n"); |
| 131 |
goto err; |
| 132 |
} |
| 133 |
if ((cfstr_filename = CFURLCopyFileSystemPath(cfurl_filename, |
| 134 |
kCFURLPOSIXPathStyle)) == NULL) { |
| 135 |
fprintf(stderr, "CFURLGetString failed\n"); |
| 136 |
goto err; |
| 137 |
} |
| 138 |
if ((cfdata_filename = CFStringCreateExternalRepresentation(NULL, |
| 139 |
cfstr_filename, kCFStringEncodingUTF8, 0)) == NULL) { |
| 140 |
fprintf(stderr, "CFStringCreateExternalRepresentation failed\n"); |
| 141 |
goto err; |
| 142 |
} |
| 143 |
filename_len = CFDataGetLength(cfdata_filename); |
| 144 |
if ((label = xmalloc(filename_len + 5)) == NULL) { |
| 145 |
fprintf(stderr, "xmalloc failed\n"); |
| 146 |
goto err; |
| 147 |
} |
| 148 |
memcpy(label, "SSH: ", 5); |
| 149 |
utf8_filename = label + 5; |
| 150 |
CFDataGetBytes(cfdata_filename, CFRangeMake(0, filename_len), |
| 151 |
utf8_filename); |
| 152 |
|
| 153 |
/* Check if we already have this passphrase. */ |
| 154 |
rv = SecKeychainFindGenericPassword(NULL, 3, "SSH", filename_len, |
| 155 |
(char *)utf8_filename, NULL, NULL, &itemRef); |
| 156 |
if (rv == errSecItemNotFound) { |
| 157 |
/* Add a new keychain item. */ |
| 158 |
SecKeychainAttribute attrs[] = { |
| 159 |
{kSecLabelItemAttr, filename_len + 5, label}, |
| 160 |
{kSecServiceItemAttr, 3, "SSH"}, |
| 161 |
{kSecAccountItemAttr, filename_len, utf8_filename} |
| 162 |
}; |
| 163 |
SecKeychainAttributeList attrList = |
| 164 |
{sizeof(attrs) / sizeof(attrs[0]), attrs}; |
| 165 |
if (SecTrustedApplicationCreateFromPath("/usr/bin/ssh-agent", |
| 166 |
&apps[0]) != noErr || |
| 167 |
SecTrustedApplicationCreateFromPath("/usr/bin/ssh-add", |
| 168 |
&apps[1]) != noErr || |
| 169 |
SecTrustedApplicationCreateFromPath("/usr/bin/ssh", |
| 170 |
&apps[2]) != noErr) { |
| 171 |
fprintf(stderr, "SecTrustedApplicationCreateFromPath failed\n"); |
| 172 |
goto err; |
| 173 |
} |
| 174 |
if ((trustedlist = CFArrayCreate(NULL, (const void **)apps, |
| 175 |
sizeof(apps) / sizeof(apps[0]), &kCFTypeArrayCallBacks)) == |
| 176 |
NULL) { |
| 177 |
fprintf(stderr, "CFArrayCreate failed\n"); |
| 178 |
goto err; |
| 179 |
} |
| 180 |
if (SecAccessCreate(cfstr_filename, trustedlist, |
| 181 |
&initialAccess) != noErr) { |
| 182 |
fprintf(stderr, "SecAccessCreate failed\n"); |
| 183 |
goto err; |
| 184 |
} |
| 185 |
if (SecKeychainItemCreateFromContent( |
| 186 |
kSecGenericPasswordItemClass, &attrList, strlen(passphrase), |
| 187 |
passphrase, NULL, initialAccess, NULL) == noErr) |
| 188 |
fprintf(stderr, "Passphrase stored in keychain: %s\n", filename); |
| 189 |
else |
| 190 |
fprintf(stderr, "Could not create keychain item\n"); |
| 191 |
} else if (rv == noErr) { |
| 192 |
/* Update an existing keychain item. */ |
| 193 |
if (SecKeychainItemModifyAttributesAndData(itemRef, NULL, |
| 194 |
strlen(passphrase), passphrase) == noErr) |
| 195 |
fprintf(stderr, "Passphrase updated in keychain: %s\n", filename); |
| 196 |
else |
| 197 |
fprintf(stderr, "Could not modify keychain item\n"); |
| 198 |
} else |
| 199 |
fprintf(stderr, "Could not access keychain\n"); |
| 200 |
|
| 201 |
err: /* Clean up. */ |
| 202 |
if (cfstr_relative_filename) |
| 203 |
CFRelease(cfstr_relative_filename); |
| 204 |
if (cfurl_relative_filename) |
| 205 |
CFRelease(cfurl_relative_filename); |
| 206 |
if (cfurl_filename) |
| 207 |
CFRelease(cfurl_filename); |
| 208 |
if (cfdata_filename) |
| 209 |
CFRelease(cfdata_filename); |
| 210 |
if (label) |
| 211 |
xfree(label); |
| 212 |
if (itemRef) |
| 213 |
CFRelease(itemRef); |
| 214 |
if (apps[0]) |
| 215 |
CFRelease(apps[0]); |
| 216 |
if (apps[1]) |
| 217 |
CFRelease(apps[1]); |
| 218 |
if (apps[2]) |
| 219 |
CFRelease(apps[2]); |
| 220 |
if (trustedlist) |
| 221 |
CFRelease(trustedlist); |
| 222 |
if (initialAccess) |
| 223 |
CFRelease(initialAccess); |
| 224 |
|
| 225 |
#else |
| 226 |
|
| 227 |
/* |
| 228 |
* store_in_keychain |
| 229 |
* no keychain implementation |
| 230 |
*/ |
| 231 |
|
| 232 |
fprintf(stderr, "Keychain is not available on this system\n"); |
| 233 |
|
| 234 |
#endif |
| 235 |
|
| 236 |
} |
| 237 |
|
| 238 |
/* |
| 239 |
* Remove the passphrase for a given identity from the keychain. |
| 240 |
*/ |
| 241 |
void |
| 242 |
remove_from_keychain(const char *filename) |
| 243 |
{ |
| 244 |
|
| 245 |
#if defined(__APPLE_KEYCHAIN__) |
| 246 |
|
| 247 |
/* |
| 248 |
* remove_from_keychain |
| 249 |
* Mac OS X implementation |
| 250 |
*/ |
| 251 |
|
| 252 |
CFStringRef cfstr_relative_filename = NULL; |
| 253 |
CFURLRef cfurl_relative_filename = NULL, cfurl_filename = NULL; |
| 254 |
CFStringRef cfstr_filename; |
| 255 |
CFDataRef cfdata_filename = NULL; |
| 256 |
CFIndex filename_len; |
| 257 |
const UInt8 *utf8_filename; |
| 258 |
OSStatus rv; |
| 259 |
SecKeychainItemRef itemRef = NULL; |
| 260 |
|
| 261 |
/* Bail out if KeychainIntegration preference is -bool NO */ |
| 262 |
if (get_boolean_preference("KeychainIntegration", 1, 1) == 0) { |
| 263 |
fprintf(stderr, "Keychain integration is disabled.\n"); |
| 264 |
goto err; |
| 265 |
} |
| 266 |
|
| 267 |
/* Interpret filename with the correct encoding. */ |
| 268 |
if ((cfstr_relative_filename = |
| 269 |
CFStringCreateWithFileSystemRepresentation(NULL, filename)) == NULL) |
| 270 |
{ |
| 271 |
fprintf(stderr, "CFStringCreateWithFileSystemRepresentation failed\n"); |
| 272 |
goto err; |
| 273 |
} |
| 274 |
if ((cfurl_relative_filename = CFURLCreateWithFileSystemPath(NULL, |
| 275 |
cfstr_relative_filename, kCFURLPOSIXPathStyle, false)) == NULL) { |
| 276 |
fprintf(stderr, "CFURLCreateWithFileSystemPath failed\n"); |
| 277 |
goto err; |
| 278 |
} |
| 279 |
if ((cfurl_filename = CFURLCopyAbsoluteURL(cfurl_relative_filename)) == |
| 280 |
NULL) { |
| 281 |
fprintf(stderr, "CFURLCopyAbsoluteURL failed\n"); |
| 282 |
goto err; |
| 283 |
} |
| 284 |
if ((cfstr_filename = CFURLCopyFileSystemPath(cfurl_filename, |
| 285 |
kCFURLPOSIXPathStyle)) == NULL) { |
| 286 |
fprintf(stderr, "CFURLGetString failed\n"); |
| 287 |
goto err; |
| 288 |
} |
| 289 |
if ((cfdata_filename = CFStringCreateExternalRepresentation(NULL, |
| 290 |
cfstr_filename, kCFStringEncodingUTF8, 0)) == NULL) { |
| 291 |
fprintf(stderr, "CFStringCreateExternalRepresentation failed\n"); |
| 292 |
goto err; |
| 293 |
} |
| 294 |
filename_len = CFDataGetLength(cfdata_filename); |
| 295 |
utf8_filename = CFDataGetBytePtr(cfdata_filename); |
| 296 |
|
| 297 |
/* Check if we already have this passphrase. */ |
| 298 |
rv = SecKeychainFindGenericPassword(NULL, 3, "SSH", filename_len, |
| 299 |
(const char *)utf8_filename, NULL, NULL, &itemRef); |
| 300 |
if (rv == noErr) { |
| 301 |
/* Remove the passphrase from the keychain. */ |
| 302 |
if (SecKeychainItemDelete(itemRef) == noErr) |
| 303 |
fprintf(stderr, "Passphrase removed from keychain: %s\n", filename); |
| 304 |
else |
| 305 |
fprintf(stderr, "Could not remove keychain item\n"); |
| 306 |
} else if (rv != errSecItemNotFound) |
| 307 |
fprintf(stderr, "Could not access keychain\n"); |
| 308 |
|
| 309 |
err: /* Clean up. */ |
| 310 |
if (cfstr_relative_filename) |
| 311 |
CFRelease(cfstr_relative_filename); |
| 312 |
if (cfurl_relative_filename) |
| 313 |
CFRelease(cfurl_relative_filename); |
| 314 |
if (cfurl_filename) |
| 315 |
CFRelease(cfurl_filename); |
| 316 |
if (cfdata_filename) |
| 317 |
CFRelease(cfdata_filename); |
| 318 |
if (itemRef) |
| 319 |
CFRelease(itemRef); |
| 320 |
|
| 321 |
#else |
| 322 |
|
| 323 |
/* |
| 324 |
* remove_from_keychain |
| 325 |
* no keychain implementation |
| 326 |
*/ |
| 327 |
|
| 328 |
fprintf(stderr, "Keychain is not available on this system\n"); |
| 329 |
|
| 330 |
#endif |
| 331 |
|
| 332 |
} |
| 333 |
|
| 334 |
/* |
| 335 |
* Add identities to ssh-agent using passphrases stored in the keychain. |
| 336 |
* Returns zero on success and nonzero on failure. |
| 337 |
* add_identity is a callback into ssh-agent. It takes a filename and a |
| 338 |
* passphrase, and attempts to add the identity to the agent. It returns |
| 339 |
* zero on success and nonzero on failure. |
| 340 |
*/ |
| 341 |
int |
| 342 |
add_identities_using_keychain(int (*add_identity)(const char *, const char *)) |
| 343 |
{ |
| 344 |
|
| 345 |
#if defined(__APPLE_KEYCHAIN__) |
| 346 |
|
| 347 |
/* |
| 348 |
* add_identities_using_keychain |
| 349 |
* Mac OS X implementation |
| 350 |
*/ |
| 351 |
|
| 352 |
OSStatus rv; |
| 353 |
SecKeychainSearchRef searchRef; |
| 354 |
SecKeychainItemRef itemRef; |
| 355 |
UInt32 length; |
| 356 |
void *data; |
| 357 |
CFIndex maxsize; |
| 358 |
|
| 359 |
/* Bail out if KeychainIntegration preference is -bool NO */ |
| 360 |
if (get_boolean_preference("KeychainIntegration", 1, 0) == 0) |
| 361 |
return 0; |
| 362 |
|
| 363 |
/* Search for SSH passphrases in the keychain */ |
| 364 |
SecKeychainAttribute attrs[] = { |
| 365 |
{kSecServiceItemAttr, 3, "SSH"} |
| 366 |
}; |
| 367 |
SecKeychainAttributeList attrList = |
| 368 |
{sizeof(attrs) / sizeof(attrs[0]), attrs}; |
| 369 |
if ((rv = SecKeychainSearchCreateFromAttributes(NULL, |
| 370 |
kSecGenericPasswordItemClass, &attrList, &searchRef)) != noErr) |
| 371 |
return 0; |
| 372 |
|
| 373 |
/* Iterate through the search results. */ |
| 374 |
while ((rv = SecKeychainSearchCopyNext(searchRef, &itemRef)) == noErr) { |
| 375 |
UInt32 tag = kSecAccountItemAttr; |
| 376 |
UInt32 format = kSecFormatUnknown; |
| 377 |
SecKeychainAttributeInfo info = {1, &tag, &format}; |
| 378 |
SecKeychainAttributeList *itemAttrList = NULL; |
| 379 |
CFStringRef cfstr_filename = NULL; |
| 380 |
char *filename = NULL; |
| 381 |
char *passphrase = NULL; |
| 382 |
|
| 383 |
/* Retrieve filename and passphrase. */ |
| 384 |
if ((rv = SecKeychainItemCopyAttributesAndData(itemRef, &info, |
| 385 |
NULL, &itemAttrList, &length, &data)) != noErr) |
| 386 |
goto err; |
| 387 |
if (itemAttrList->count != 1) |
| 388 |
goto err; |
| 389 |
cfstr_filename = CFStringCreateWithBytes(NULL, |
| 390 |
itemAttrList->attr->data, itemAttrList->attr->length, |
| 391 |
kCFStringEncodingUTF8, true); |
| 392 |
maxsize = CFStringGetMaximumSizeOfFileSystemRepresentation( |
| 393 |
cfstr_filename); |
| 394 |
if ((filename = xmalloc(maxsize)) == NULL) |
| 395 |
goto err; |
| 396 |
if (CFStringGetFileSystemRepresentation(cfstr_filename, |
| 397 |
filename, maxsize) == false) |
| 398 |
goto err; |
| 399 |
if ((passphrase = xmalloc(length + 1)) == NULL) |
| 400 |
goto err; |
| 401 |
memcpy(passphrase, data, length); |
| 402 |
passphrase[length] = '\0'; |
| 403 |
|
| 404 |
/* Add the identity. */ |
| 405 |
add_identity(filename, passphrase); |
| 406 |
|
| 407 |
err: /* Clean up. */ |
| 408 |
if (itemRef) |
| 409 |
CFRelease(itemRef); |
| 410 |
if (cfstr_filename) |
| 411 |
CFRelease(cfstr_filename); |
| 412 |
if (filename) |
| 413 |
xfree(filename); |
| 414 |
if (passphrase) |
| 415 |
xfree(passphrase); |
| 416 |
if (itemAttrList) |
| 417 |
SecKeychainItemFreeAttributesAndData(itemAttrList, |
| 418 |
data); |
| 419 |
} |
| 420 |
|
| 421 |
CFRelease(searchRef); |
| 422 |
|
| 423 |
return 0; |
| 424 |
|
| 425 |
#else |
| 426 |
|
| 427 |
/* |
| 428 |
* add_identities_using_keychain |
| 429 |
* no implementation |
| 430 |
*/ |
| 431 |
|
| 432 |
return 1; |
| 433 |
|
| 434 |
#endif |
| 435 |
|
| 436 |
} |
| 437 |
|
| 438 |
/* |
| 439 |
* Prompt the user for a key's passphrase. The user will be offered the option |
| 440 |
* of storing the passphrase in their keychain. Returns the passphrase |
| 441 |
* (which the caller is responsible for xfreeing), or NULL if this function |
| 442 |
* fails or is not implemented. If this function is not implemented, ssh will |
| 443 |
* fall back on the standard read_passphrase function, and the user will need |
| 444 |
* to use ssh-add -K to add their keys to the keychain. |
| 445 |
*/ |
| 446 |
char * |
| 447 |
keychain_read_passphrase(const char *filename) |
| 448 |
{ |
| 449 |
|
| 450 |
#if defined(__APPLE_KEYCHAIN__) |
| 451 |
|
| 452 |
/* |
| 453 |
* keychain_read_passphrase |
| 454 |
* Mac OS X implementation |
| 455 |
*/ |
| 456 |
|
| 457 |
CFStringRef cfstr_relative_filename = NULL; |
| 458 |
CFURLRef cfurl_relative_filename = NULL, cfurl_filename = NULL; |
| 459 |
CFStringRef cfstr_filename; |
| 460 |
CFDataRef cfdata_filename = NULL; |
| 461 |
CFIndex filename_len; |
| 462 |
UInt8 *label = NULL; |
| 463 |
UInt8 *utf8_filename; |
| 464 |
SecPasswordRef passRef = NULL; |
| 465 |
SecTrustedApplicationRef apps[] = {NULL, NULL, NULL}; |
| 466 |
CFArrayRef trustedlist = NULL; |
| 467 |
SecAccessRef initialAccess = NULL; |
| 468 |
CFURLRef path = NULL; |
| 469 |
CFStringRef pathFinal = NULL; |
| 470 |
CFURLRef bundle_url = NULL; |
| 471 |
CFBundleRef bundle = NULL; |
| 472 |
CFStringRef promptTemplate = NULL, prompt = NULL; |
| 473 |
UInt32 length; |
| 474 |
const void *data; |
| 475 |
AuthenticationConnection *ac = NULL; |
| 476 |
char *result = NULL; |
| 477 |
|
| 478 |
/* Bail out if KeychainIntegration preference is -bool NO */ |
| 479 |
if (get_boolean_preference("KeychainIntegration", 1, 1) == 0) |
| 480 |
goto err; |
| 481 |
|
| 482 |
/* Bail out if the user set AskPassGUI preference to -bool NO */ |
| 483 |
if (get_boolean_preference("AskPassGUI", 1, 1) == 0) |
| 484 |
goto err; |
| 485 |
|
| 486 |
/* Bail out if we can't communicate with ssh-agent */ |
| 487 |
if ((ac = ssh_get_authentication_connection()) == NULL) |
| 488 |
goto err; |
| 489 |
|
| 490 |
/* Interpret filename with the correct encoding. */ |
| 491 |
if ((cfstr_relative_filename = |
| 492 |
CFStringCreateWithFileSystemRepresentation(NULL, filename)) == NULL) |
| 493 |
{ |
| 494 |
fprintf(stderr, "CFStringCreateWithFileSystemRepresentation failed\n"); |
| 495 |
goto err; |
| 496 |
} |
| 497 |
if ((cfurl_relative_filename = CFURLCreateWithFileSystemPath(NULL, |
| 498 |
cfstr_relative_filename, kCFURLPOSIXPathStyle, false)) == NULL) { |
| 499 |
fprintf(stderr, "CFURLCreateWithFileSystemPath failed\n"); |
| 500 |
goto err; |
| 501 |
} |
| 502 |
if ((cfurl_filename = CFURLCopyAbsoluteURL(cfurl_relative_filename)) == |
| 503 |
NULL) { |
| 504 |
fprintf(stderr, "CFURLCopyAbsoluteURL failed\n"); |
| 505 |
goto err; |
| 506 |
} |
| 507 |
if ((cfstr_filename = CFURLCopyFileSystemPath(cfurl_filename, |
| 508 |
kCFURLPOSIXPathStyle)) == NULL) { |
| 509 |
fprintf(stderr, "CFURLGetString failed\n"); |
| 510 |
goto err; |
| 511 |
} |
| 512 |
if ((cfdata_filename = CFStringCreateExternalRepresentation(NULL, |
| 513 |
cfstr_filename, kCFStringEncodingUTF8, 0)) == NULL) { |
| 514 |
fprintf(stderr, "CFStringCreateExternalRepresentation failed\n"); |
| 515 |
goto err; |
| 516 |
} |
| 517 |
filename_len = CFDataGetLength(cfdata_filename); |
| 518 |
if ((label = xmalloc(filename_len + 5)) == NULL) { |
| 519 |
fprintf(stderr, "xmalloc failed\n"); |
| 520 |
goto err; |
| 521 |
} |
| 522 |
memcpy(label, "SSH: ", 5); |
| 523 |
utf8_filename = label + 5; |
| 524 |
CFDataGetBytes(cfdata_filename, CFRangeMake(0, filename_len), |
| 525 |
utf8_filename); |
| 526 |
|
| 527 |
/* Build a SecPasswordRef. */ |
| 528 |
SecKeychainAttribute searchAttrs[] = { |
| 529 |
{kSecServiceItemAttr, 3, "SSH"}, |
| 530 |
{kSecAccountItemAttr, filename_len, utf8_filename} |
| 531 |
}; |
| 532 |
SecKeychainAttributeList searchAttrList = |
| 533 |
{sizeof(searchAttrs) / sizeof(searchAttrs[0]), searchAttrs}; |
| 534 |
SecKeychainAttribute attrs[] = { |
| 535 |
{kSecLabelItemAttr, filename_len + 5, label}, |
| 536 |
{kSecServiceItemAttr, 3, "SSH"}, |
| 537 |
{kSecAccountItemAttr, filename_len, utf8_filename} |
| 538 |
}; |
| 539 |
SecKeychainAttributeList attrList = |
| 540 |
{sizeof(attrs) / sizeof(attrs[0]), attrs}; |
| 541 |
if (SecGenericPasswordCreate(&searchAttrList, &attrList, &passRef) != |
| 542 |
noErr) { |
| 543 |
fprintf(stderr, "SecGenericPasswordCreate failed\n"); |
| 544 |
goto err; |
| 545 |
} |
| 546 |
if (SecTrustedApplicationCreateFromPath("/usr/bin/ssh-agent", &apps[0]) |
| 547 |
!= noErr || |
| 548 |
SecTrustedApplicationCreateFromPath("/usr/bin/ssh-add", &apps[1]) |
| 549 |
!= noErr || |
| 550 |
SecTrustedApplicationCreateFromPath("/usr/bin/ssh", &apps[2]) |
| 551 |
!= noErr) { |
| 552 |
fprintf(stderr, "SecTrustedApplicationCreateFromPath failed\n"); |
| 553 |
goto err; |
| 554 |
} |
| 555 |
if ((trustedlist = CFArrayCreate(NULL, (const void **)apps, |
| 556 |
sizeof(apps) / sizeof(apps[0]), &kCFTypeArrayCallBacks)) == NULL) { |
| 557 |
fprintf(stderr, "CFArrayCreate failed\n"); |
| 558 |
goto err; |
| 559 |
} |
| 560 |
if (SecAccessCreate(cfstr_filename, trustedlist, &initialAccess) |
| 561 |
!= noErr) { |
| 562 |
fprintf(stderr, "SecAccessCreate failed\n"); |
| 563 |
goto err; |
| 564 |
} |
| 565 |
if (SecPasswordSetInitialAccess(passRef, initialAccess) != noErr) { |
| 566 |
fprintf(stderr, "SecPasswordSetInitialAccess failed\n"); |
| 567 |
goto err; |
| 568 |
} |
| 569 |
|
| 570 |
/* Request the passphrase from the user. */ |
| 571 |
path = CFURLCreateFromFileSystemRepresentation(NULL, (UInt8 *)filename, |
| 572 |
strlen(filename), false); |
| 573 |
pathFinal = CFURLCopyLastPathComponent(path); |
| 574 |
if (!((bundle_url = CFURLCreateWithFileSystemPath(NULL, |
| 575 |
CFSTR("/System/Library/CoreServices/"), kCFURLPOSIXPathStyle, true)) |
| 576 |
!= NULL && (bundle = CFBundleCreate(NULL, bundle_url)) != NULL && |
| 577 |
(promptTemplate = CFCopyLocalizedStringFromTableInBundle( |
| 578 |
CFSTR("Enter your password for the SSH key \"%@\"."), |
| 579 |
CFSTR("OpenSSH"), bundle, "Text of the dialog asking the user for" |
| 580 |
"their passphrase. The %@ will be replaced with the filename of a" |
| 581 |
"specific key.")) != NULL) && |
| 582 |
(promptTemplate = CFStringCreateCopy(NULL, |
| 583 |
CFSTR("Enter your password for the SSH key \"%@\"."))) == NULL) { |
| 584 |
fprintf(stderr, "CFStringCreateCopy failed\n"); |
| 585 |
goto err; |
| 586 |
} |
| 587 |
prompt = CFStringCreateWithFormat(NULL, NULL, promptTemplate, |
| 588 |
pathFinal); |
| 589 |
switch (SecPasswordAction(passRef, prompt, |
| 590 |
kSecPasswordGet|kSecPasswordFail, &length, &data)) { |
| 591 |
case noErr: |
| 592 |
result = xmalloc(length + 1); |
| 593 |
memcpy(result, data, length); |
| 594 |
result[length] = '\0'; |
| 595 |
|
| 596 |
/* Save password in keychain if requested. */ |
| 597 |
if (SecPasswordAction(passRef, CFSTR(""), kSecPasswordSet, |
| 598 |
&length, &data) == noErr) |
| 599 |
ssh_add_from_keychain(ac); |
| 600 |
break; |
| 601 |
case errAuthorizationCanceled: |
| 602 |
result = xmalloc(1); |
| 603 |
*result = '\0'; |
| 604 |
break; |
| 605 |
default: |
| 606 |
goto err; |
| 607 |
} |
| 608 |
|
| 609 |
err: /* Clean up. */ |
| 610 |
if (cfstr_relative_filename) |
| 611 |
CFRelease(cfstr_relative_filename); |
| 612 |
if (cfurl_relative_filename) |
| 613 |
CFRelease(cfurl_relative_filename); |
| 614 |
if (cfurl_filename) |
| 615 |
CFRelease(cfurl_filename); |
| 616 |
if (cfdata_filename) |
| 617 |
CFRelease(cfdata_filename); |
| 618 |
if (label) |
| 619 |
xfree(label); |
| 620 |
if (passRef) |
| 621 |
CFRelease(passRef); |
| 622 |
if (apps[0]) |
| 623 |
CFRelease(apps[0]); |
| 624 |
if (apps[1]) |
| 625 |
CFRelease(apps[1]); |
| 626 |
if (apps[2]) |
| 627 |
CFRelease(apps[2]); |
| 628 |
if (trustedlist) |
| 629 |
CFRelease(trustedlist); |
| 630 |
if (initialAccess) |
| 631 |
CFRelease(initialAccess); |
| 632 |
if (path) |
| 633 |
CFRelease(path); |
| 634 |
if (pathFinal) |
| 635 |
CFRelease(pathFinal); |
| 636 |
if (bundle_url) |
| 637 |
CFRelease(bundle_url); |
| 638 |
if (bundle) |
| 639 |
CFRelease(bundle); |
| 640 |
if (promptTemplate) |
| 641 |
CFRelease(promptTemplate); |
| 642 |
if (prompt) |
| 643 |
CFRelease(prompt); |
| 644 |
if (ac) |
| 645 |
ssh_close_authentication_connection(ac); |
| 646 |
|
| 647 |
return result; |
| 648 |
|
| 649 |
#else |
| 650 |
|
| 651 |
/* |
| 652 |
* keychain_read_passphrase |
| 653 |
* no implementation |
| 654 |
*/ |
| 655 |
|
| 656 |
return NULL; |
| 657 |
|
| 658 |
#endif |
| 659 |
|
| 660 |
} |