KiCad PCB EDA Suite
Loading...
Searching...
No Matches
kigit_ssh_keys.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include "kigit_ssh_keys.h"
21
22#include <trace_helpers.h>
23
24#include <algorithm>
25#include <cstdint>
26#include <cstring>
27#include <fstream>
28#include <iterator>
29#include <map>
30#include <mutex>
31#include <string>
32
33#include <nlohmann/json.hpp>
34
35#include <wx/arrstr.h>
36#include <wx/base64.h>
37#include <wx/dir.h>
38#include <wx/ffile.h>
39#include <wx/file.h>
40#include <wx/filename.h>
41#include <wx/log.h>
42#include <wx/utils.h>
43
44#ifndef _WIN32
45#include <cerrno>
46#include <cstdlib>
47#include <pwd.h>
48#include <unistd.h>
49#endif
50
51
52namespace
53{
54
55// Quotes only group, so a Windows path keeps its backslashes
56std::vector<wxString> splitArgs( const wxString& aText )
57{
58 std::vector<wxString> args;
59 wxString current;
60 bool quoted = false;
61 bool inArg = false;
62
63 for( wxUniChar c : aText )
64 {
65 // As ssh, a comment starts only where an argument could
66 if( !quoted && !inArg && c == '#' )
67 break;
68
69 if( c == '"' )
70 {
71 quoted = !quoted;
72 inArg = true;
73 }
74 else if( !quoted && ( c == ' ' || c == '\t' ) )
75 {
76 if( inArg )
77 args.push_back( current );
78
79 current.clear();
80 inArg = false;
81 }
82 else
83 {
84 current += c;
85 inArg = true;
86 }
87 }
88
89 if( inArg )
90 args.push_back( current );
91
92 return args;
93}
94
95
96// git runs core.sshCommand through sh, Git for Windows included, so its words split as sh splits them
97std::vector<wxString> splitShellArgs( const wxString& aText )
98{
99 std::vector<wxString> args;
100 wxString current;
101 bool inArg = false;
102
103 for( auto it = aText.begin(); it != aText.end(); ++it )
104 {
105 const wxUniChar c = *it;
106
107 if( c == '\'' )
108 {
109 inArg = true;
110
111 while( ++it != aText.end() && *it != '\'' )
112 current += *it;
113
114 if( it == aText.end() )
115 break;
116 }
117 else if( c == '"' )
118 {
119 inArg = true;
120
121 while( ++it != aText.end() && *it != '"' )
122 {
123 auto next = std::next( it );
124
125 if( *it == '\\' && next != aText.end()
126 && ( *next == '"' || *next == '\\' || *next == '$' || *next == '`' ) )
127 {
128 it = next;
129 }
130
131 current += *it;
132 }
133
134 if( it == aText.end() )
135 break;
136 }
137 else if( c == '\\' )
138 {
139 inArg = true;
140
141 if( ++it == aText.end() )
142 break;
143
144 current += *it;
145 }
146 else if( c == ' ' || c == '\t' || c == '\n' )
147 {
148 if( inArg )
149 args.push_back( current );
150
151 current.clear();
152 inArg = false;
153 }
154 else
155 {
156 current += c;
157 inArg = true;
158 }
159 }
160
161 if( inArg )
162 args.push_back( current );
163
164 return args;
165}
166
167
168// A keyword ends at whitespace or '=', and either or both may separate it from its arguments
169void splitKeyword( const wxString& aLine, wxString& aKeyword, wxString& aArgs )
170{
171 size_t end = aLine.find_first_of( wxS( " \t=" ) );
172
173 if( end == wxString::npos )
174 {
175 aKeyword = aLine;
176 aArgs.clear();
177 return;
178 }
179
180 aKeyword = aLine.substr( 0, end );
181
182 size_t start = aLine.find_first_not_of( wxS( " \t" ), end );
183
184 if( start != wxString::npos && aLine[start] == '=' )
185 start = aLine.find_first_not_of( wxS( " \t" ), start + 1 );
186
187 aArgs = start == wxString::npos ? wxString() : aLine.substr( start );
188}
189
190
191// A matching negated pattern rejects the host even when another pattern accepts it. ssh compares
192// patterns case-sensitively with the host as typed in the address
193bool hostMatches( const std::vector<wxString>& aPatterns, const wxString& aHost )
194{
195 bool matched = false;
196
197 for( const wxString& pattern : aPatterns )
198 {
199 const bool negated = pattern.StartsWith( wxS( "!" ) );
200 wxString glob = negated ? pattern.Mid( 1 ) : pattern;
201
202 if( !aHost.Matches( glob ) )
203 continue;
204
205 if( negated )
206 return false;
207
208 matched = true;
209 }
210
211 return matched;
212}
213
214
215wxString expandPath( const wxString& aValue, const wxString& aHomeDir )
216{
217 // The other tokens need the connection's user, port and the like, which we do not know
218 for( size_t pos = aValue.find( '%' ); pos != wxString::npos; pos = aValue.find( '%', pos + 2 ) )
219 {
220 if( pos + 1 >= aValue.size() || aValue[pos + 1] != 'd' )
221 return aValue;
222 }
223
224 wxString head;
225 wxString tail = aValue;
226
227 // "~user" names another account's home, which is not ours to guess
228 if( aValue == wxS( "~" ) || aValue.StartsWith( wxS( "~/" ) ) || aValue.StartsWith( wxS( "~\\" ) ) )
229 {
230 head = aHomeDir;
231 tail = aValue.Mid( 1 );
232 }
233
234 tail.Replace( wxS( "%d" ), aHomeDir );
235 return head + tail;
236}
237
238
239// sshd allows 6 attempts by default. The agent and each key refused before this one use some, so a long tail
240// of keys found by name alone would never be reached
241constexpr size_t MAX_DISCOVERED_KEYS = 4;
242
243
244struct DERIVED_PUBLIC_KEYS
245{
246 std::mutex mutex;
247 wxString folder;
248};
249
250
251DERIVED_PUBLIC_KEYS& derivedPublicKeys()
252{
253 static DERIVED_PUBLIC_KEYS store;
254 return store;
255}
256
257
258// FNV-1a, so a key finds the same file in every session and build
259wxString derivedPublicKeyPath( const wxString& aKey )
260{
261 wxString folder;
262
263 {
264 DERIVED_PUBLIC_KEYS& store = derivedPublicKeys();
265 std::lock_guard<std::mutex> lock( store.mutex );
266 folder = store.folder;
267 }
268
269 if( folder.IsEmpty() || aKey.IsEmpty() )
270 return wxEmptyString;
271
272 uint64_t hash = 14695981039346656037ULL;
273
274 for( unsigned char c : wxFileName( aKey ).GetFullPath().utf8_string() )
275 {
276 hash ^= c;
277 hash *= 1099511628211ULL;
278 }
279
280 return wxFileName( folder, wxString::Format( wxS( "%016llx.pub" ), static_cast<unsigned long long>( hash ) ) )
281 .GetFullPath();
282}
283
284
285uint32_t readUint32( const unsigned char* aData )
286{
287 return ( static_cast<uint32_t>( aData[0] ) << 24 ) | ( static_cast<uint32_t>( aData[1] ) << 16 )
288 | ( static_cast<uint32_t>( aData[2] ) << 8 ) | static_cast<uint32_t>( aData[3] );
289}
290
291
292// The "<type> <base64 blob>" line for an OpenSSH private key, read from its unencrypted header. Empty for any
293// other format, whose public half is only reachable with the passphrase
294std::string openSSHPublicKeyLine( const std::string& aKeyText )
295{
296 const std::string begin = "-----BEGIN OPENSSH PRIVATE KEY-----";
297 size_t start = aKeyText.find( begin );
298
299 if( start == std::string::npos )
300 return {};
301
302 start += begin.size();
303 size_t end = aKeyText.find( "-----END", start );
304
305 if( end == std::string::npos )
306 return {};
307
308 wxMemoryBuffer raw = wxBase64Decode( aKeyText.data() + start, end - start, wxBase64DecodeMode_SkipWS );
309
310 const unsigned char* data = static_cast<const unsigned char*>( raw.GetData() );
311 const size_t size = raw.GetDataLen();
312 const char magic[] = "openssh-key-v1";
313 size_t pos = sizeof( magic );
314
315 if( size < pos || std::memcmp( data, magic, pos ) != 0 )
316 return {};
317
318 // Cipher name, KDF name and KDF options come before the key count and the first public key
319 for( int field = 0; field < 3; ++field )
320 {
321 if( size < pos + 4 || size - pos - 4 < readUint32( data + pos ) )
322 return {};
323
324 pos += 4 + readUint32( data + pos );
325 }
326
327 if( size < pos + 8 || readUint32( data + pos ) == 0 )
328 return {};
329
330 const uint32_t blobLen = readUint32( data + pos + 4 );
331 pos += 8;
332
333 if( size - pos < blobLen || blobLen < 4 || blobLen - 4 < readUint32( data + pos ) )
334 return {};
335
336 const std::string type( reinterpret_cast<const char*>( data + pos + 4 ), readUint32( data + pos ) );
337
338 if( type.empty() )
339 return {};
340
341 return type + " " + wxBase64Encode( data + pos, blobLen ).utf8_string() + "\n";
342}
343
344
345// A private key named only by being in ~/.ssh with a .pub beside it, sorted so the order is stable
346std::vector<wxString> scanSSHFolder( const wxString& aSSHDir )
347{
348 std::vector<wxString> keys;
349 wxDir dir;
350
351 if( !wxDir::Exists( aSSHDir ) || !dir.Open( aSSHDir ) )
352 return keys;
353
354 wxString name;
355
356 for( bool more = dir.GetFirst( &name, wxEmptyString, wxDIR_FILES ); more; more = dir.GetNext( &name ) )
357 {
358 const wxString path = wxFileName( aSSHDir, name ).GetFullPath();
359
360 if( name.EndsWith( wxS( ".pub" ) ) || !wxFileName::FileExists( path + wxS( ".pub" ) ) )
361 continue;
362
363 wxFFile file( path, wxS( "rb" ) );
364 char head[64] = {};
365 size_t read = file.IsOpened() ? file.Read( head, sizeof( head ) - 1 ) : 0;
366
367 if( std::string( head, read ).find( "PRIVATE KEY" ) != std::string::npos )
368 keys.push_back( path );
369 }
370
371 std::sort( keys.begin(), keys.end() );
372 return keys;
373}
374
375
376struct REMEMBERED_KEYS
377{
378 std::mutex mutex;
379 wxString path;
380 bool loaded = false;
381 std::map<wxString, wxString> keys;
382 std::map<wxString, wxString> pending;
383};
384
385
386REMEMBERED_KEYS& rememberedKeys()
387{
388 static REMEMBERED_KEYS store;
389 return store;
390}
391
392
393std::map<wxString, wxString> readRememberedKeys( const wxString& aPath )
394{
395 std::map<wxString, wxString> keys;
396
397 if( aPath.IsEmpty() )
398 return keys;
399
400 std::ifstream file( aPath.fn_str(), std::ios::binary );
401
402 if( !file )
403 return keys;
404
405 // A damaged file only costs the order keys are offered in, so it is dropped rather than reported
406 nlohmann::json json = nlohmann::json::parse( file, nullptr, false );
407
408 if( !json.is_object() )
409 return keys;
410
411 for( const auto& [remote, key] : json.items() )
412 {
413 if( key.is_string() )
414 keys[wxString::FromUTF8( remote )] = wxString::FromUTF8( key.get<std::string>() );
415 }
416
417 return keys;
418}
419
420
421bool writeRememberedKeys( const wxString& aPath, const std::map<wxString, wxString>& aKeys )
422{
423 nlohmann::json json = nlohmann::json::object();
424
425 for( const auto& [remote, key] : aKeys )
426 json[remote.utf8_string()] = key.utf8_string();
427
428 wxFileName fn( aPath );
429
430 if( !fn.DirExists() && !fn.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
431 return false;
432
433 // Commit renames a uniquely named file over the old one, so a reader never sees half a file
434 wxTempFile out( aPath );
435
436 return out.IsOpened() && out.Write( wxString::FromUTF8( json.dump( 2 ) ), wxConvUTF8 ) && out.Commit();
437}
438
439
440// The caller holds the store's mutex
441void loadRememberedKeys( REMEMBERED_KEYS& aStore )
442{
443 if( aStore.loaded )
444 return;
445
446 aStore.loaded = true;
447 aStore.keys = readRememberedKeys( aStore.path );
448}
449
450} // namespace
451
452
453std::vector<wxString> KIGIT::ParseSSHConfigIdentityFiles( const wxString& aConfigText, const wxString& aHost,
454 const wxString& aHomeDir )
455{
456 std::vector<wxString> files;
457
458 // Lines before the first Host apply to every host
459 bool active = true;
460
461 for( wxString line : wxSplit( aConfigText, '\n', '\0' ) )
462 {
463 line.Trim( true ).Trim( false );
464
465 if( line.IsEmpty() || line.StartsWith( wxS( "#" ) ) )
466 continue;
467
468 wxString keyword;
469 wxString args;
470 splitKeyword( line, keyword, args );
471 keyword.MakeLower();
472
473 if( keyword == wxS( "host" ) )
474 {
475 active = hostMatches( splitArgs( args ), aHost );
476 }
477 else if( keyword == wxS( "match" ) )
478 {
479 // Match criteria need the resolved connection, so its block is never applied
480 active = false;
481 }
482 else if( active && keyword == wxS( "identityfile" ) )
483 {
484 std::vector<wxString> values = splitArgs( args );
485
486 if( !values.empty() )
487 files.push_back( expandPath( values.front(), aHomeDir ) );
488 }
489 }
490
491 return files;
492}
493
494
495std::vector<wxString> KIGIT::ParseSSHCommandIdentityFiles( const wxString& aCommand, const wxString& aHomeDir )
496{
497 std::vector<wxString> files;
498 std::vector<wxString> args = splitShellArgs( aCommand );
499
500 for( size_t ii = 0; ii < args.size(); ++ii )
501 {
502 if( args[ii] == wxS( "-i" ) && ii + 1 < args.size() )
503 files.push_back( expandPath( args[++ii], aHomeDir ) );
504 else if( args[ii].StartsWith( wxS( "-i" ) ) && args[ii].size() > 2 )
505 files.push_back( expandPath( args[ii].Mid( 2 ), aHomeDir ) );
506 }
507
508 return files;
509}
510
511
512std::vector<wxString> KIGIT::DefaultSSHKeyCandidates( const wxString& aHomeDir, const wxString& aHost,
513 const wxString& aSSHCommand )
514{
515 std::vector<wxString> keys;
516
517 // An empty home would make every key path relative to the working directory
518 if( aHomeDir.IsEmpty() )
519 return keys;
520
521 wxFileName sshDir( aHomeDir, wxEmptyString );
522 sshDir.AppendDir( wxS( ".ssh" ) );
523
524 const wxString configPath = wxFileName( sshDir.GetPath(), wxS( "config" ) ).GetFullPath();
525 std::vector<wxString> candidates = ParseSSHCommandIdentityFiles( aSSHCommand, aHomeDir );
526 std::ifstream config( configPath.fn_str(), std::ios::binary );
527
528 if( config )
529 {
530 std::string text( ( std::istreambuf_iterator<char>( config ) ), std::istreambuf_iterator<char>() );
531
532 // ssh rejects a BOM, but Windows editors write one and it would hide the first Host line
533 if( text.rfind( "\xEF\xBB\xBF", 0 ) == 0 )
534 text.erase( 0, 3 );
535
536 wxString decoded = wxString::FromUTF8( text );
537
538 // An ANSI-saved config would otherwise decode to nothing
539 if( decoded.IsEmpty() && !text.empty() )
540 decoded = wxString( text.data(), wxConvISO8859_1, text.size() );
541
542 std::vector<wxString> configured = ParseSSHConfigIdentityFiles( decoded, aHost, aHomeDir );
543 candidates.insert( candidates.end(), configured.begin(), configured.end() );
544 }
545
546 for( const wxString name : { wxS( "id_ed25519" ), wxS( "id_ecdsa" ), wxS( "id_rsa" ), wxS( "id_dsa" ) } )
547 candidates.push_back( wxFileName( sshDir.GetPath(), name ).GetFullPath() );
548
549 auto isKnown = [&]( const wxString& aPath )
550 {
551 return std::any_of( keys.begin(), keys.end(),
552 [&]( const wxString& aKey )
553 {
554 return IsSameSSHKey( aKey, aPath );
555 } );
556 };
557
558 for( const wxString& candidate : candidates )
559 {
560 wxFileName fn( candidate );
561
562 // ssh would resolve a relative path against its working directory, which ours is not
563 if( !fn.IsAbsolute() || !wxFileName::FileExists( fn.GetFullPath() ) )
564 continue;
565
566 const wxString path = fn.GetFullPath();
567
568 if( !isKnown( path ) && !EnsurePublicKeyFile( path ).IsEmpty() )
569 keys.push_back( path );
570 }
571
572 for( const wxString& path : scanSSHFolder( sshDir.GetPath() ) )
573 {
574 if( keys.size() >= MAX_DISCOVERED_KEYS )
575 break;
576
577 if( !isKnown( path ) )
578 keys.push_back( path );
579 }
580
581 return keys;
582}
583
584
585bool KIGIT::IsSameSSHKey( const wxString& aKey, const wxString& aOther )
586{
587 if( aKey.IsEmpty() || aOther.IsEmpty() )
588 return aKey == aOther;
589
590 return wxFileName( aKey ).GetFullPath().IsSameAs( wxFileName( aOther ).GetFullPath(),
591 wxFileName::IsCaseSensitive() );
592}
593
594
595// wxGetenv converts through an unlocked static buffer except on MSVC, and key discovery runs on any thread
596std::vector<wxString> KIGIT::HomeDirCandidates()
597{
598 std::vector<wxString> homes;
599
600#ifdef _WIN32
601 wxString drive;
602 wxString path;
603 wxString profile;
604
605 homes.push_back( wxGetHomeDir() );
606
607 // wxGetHomeDir drops HOMEPATH "", which is also how a mapped network home drive such as H: looks
608 if( wxGetEnv( wxS( "HOMEDRIVE" ), &drive ) && wxGetEnv( wxS( "HOMEPATH" ), &path ) && !drive.IsEmpty() )
609 homes.push_back( drive + path );
610
611 if( wxGetEnv( wxS( "USERPROFILE" ), &profile ) && !profile.IsEmpty() )
612 homes.push_back( profile );
613#else
614 if( const char* home = std::getenv( "HOME" ); home && *home )
615 {
616 homes.emplace_back( home, wxConvLibc );
617 return homes;
618 }
619
620 passwd entry;
621 passwd* found = nullptr;
622 std::vector<char> buffer( 1024 );
623 int rc;
624
625 while( ( rc = getpwuid_r( getuid(), &entry, buffer.data(), buffer.size(), &found ) ) == ERANGE )
626 buffer.resize( buffer.size() * 2 );
627
628 if( rc == 0 && found && found->pw_dir )
629 homes.emplace_back( found->pw_dir, wxConvLibc );
630#endif
631
632 return homes;
633}
634
635
637{
638 const std::vector<wxString> homes = HomeDirCandidates();
639
640 // Git for Windows keeps keys under HOME and Windows OpenSSH under USERPROFILE
641 for( const wxString& home : homes )
642 {
643 wxFileName sshDir( home, wxEmptyString );
644 sshDir.AppendDir( wxS( ".ssh" ) );
645
646 if( sshDir.DirExists() )
647 return home;
648 }
649
650 return homes.empty() ? wxString() : homes.front();
651}
652
653
655{
656 const wxString home = HomeDir();
657
658 if( home.IsEmpty() )
659 return wxEmptyString;
660
661 wxFileName sshDir( home, wxEmptyString );
662 sshDir.AppendDir( wxS( ".ssh" ) );
663 return sshDir.GetPath();
664}
665
666
667wxString KIGIT::PrivateKeyPath( const wxString& aPicked )
668{
669 return aPicked.ends_with( wxS( ".pub" ) ) ? aPicked.Left( aPicked.size() - 4 ) : aPicked;
670}
671
672
673std::string KIGIT::PublicKeyBlob( const wxString& aKey )
674{
675 wxFFile file( PublicKeyPath( aKey ), wxS( "rb" ) );
676 wxString text;
677
678 if( !file.IsOpened() || !file.ReadAll( &text ) )
679 return {};
680
681 // "<type> <base64 blob> [comment]" on the first line
682 wxArrayString fields = wxSplit( text.BeforeFirst( '\n' ).Trim().Trim( false ), ' ', '\0' );
683
684 if( fields.size() < 2 )
685 return {};
686
687 wxMemoryBuffer blob = wxBase64Decode( fields[1] );
688
689 return std::string( static_cast<const char*>( blob.GetData() ), blob.GetDataLen() );
690}
691
692
693bool KIGIT::IsOfferableSSHKey( const wxString& aKey )
694{
695 return !aKey.IsEmpty() && wxFileName::FileExists( aKey ) && !EnsurePublicKeyFile( aKey ).IsEmpty();
696}
697
698
699wxString KIGIT::PublicKeyPath( const wxString& aKey )
700{
701 if( aKey.IsEmpty() )
702 return wxEmptyString;
703
704 if( wxFileName::FileExists( aKey + wxS( ".pub" ) ) )
705 return aKey + wxS( ".pub" );
706
707 const wxString derived = derivedPublicKeyPath( aKey );
708
709 return !derived.IsEmpty() && wxFileName::FileExists( derived ) ? derived : wxString();
710}
711
712
713wxString KIGIT::EnsurePublicKeyFile( const wxString& aKey )
714{
715 if( aKey.IsEmpty() || wxFileName::FileExists( aKey + wxS( ".pub" ) ) )
716 return PublicKeyPath( aKey );
717
718 const wxString derived = derivedPublicKeyPath( aKey );
719 std::ifstream file( aKey.fn_str(), std::ios::binary );
720
721 if( derived.IsEmpty() || !file )
722 return wxEmptyString;
723
724 const std::string line = openSSHPublicKeyLine(
725 std::string( ( std::istreambuf_iterator<char>( file ) ), std::istreambuf_iterator<char>() ) );
726
727 if( line.empty() )
728 return wxEmptyString;
729
730 // Rewritten when it differs, so a key regenerated at the same path does not keep the old public half
731 std::ifstream old( derived.fn_str(), std::ios::binary );
732 const std::string current( ( std::istreambuf_iterator<char>( old ) ), std::istreambuf_iterator<char>() );
733
734 if( current == line )
735 return derived;
736
737 wxFileName fn( derived );
738
739 if( !fn.DirExists() && !fn.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
740 return wxEmptyString;
741
742 wxTempFile out( derived );
743
744 if( out.IsOpened() && out.Write( wxString::FromUTF8( line ), wxConvUTF8 ) && out.Commit() )
745 return derived;
746
747 wxLogTrace( traceGit, "Could not write the public key for %s to %s", aKey, derived );
748 return wxEmptyString;
749}
750
751
752void KIGIT::SetDerivedPublicKeyFolder( const wxString& aDir )
753{
754 DERIVED_PUBLIC_KEYS& store = derivedPublicKeys();
755 std::lock_guard<std::mutex> lock( store.mutex );
756 store.folder = aDir;
757}
758
759
760wxString KIGIT::RememberedSSHKey( const wxString& aRemote )
761{
762 REMEMBERED_KEYS& store = rememberedKeys();
763 std::lock_guard<std::mutex> lock( store.mutex );
764 loadRememberedKeys( store );
765
766 auto it = store.keys.find( aRemote );
767 return it == store.keys.end() ? wxString() : it->second;
768}
769
770
771void KIGIT::RememberSSHKey( const wxString& aRemote, const wxString& aKey )
772{
773 if( aRemote.IsEmpty() || aKey.IsEmpty() )
774 return;
775
776 REMEMBERED_KEYS& store = rememberedKeys();
777 std::lock_guard<std::mutex> lock( store.mutex );
778 loadRememberedKeys( store );
779
780 auto it = store.keys.find( aRemote );
781
782 if( it != store.keys.end() && IsSameSSHKey( it->second, aKey ) && store.pending.empty() )
783 return;
784
785 store.keys[aRemote] = aKey;
786 store.pending[aRemote] = aKey;
787
788 if( store.path.IsEmpty() )
789 return;
790
791 // Another KiCad may have saved since this one loaded, so merge into what is on disk now
792 std::map<wxString, wxString> merged = readRememberedKeys( store.path );
793
794 for( const auto& [remote, key] : store.pending )
795 merged[remote] = key;
796
797 if( writeRememberedKeys( store.path, merged ) )
798 {
799 store.keys = std::move( merged );
800 store.pending.clear();
801 }
802 else
803 {
804 wxLogTrace( traceGit, "Could not save remembered SSH keys to %s", store.path );
805 }
806}
807
808
809void KIGIT::SetRememberedSSHKeyStore( const wxString& aPath )
810{
811 REMEMBERED_KEYS& store = rememberedKeys();
812 std::lock_guard<std::mutex> lock( store.mutex );
813 store.path = aPath;
814 store.loaded = false;
815 store.keys.clear();
816 store.pending.clear();
817}
const char * name
nlohmann::json json
Definition gerbview.cpp:50
const wxChar *const traceGit
Flag to enable Git debugging output.
APIEXPORT wxString PrivateKeyPath(const wxString &aPicked)
The private key for a path picked in a file dialog, which may be the key's .pub file.
APIEXPORT bool IsOfferableSSHKey(const wxString &aKey)
Whether aKey can be offered to a server.
APIEXPORT wxString EnsurePublicKeyFile(const wxString &aKey)
PublicKeyPath for aKey, first writing a public key file to KiCad's cache when the key has no ....
APIEXPORT wxString RememberedSSHKey(const wxString &aRemote)
The key that last connected to aRemote, or empty.
APIEXPORT std::vector< wxString > ParseSSHCommandIdentityFiles(const wxString &aCommand, const wxString &aHomeDir)
The -i key files in an ssh command line such as git's core.sshCommand, in order.
APIEXPORT std::vector< wxString > ParseSSHConfigIdentityFiles(const wxString &aConfigText, const wxString &aHost, const wxString &aHomeDir)
The IdentityFile entries an ssh config gives aHost, in file order.
APIEXPORT wxString PublicKeyPath(const wxString &aKey)
The public key file to offer with aKey.
APIEXPORT void SetDerivedPublicKeyFolder(const wxString &aDir)
Keep public key files written by EnsurePublicKeyFile in aDir.
APIEXPORT std::vector< wxString > DefaultSSHKeyCandidates(const wxString &aHomeDir, const wxString &aHost, const wxString &aSSHCommand=wxEmptyString)
Private keys to offer aHost, best first.
APIEXPORT wxString HomeDir()
The user's home folder, where ssh looks for its config and default keys: the first candidate with a ....
APIEXPORT wxString SSHKeyFolder()
The folder ssh keeps keys in, for a file picker to open at. Empty when the home folder is unknown.
APIEXPORT void RememberSSHKey(const wxString &aRemote, const wxString &aKey)
Remember aKey as the key that connected to aRemote. Safe to call from any thread.
APIEXPORT std::vector< wxString > HomeDirCandidates()
The folders that may be the user's home, most likely first.
APIEXPORT void SetRememberedSSHKeyStore(const wxString &aPath)
Keep remembered keys in aPath.
APIEXPORT std::string PublicKeyBlob(const wxString &aKey)
The public key blob from aKey's public key file, as the server sees it, or empty when it cannot be re...
APIEXPORT bool IsSameSSHKey(const wxString &aKey, const wxString &aOther)
Whether two key paths name the same file.
CITER next(CITER it)
Definition ptree.cpp:120
static std::string decoded(const std::string &aBase64)
std::string path
static std::vector< int > candidates(const SEGMENT_INDEX &aIndex, const SEG &aQuery, int aPadding)
VECTOR2I end
wxLogTrace helper definitions.