-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdentityManager.cpp
More file actions
935 lines (775 loc) · 23.1 KB
/
Copy pathIdentityManager.cpp
File metadata and controls
935 lines (775 loc) · 23.1 KB
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
#include "IdentityManager.h"
#include "MailManager.h"
#include <ext/stdio_filebuf.h>
#include <libutils/FileUtils.h>
#include <libutils/Logger.h>
#include <libutils/String.h>
#include <libutils/Process.h>
#include <libutils/HttpStatusCodes.h>
#include <libopi/ServiceHelper.h>
#include <libopi/HostsConfig.h>
#include <libopi/AuthServer.h>
#include <libopi/DnsServer.h>
#include <libopi/SysConfig.h>
#include <libopi/Secop.h>
#include <unistd.h>
#include <utility>
using namespace Utils;
using namespace OPI;
using namespace Utils::HTTP;
#define SCFG (OPI::SysConfig())
namespace KGP
{
IdentityManager::IdentityManager(): tmpfilename("")
{
}
IdentityManager &IdentityManager::Instance()
{
static IdentityManager mgr;
return mgr;
}
bool IdentityManager::SetFqdn(const string &name, const string &domain)
{
string oldFqdn = this->GetFqdnAsString();
MailManager& mailmgr = MailManager::Instance();
try
{
if( sethostname(name.c_str(), name.length()) == -1)
{
this->global_error = string("Failed to set hostname:") + strerror(errno);
return false;
}
if( setdomainname(domain.c_str(), domain.length()) == -1)
{
this->global_error = string("Failed to set domainname:") + strerror(errno);
return false;
}
OPI::SysConfig cfg(true);
this->hostname = name;
this->domain = domain;
cfg.PutKey("hostinfo","hostname", name);
cfg.PutKey("hostinfo","domain", domain);
// Update mail hostname
if( ! mailmgr.SetHostname(name, domain) )
{
this->global_error = mailmgr.StrError();
return false;
}
// Update /etc/hosts
OPI::HostsConfig hosts;
// Check if we have old host
HostEntryPtr host = hosts.GetEntryByAddress("127.0.1.1");
if( host )
{
hosts.DeleteEntry(host);
}
hosts.AddEntry("127.0.1.1", name+"."+domain, {name});
hosts.WriteBack();
// Update system hostname
File::Write("/etc/hostname", this->hostname, File::UserRW | File::GroupRead | File::OtherRead);
}
catch (std::runtime_error& err)
{
logg << Logger::Error << "Failed to set hostname :" << err.what() << lend;
this->global_error = string("Failed to set hostname :") + err.what();
return false;
}
try
{
string newfqdn = this->GetFqdnAsString();
if( oldFqdn == newfqdn )
{
logg << Logger::Debug << "New fqdn same as old one, not changing (" << newfqdn << ")" << lend;
}
else if( oldFqdn != "" )
{
logg << Logger::Debug << "Update MailConfig" << lend;
mailmgr.ChangeDomain(oldFqdn, newfqdn);
}
}
catch (std::runtime_error& err)
{
this->global_error = string("Failed change mailhostname: ") + err.what();
logg << Logger::Error << this->global_error << lend;
return false;
}
return true;
}
string IdentityManager::GetHostname()
{
if( this->hostname == "" )
{
// Get hostname from sysconfig
if( SCFG.HasKey("hostinfo", "hostname") )
{
this->hostname = SCFG.GetKeyAsString("hostinfo", "hostname");
}
}
return this->hostname;
}
string IdentityManager::GetDomain()
{
if( this->domain == "" )
{
// Get domain from sysconfig
if( SCFG.HasKey("hostinfo", "domain") )
{
this->domain = SCFG.GetKeyAsString("hostinfo", "domain");
}
}
return this->domain;
}
tuple<string, string> IdentityManager::GetFqdn()
{
OPI::SysConfig cfg;
string host, domain;
if( cfg.HasKey("hostinfo", "hostname") && cfg.HasKey("hostinfo", "domain") )
{
host = cfg.GetKeyAsString("hostinfo", "hostname");
domain = cfg.GetKeyAsString("hostinfo", "domain");
}
return make_tuple(host, domain);
}
string IdentityManager::GetFqdnAsString()
{
// GetHostname reads and sets this->hostname if necessary
if (this->GetHostname() != "" && this->GetDomain() != "")
{
return this->hostname + "." + domain;
}
return "";
}
tuple<bool,string> IdentityManager::WriteCustomCertificate(const string &key, const string &cert)
{
SysConfig sysconfig(true);
string CustomCertFile,CustomKeyFile,CustomCertPath;
string webcert = sysconfig.GetKeyAsString("webcertificate","activecert");
string webkey = sysconfig.GetKeyAsString("webcertificate","activekey");
// Generate new filenames
logg << Logger::Debug << "Generating new custom cert paths" << lend;
CustomCertPath = "/etc/opi/usercert/";
CustomCertFile = CustomCertPath + "cert.pem";
CustomKeyFile = CustomCertPath + "privkey.pem";
// check that the path exists.
if ( ! File::DirExists(CustomCertPath) )
{
// create paths.
try
{
File::MkPath(CustomCertPath, File::UserRWX);
}
catch (Utils::ErrnoException& err)
{
logg << Logger::Error << "Failed to create path for usercerts:" << err.what() << lend;
this->global_error = string("Failed to create path for usercerts:") + err.what();
return make_tuple(false,"Failed to create path for usercerts:");
}
}
try {
// Write content to userCert files
File::Write(CustomCertFile,cert, File::UserRW | File::GroupRead | File::OtherRead);
if ( key != "" ) {
File::Write(CustomKeyFile,key,File::UserRW | File::GroupRead | File::OtherRead);
}
else
{
// if no key provided, use the existing key
// if just the device name is updated, this function is triggered anyway
// but the "key" will be empty
CustomKeyFile = File::RealPath(webkey);
}
}
catch (std::runtime_error& err)
{
this->global_error = string("Filed to write custom certificate files: ") +err.what();
return make_tuple(false, this->global_error);
}
// create a backup copy of the cert symlinks nginx uses
string curr_key,curr_cert;
curr_key = File::RealPath(webkey);
curr_cert = File::RealPath(webcert);
File::Delete(webcert);
File::Delete(webkey);
ignore = symlink(CustomCertFile.c_str(),webcert.c_str());
ignore = symlink(CustomKeyFile.c_str(),webkey.c_str());
// new links should now be in place, let nginx test the config
int retval =0;
string Message;
tie(retval,Message)=Process::Exec( "nginx -t" );
if ( ! retval )
{
// nginx config test failed, restore old links
logg << Logger::Debug << "Nginx config test failed" << lend;
File::Delete(webcert);
File::Delete(webkey);
ignore = symlink(curr_cert.c_str(),webcert.c_str());
ignore = symlink(curr_key.c_str(),webkey.c_str());
logg << Logger::Error << "Webserver config test failed with new certificates" << lend;
return make_tuple(false,"Webserver config test failed with new certificates");
}
return make_tuple(true,"");
}
bool IdentityManager::CreateCertificate()
{
if ( SCFG.HasKey("webcertificate","backend") )
{
return this->CreateCertificate(true,SCFG.GetKeyAsString("webcertificate","backend"));
}
return false;
}
bool IdentityManager::CreateCertificate(bool forceProvider, const string &certtype)
{
OPI::SysConfig cfg(true);
string fqdn = this->GetHostname() + "." + this->GetDomain();
string provider;
cfg.PutKey("webcertificate","backend",certtype);
if ( forceProvider )
{
try
{
// Always genereate self signed web server certificate, used as long as no LE-cert or similar present
if( ! OPI::CryptoHelper::MakeSelfSignedCert(
cfg.GetKeyAsString("dns","dnsauthkey"),
cfg.GetKeyAsString("webcertificate","defaultcert"),
fqdn,
cfg.GetKeyAsString("hostinfo","hostname")
) )
{
logg << Logger::Error << "Failed to create self signed server certificate" << lend;
return false;
}
// Also use this cert as the default OPI-cert. This will be replaced below if we have
// a valid provider.
string syscert = SCFG.GetKeyAsString("hostinfo","syscert");
unlink( syscert.c_str() );
if( symlink( cfg.GetKeyAsString("webcertificate","defaultcert").c_str(), syscert.c_str() ) < 0)
{
logg << Logger::Error << "Failed to symlink selfsigned cert" << lend;
}
// generate certificate for provider, or if no provider generate self signed certificate
if ( this->HasDnsProvider() )
{
provider = cfg.GetKeyAsString("dns","provider");
if ( provider == "OpenProducts" )
{
provider = "OPI"; // legacy, provider shall be OPI for OpenProducts certificates.
}
if( this->IsEnabledDNS() )
{
// Get a certificate for the supported DNS-provider
list<string> domains = this->DnsAvailableDomains();
if( std::find(domains.begin(), domains.end(), this->domain) != domains.end() )
{
if( !this->GetCertificate(fqdn, provider) )
{
logg << Logger::Error << "Failed to get certificate for device name: "<<fqdn<<lend;
return false;
}
}
else
{
logg << Logger::Debug << "Domain " << this->domain << " not supported in backend " <<lend;
}
}
else
{
// We seem to have a custom domain but still use a mail service
// TODO: This is a workaround, maybe should be implemented in mailmanager?
logg << Logger::Debug << "Try get a mail-relay cert only" << lend;
if( !this->GetCertificate(this->GetHostname()+".mailrelay", provider) )
{
logg << Logger::Error << "Failed to get certificate for device name: "<<fqdn<<lend;
return false;
}
}
}
}
catch (std::runtime_error& err)
{
logg << Logger::Error << "Failed to generate certificate:" << err.what() << lend;
this->global_error = string("Failed to generate certificate:") + err.what();
return false;
}
}
if( certtype != "CUSTOMCERT" )
{
logg << Logger::Debug << "Get signed Certificate for '"<< fqdn <<"'"<<lend;
if( ! this->GetSignedCert(fqdn) )
{
// The call forks a new process and can not really fail, but the generation of the certificate
// can fail. But that can not be indicted here...
logg << Logger::Notice << "Failed to get launch thread to get signed cert."<<lend;
}
}
else
{
logg << Logger::Debug << "Custom certficate, not trying to sign" << lend;
}
return true;
}
bool IdentityManager::DnsNameAvailable(const string &hostname, const string &domain)
{
OPI::DnsServer dns;
int result_code = 0;
json ret;
tie(result_code, ret) = dns.CheckOPIName( hostname +"."+ domain );
if( result_code != Status::Ok && result_code != Status::Forbidden )
{
logg << Logger::Notice << "Request for DNS check name failed" << lend;
return false;
}
return result_code == Status::Ok;
}
bool IdentityManager::DnsDomainAvailable(const string &domain)
{
if ( ! SCFG.HasKey("dns","availabledomains") )
{
return false;
}
list<string> domains = SCFG.GetKeyAsStringList("dns","availabledomains");
for(const auto& d: domains)
{
if ( domain == d )
{
return true;
}
}
return false;
}
bool IdentityManager::AddDnsName(const string &hostname, const string &domain)
{
logg << Logger::Debug << "Add DNS name" << lend;
if( ! this->CheckUnitID() )
{
logg << Logger::Error << "No unitid provided" << lend;
this->global_error = "No unitid provided to IdentityManager";
return false;
}
string fqdn = hostname +"."+domain;
DnsServer dns;
if( ! dns.UpdateDynDNS(this->unitid, fqdn) )
{
logg << Logger::Error << "Failed to update Dyndns ("<< this->unitid << ") ("<< fqdn <<")"<<lend;
this->global_error = "Failed to update DynDNS";
return false;
}
return true;
}
tuple<string, string> IdentityManager::GetCurrentDnsName()
{
// Currently if we have a dns-name it is the same as the hostname.
if( this->HasDnsProvider() )
{
return this->GetFqdn();
}
return make_tuple("","");
}
bool IdentityManager::HasDnsProvider()
{
return ( SCFG.HasKey("dns", "provider") && SCFG.GetKeyAsString("dns", "provider") == "OpenProducts" );
}
bool IdentityManager::EnableDnsProvider(const string &provider)
{
// Currently only OP supported
if( provider != "OpenProducts" )
{
logg << Logger::Notice << "DNS provider " << provider << " not supported"<<lend;
return false;
}
// We need a writable config
OPI::SysConfig cfg(true);
cfg.PutKey("dns", "provider", "OpenProducts");
list<string> domains({"mykeep.net", "op-i.me"});
cfg.PutKey("dns", "availabledomains", domains);
return true;
}
list<string> IdentityManager::DnsAvailableDomains()
{
list<string> domains;
if( SCFG.HasKey("dns","availabledomains") )
{
domains = SCFG.GetKeyAsStringList("dns","availabledomains");
}
return domains;
}
bool IdentityManager::DisableDNS()
{
try {
OPI::SysConfig cfg(true);
cfg.PutKey("dns","enabled", false);
}
catch (std::runtime_error& err)
{
logg << Logger::Error << "Failed to disable dns provider:" << err.what() << lend;
this->global_error = string("Failed to disable dns provider:") + err.what();
return false;
}
return true;
}
bool IdentityManager::IsEnabledDNS()
{
OPI::SysConfig cfg;
if( cfg.HasScope("dns") && cfg.HasKey("dns","enabled") )
{
return cfg.GetKeyAsBool("dns", "enabled");
}
return false;
}
bool IdentityManager::EnableDNS()
{
if ( SCFG.HasKey("dns", "provider") )
{
return this->SetDNSProvider(SCFG.GetKeyAsString("dns", "provider"));
}
else
{
logg << Logger::Error << "Failed to enable dns provider (missing in config)" << lend;
this->global_error = string("Failed to enable dns provider (missing in config)");
return false;
}
}
bool IdentityManager::SetDNSProvider(const string &provider)
{
// set and enable dns provider
try {
OPI::SysConfig cfg(true);
cfg.PutKey("dns","provider", provider);
cfg.PutKey("dns","enabled", true);
}
catch (std::runtime_error& err)
{
logg << Logger::Error << "Failed to set dns provider:" << err.what() << lend;
this->global_error = string("Failed to set dns provider:") + err.what();
return false;
}
return true;
}
bool IdentityManager::RegisterKeys() {
logg << Logger::Debug << "Register keys"<<lend;
try{
// If OP device
if( this->HasDnsProvider() )
{
// Generate sysauthkeys and register with secop if not present
AuthServer::Setup();
string sysauthkey = SCFG.GetKeyAsString("hostinfo","sysauthkey");
string syspubkey = SCFG.GetKeyAsString("hostinfo","syspubkey");
logg << Logger::Debug << "Delete (if existing) old private key"<<lend;
// sysauth keys shall no longer exist on file.
if ( File::FileExists(sysauthkey)) {
File::Delete(sysauthkey);
}
if ( File::FileExists(syspubkey)) {
File::Delete(syspubkey);
}
}
// Create "dns"-key
logg << Logger::Debug << "Checking dns-key, possibly recreating" << lend;
string dnsauthkey = SCFG.GetKeyAsString("dns","dnsauthkey");
string dnspubkey = SCFG.GetKeyAsString("dns","dnspubkey");
string priv_path = File::GetPath( dnsauthkey );
if( ! File::DirExists( priv_path ) )
{
File::MkPath( priv_path, File::UserRWX | File::GroupRX | File::OtherRX);
}
string pub_path = File::GetPath( dnspubkey );
if( ! File::DirExists( pub_path ) )
{
File::MkPath( pub_path, File::UserRWX | File::GroupRX | File::OtherRX);
}
if( ! File::FileExists( dnsauthkey) || ! File::FileExists( dnspubkey ) )
{
logg << Logger::Notice << "Recreating dns key" << lend;
RSAWrapper dns;
dns.GenerateKeys();
if ( File::FileExists(dnsauthkey)) {
string olddnsauthkey = File::GetContentAsString( dnsauthkey,true );
File::Write(dnsauthkey+".old",olddnsauthkey, File::UserRW );
}
if ( File::FileExists(dnspubkey)) {
string olddnspubkey = File::GetContentAsString(dnspubkey,true );
File::Write(dnspubkey+".old",olddnspubkey, File::UserRW | File::GroupRead | File::OtherRead );
}
// Make sure we have no keys before writing new ones
try
{
File::Delete( dnsauthkey );
}
catch (std::runtime_error &err)
{
// Ok if file does not exist
logg << Logger::Debug << "Unable to delete: " << dnsauthkey << " : " << err.what()<< lend;
}
try
{
File::Delete( dnspubkey );
}
catch (std::runtime_error &err)
{
// Ok if file does not exist
logg << Logger::Debug << "Unable to delete: " << dnspubkey << " : " << err.what()<< lend;
}
File::Write(dnsauthkey, dns.PrivKeyAsPEM(), File::UserRW );
File::Write(dnspubkey, dns.PubKeyAsPEM(), File::UserRW | File::GroupRead | File::OtherRead );
}
}
catch( runtime_error& err)
{
this->global_error = string("Failed to register keys: ") + err.what();
logg << Logger::Notice << this->global_error << lend;
return false;
}
return true;
}
tuple<bool,string> IdentityManager::UploadKeys(const string &unitid, const string &mpwd)
{
AuthServer s(unitid);
int resultcode = 0;
string token;
json ret;
tie(resultcode, ret) = s.Login();
logg << Logger::Debug << "Login resultcode from server: " << resultcode <<lend;
if( resultcode != Status::Ok && resultcode != Status::Forbidden && resultcode != Status::ServiceUnavailable)
{
logg << Logger::Error << "Unexpected reply from server "<< resultcode <<lend;
return make_tuple(false,"");
}
if( resultcode == Status::ServiceUnavailable )
{
// keys are missing in secop, register new keys in secop.
if ( ! this->RegisterKeys() )
{
logg << Logger::Error << "Failed to register keys"<< resultcode <<lend;
return make_tuple(false,"");
}
// try to login again
tie(resultcode, ret) = s.Login();
logg << Logger::Debug << "Retry Login resultcode from server: " << resultcode <<lend;
if( resultcode != Status::Ok && resultcode != Status::Forbidden )
{
logg << Logger::Error << "Unexpected reply from server "<< resultcode <<lend;
return make_tuple(false,"");
}
}
if( resultcode == Status::Forbidden || resultcode == Status::ServiceUnavailable )
{
logg << Logger::Debug << "Send Secret"<<lend;
if( ! ret.contains("reply") || ! ret["reply"].contains("challange") )
{
logg << Logger::Error << "Missing argument from server "<< resultcode <<lend;
return make_tuple(false,"");
}
// Got new challenge to encrypt with master
string challenge = ret["reply"]["challange"].get<string>();
RSAWrapperPtr c = AuthServer::GetKeysFromSecop();
SecVector<byte> key = PBKDF2(SecString(mpwd.c_str(), mpwd.size() ), 32 );
AESWrapper aes( key );
string cryptchal = Base64Encode( aes.Encrypt( challenge ) );
tie(resultcode, ret) = s.SendSecret(cryptchal, Base64Encode(c->PubKeyAsPEM()) );
if( resultcode != Status::Ok )
{
if( resultcode == Status::Forbidden)
{
logg << Logger::Debug << "Access denied to OP servers"<<lend;
return make_tuple(false,"");
}
else
{
logg << Logger::Debug << "Failed to communicate with OP server"<<lend;
return make_tuple(false,"");
}
}
if( ret.contains("token") && ret["token"].is_string() )
{
token = ret["token"].get<string>();
if (! this->UploadDnsKey(unitid,token))
{
logg << Logger::Error << "Failed to upload DNS key"<<lend;
return make_tuple(false,"");
}
}
else
{
logg << Logger::Error << "Missing argument in reply"<<lend;
return make_tuple(false,"");
}
}
else
{
if( ret.contains("token") && ret["token"].is_string() )
{
token = ret["token"].get<string>();
if (! this->UploadDnsKey(unitid,token))
{
logg << Logger::Error << "Failed to upload DNS key"<<lend;
return make_tuple(false,"");
}
}
else
{
logg << Logger::Error << "Missing argument in reply"<<lend;
return make_tuple(false,"");
}
}
return make_tuple(true,token);
}
void IdentityManager::CleanUp()
{
if( this->signerthread )
{
this->signerthread->Join();
}
}
bool IdentityManager::UploadDnsKey(const string& unitid, const string& token)
{
logg << Logger::Error<< "Received token from server: " << token <<lend;
// Try to upload dns-key
stringstream pk;
for( const auto& row: File::GetContent(SCFG.GetKeyAsString("dns","dnspubkey")) )
{
pk << row << "\n";
}
DnsServer dns;
string pubkey = Base64Encode( pk.str() );
if(! dns.RegisterPublicKey(unitid, pubkey, token ))
{
logg << Logger::Error<< "Failed to upload DNS key" <<lend;
return false;
}
return true;
}
bool IdentityManager::GetCertificate(const string &fqdn, const string &provider)
{
logg << Logger::Debug << "Request certificate for '" << fqdn <<"' from '" << provider << "'"<<lend;
/*
*
* This is a workaround for a bug in the authserver that loses our
* credentials when we login with dns-key
*
*/
if( ! this->OPLogin() )
{
return false;
}
string syscert = SCFG.GetKeyAsString("hostinfo","syscert");
string dnsauthkey = SCFG.GetKeyAsString("dns","dnsauthkey");
string csrfile = File::GetPath( syscert )+"/"+String::Split(fqdn,".",2).front()+".csr";
if( ! CryptoHelper::MakeCSR(dnsauthkey, csrfile, fqdn, provider) )
{
this->global_error = "Failed to make certificate signing request";
return false;
}
string csr = File::GetContentAsString(csrfile, true);
AuthServer s(this->unitid);
int resultcode = 0;
json ret;
tie(resultcode, ret) = s.GetCertificate(csr,this->token );
if( resultcode != Status::Ok )
{
logg << Logger::Error << "Failed to get csr "<<resultcode <<lend;
this->global_error = "Failed to get certificate from OP servers";
return false;
}
if( ! ret.contains("cert") || ! ret["cert"].is_string() )
{
logg << Logger::Error << "Malformed reply from server " <<lend;
this->global_error = "Unexpected reply from OP server when retrieving certificate";
return false;
}
// Make sure we have no symlinked tempcert in place
unlink( syscert.c_str() );
File::Write( syscert, ret["cert"].get<string>(), File::UserRW | File::GroupRead | File::OtherRead);
// TODO: this should be a generic certificate event that opi-mail should react to
if( ! OPI::ServiceHelper::Reload("postfix") )
{
logg << Logger::Error << "Failed to reload mailserver" << lend;
}
return true;
}
class SignerThread: public Utils::Thread
{
public:
SignerThread(string name): Thread(false), opiname(std::move(name)), result(true) {}
void Run() override;
bool Result();
~SignerThread() override;
private:
string opiname;
bool result;
};
void SignerThread::Run()
{
tie(this->result, ignore) = Process::Exec("/usr/share/kinguard-certhandler/letsencrypt.sh -ac");
}
bool SignerThread::Result()
{
// Only valid upon completed run
return this->result;
}
SignerThread::~SignerThread() = default;
bool IdentityManager::GetSignedCert(const string &fqdn)
{
try
{
logg << Logger::Debug << "Launching detached signer thread" << lend;
this->signerthread = ThreadPtr( new SignerThread(fqdn) );
this->signerthread->Start();
}
catch( std::runtime_error& err)
{
logg << Logger::Error << "Failed to launch signer thread: " << err.what() << lend;
return false;
}
return true;
}
bool IdentityManager::CheckUnitID()
{
if( this->unitid != "" )
{
// We have a unitid retrieved earlier
return true;
}
if( SCFG.HasKey("hostinfo", "unitid") )
{
this->unitid = SCFG.GetKeyAsString("hostinfo", "unitid");
}
return this->unitid != "";
}
bool IdentityManager::OPLogin()
{
logg << Logger::Debug << "Do OP login" << lend;
// Check again for unit-id that could have changed
this->CheckUnitID();
if( this->unitid.length() == 0 )
{
logg << Logger::Notice << "Missing UnitID when trying to do OP-Login" << lend;
return false;
}
AuthServer s( this->unitid);
int resultcode = 0;
json ret;
tie(resultcode, ret) = s.Login();
if( resultcode != Status::Ok && resultcode != Status::Forbidden )
{
logg << Logger::Error << "Unexpected reply from server "<< resultcode <<lend;
this->global_error ="Unexpected reply from OP server ("+ ret["desc"].get<string>()+")";
return false;
}
if( resultcode == Status::Forbidden)
{
this->global_error ="Failed to authenticate with OP server. Wrong activation code or password.";
return false;
}
if( ret.contains("token") && ret["token"].is_string() )
{
this->token = ret["token"].get<string>();
}
else
{
logg << Logger::Error << "Missing argument in reply"<<lend;
this->global_error ="Failed to communicate with OP server (Missing argument)";
return false;
}
return true;
}
} // Namespace KGP