Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Moved common/win32ssl.h include into the __WIN32__ if section in frmNewAccount.cpp
[xestiaab/.git] / source / frmNewAccount.cpp
1 // frmNewAccount.cpp - New Account form.
2 //
3 // (c) 2012-2015 Xestia Software Development.
4 //
5 // This file is part of Xestia Address Book.
6 //
7 // Xestia Address Book is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
10 //
11 // Xestia Address Book 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 along
17 // with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>
19 #include "frmNewAccount.h"
20 #include <thread>
21 #include <cstdlib>
22 #include <wx/filefn.h>
23 #include <wx/fileconf.h>
25 #if defined(__WIN32__)
26 #include <cryptuiapi.h>
27 #include "common/win32ssl.h"
28 #endif
30 #include "carddav/carddav.h"
31 #include "common/dirs.h"
32 #include "common/svrblist.h"
33 #include "frmInvalidSSLCertificate.h"
35 frmNewAccount::frmNewAccount( wxWindow* parent )
36 :
37 frmNewAccountADT( parent )
38 {
40         // Disable the previous button upon form creation.
42         btnPrevious->Disable();
43         
44 }
46 void frmNewAccount::CheckAccountName( wxCommandEvent& event )
47 {
48     
49         // Check that the account name is valid.
50     
51         wxString CheckAccName = txtAccountName->GetValue();
52     
53         if ((txtAccountName->IsEmpty() && PageSeek == 2) || CheckAccName.Len() < 4){
54         
55                 btnNext->Disable();
56         
57         } else {
58         
59                 btnNext->Enable();
60         
61         }
62     
63 }
65 void frmNewAccount::ProcessPrevious( wxCommandEvent& event )
66 {
68         // Go to the previous page.
70         PageSeek--;
71     
72         if (PageSeek == 0){
73         
74                 // Currently at the Connection test screen.
75         
76                 tabConn->Hide();
77                 tabFinish->Hide();
78                 tabType->Show();
79                 szrNewAccount->RecalcSizes();
80         
81                 btnPrevious->Disable();
82                 btnNext->Enable();
83         
84         } else if (PageSeek == 1){
85         
86                 if (cmbServerType->GetCurrentSelection() == 0){
87             
88                         tabConn->Hide();
89                         tabFinish->Hide();
90                         tabType->Show();
91                         PageSeek = 0;
92                         btnPrevious->Disable();
93                         btnNext->Enable();
94                         btnNext->SetLabel(_("Next >"));
95                         return;
96             
97                 }
98         
99                 // Currently at the Finish screen.
100         
101                 tabType->Hide();
102                 tabConn->Show();
103                 tabFinish->Hide();
104                 szrNewAccount->RecalcSizes();
105         
106                 btnNext->SetLabel(_("Next >"));
107                 btnNext->Enable();
108         
109         }
110     
113 void frmNewAccount::ProcessNext( wxCommandEvent& event )
116         // Go to the next page or setup the new account.
118         PageSeek++;
119     
120         if (PageSeek == 1){
121         
122                 if (cmbServerType->GetCurrentSelection() == 0){
123             
124                         tabType->Hide();
125                         tabConn->Hide();
126                         tabFinish->Show();
127                         PageSeek = 2;
128                         btnPrevious->Enable();
129                         szrNewAccount->RecalcSizes();
130                         btnNext->Disable();
131                         btnNext->SetLabel(_("Finish"));
132                         return;
133             
134                 }
135         
136                 // Check if server address matches against the blacklist.
137                 // Bring up warning message if it does.
138                 
139                 if (CheckBlacklist(txtServerAddress->GetValue())){
140                 
141                         int MessageBoxResult = wxMessageBox(_("The server with the address given does not support the CardDAV protocol properly and shouldn't be used.\n\nData loss is very likely.\n\nDo you still want to continue using this server?"), _("Server warning"), wxYES_NO, this);
142                 
143                         if (MessageBoxResult == wxNO){
144                                 PageSeek--;
145                                 return;
146                         }
147                         
148                 }
149                 
150                 btnNext->Disable();
151         
152                 bool ServerResult = FALSE;
153                 bool ServerAction = FALSE;
154                 bool UseSSL = TRUE;
155                 wxString ServerMessage;
156         
157                 // Connection test screen.
158         
159                 tabType->Hide();
160                 tabConn->Show();
161                 tabFinish->Hide();
162                 szrNewAccount->RecalcSizes();
163                 btnPrevious->Enable();
164         
165                 // Reset screen.
166         
167                 lblServerConnResult->SetLabel(wxT(""));
168                 lblServerResponse->SetLabel(wxT(""));
169                 lblServerSSLResult->SetLabel(wxT(""));
170                 lblServerSSLValid->SetLabel(wxT(""));
171                 lblAbleToLoginResult->SetLabel(wxT(""));
172                 lblCardDAVSupportResult->SetLabel(wxT(""));
173         
174                 // Spawn a thread and check if server supports CardDAV.
175         
176                 CardDAV CardDAVConn;
177         
178                 lblServerConnResult->SetLabel(_("Testing..."));
179         
180                 UseSSL = chkUseSSL->GetValue();
181                 CardDAVConn.SetupConnection(txtServerAddress->GetValue(),
182                         wxAtoi(txtServerPort->GetValue()),
183                         txtUsername->GetValue(),
184                         txtPassword->GetValue(),
185                         UseSSL);
186                 CardDAVConn.SetupResultBools(&ServerResult, &ServerAction);
187         
188 #if defined(__WIN32__)
190                 BOOL ModifiedCertificateData = FALSE;
192 #endif
194                 // Verify SSL trust first before doing anything.
196                 if (UseSSL == TRUE){
198                         CURLcode sslcode = CardDAVConn.SSLVerifyTest();
200                         if (sslcode == CURLE_OK){
201                                 
204                         } else if (sslcode == CURLE_SSL_CACERT || sslcode == CURLE_SSL_CONNECT_ERROR){
206                                 // Certificate is more than likely a self-signed or
207                                 // expired certificate so display the invalid
208                                 // SSL certificate message.
210                                 // Setup the data to be sent in the wxPostEvent command.
211                                 
212                                 int SSLResult;
213                                 
214 #if defined(__APPLE__)
215         
216                                 SSLResult = DisplayTrustPanel(&CardDAVConn);
218                                 if (SSLResult != NSOKButton){
219                                         
220                                         lblServerConnResult->SetLabel(_("Failed"));
221                                         lblServerResponse->SetLabel(_("Not applicable"));
222                                         lblServerSSLResult->SetLabel(_("Used"));
223                                         lblServerSSLValid->SetLabel(_("No"));
224                                         lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
225                                         return;
226                                         
227                                 } else {
228                                         
229                                         // Evalulate the trust object.
230                                         
231                                         SecTrustResultType EvalResult = ProcessResultType(&CardDAVConn);
232                                         
233                                         switch(EvalResult){
234                                                 case kSecTrustResultProceed:
235                                                         lblServerSSLValid->SetLabel(_("Verified"));
236                                                         break;
237                                                 case kSecTrustResultConfirm:
238                                                         lblServerSSLValid->SetLabel(_("Verified (user)"));
239                                                         break;
240                                                 default:
241                                                         lblServerSSLValid->SetLabel(_("Unable to verify"));
242                                         }
243                                         
244                                         lblServerResponse->SetLabel(_("Not applicable"));
245                                         lblServerSSLResult->SetLabel(_("Used"));
246                                         
247                                         if (EvalResult != kSecTrustResultProceed){
248                                                 return;
249                                         }
250                                         
251                                 }
253 #elif defined(__WIN32__)
255                                 CRYPTUI_VIEWCERTIFICATE_STRUCTW CertificateData = BuildCertificateData(&CardDAVConn, (HWND)this->GetHandle());
257                                 if (!CryptUIDlgViewCertificate(&CertificateData, &ModifiedCertificateData)){
258                                         wxMessageBox(_("An error occured while trying to open the certificate dialog."), _("Error opening Certificate Information dialog"));
259                                 }
261                                 if (ModifiedCertificateData == FALSE){
263                                         lblServerConnResult->SetLabel(_("Failed"));
264                                         lblServerResponse->SetLabel(_("Not applicable"));
265                                         lblServerSSLResult->SetLabel(_("Used"));
266                                         lblServerSSLValid->SetLabel(_("No"));
267                                         lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
268                                         return;
270                                 }
272 #else
273                         
274                                 frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
276                                 frmICPtr->LoadDataNew(CardDAVConn.GetSSLVerifyResults(), txtServerAddress->GetValue());
277                                 frmICPtr->ShowModal();
278                                                                 
279                                 SSLResult = frmICPtr->GetResult();
280                                                         
281                                 // Clean up before processing response.
282                                 
283                                 delete frmICPtr;
284                                 frmICPtr = NULL;
285                                                         
286                                 // Process the response from the user.
287                                                         
288                                 if (SSLResult == 1){
289                                                                 
290                                         // Accept the Certificate.
292                                         CardDAVConn.AllowSelfSignTest(TRUE);
293                                                                 
294                                 } else if (SSLResult == 2){
295                                                                 
296                                         // Reject the certificate, abort the task and mark as failed.
298                                         lblServerConnResult->SetLabel(_("Failed"));
299                                         lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
300                                         return;
301                                                                 
302                                 }
303                                 
304 #endif
306                         } else {
308                                 // Something else happened. Stop the process and
309                                 // display an error message instead.
311                                 CURLcode sslcode = CardDAVConn.SSLVerifyTest();
312                                 
313                                 lblServerConnResult->SetLabel(_("Failed"));
314                                 lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
315                                 return;
317                         }
319                 }
321                 std::thread ConnTest(&CardDAV::Connect, &CardDAVConn);
322         
323                 ConnTest.join();
325 #if defined(__WIN32__)
326                 if (ServerResult == FALSE && ModifiedCertificateData == FALSE){
327 #else
328                 if (ServerResult == FALSE){
329 #endif
330             
331                         lblServerConnResult->SetLabel(_("Failed"));
332                         lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage());
333                         return;
334                         
335                 } else {
336             
337                         lblServerConnResult->SetLabel(_("Connected"));
338             
339                 }
341                 if (CardDAVConn.CanDoSSL() == TRUE){
343                         lblServerSSLResult->SetLabel(_("Used"));
345                 } else {
346             
347                         lblServerSSLResult->SetLabel(_("Not Used"));
348                         lblServerSSLValid->SetLabel(_("Not Applicable"));
349             
350                 }
351                 
352 #if defined(__APPLE__)
354                 // Evalulate the trust object.
355                 
356                 SecTrustResultType EvalResult = ProcessResultType(&CardDAVConn);
357                 
358                 switch(EvalResult){
359                         case kSecTrustResultProceed:
360                                 lblServerSSLValid->SetLabel(_("Verified"));
361                                 break;
362                         case kSecTrustResultConfirm:
363                                 lblServerSSLValid->SetLabel(_("Verified (user)"));
364                                 break;
365                         default:
366                                 lblServerSSLValid->SetLabel(_("Unable to verify"));
367                 }
369 #elif defined(__WIN32__)
371                 if (ModifiedCertificateData == TRUE){
372                         lblServerSSLValid->SetLabel(_("Verified (user)"));
373                 } else {
374                         lblServerSSLValid->SetLabel(_("Verified"));
375                 }
377 #else
378                 
379                 if (CardDAVConn.SSLVerify() == TRUE && CardDAVConn.CanDoSSL() == TRUE){
380             
381                         lblServerSSLValid->SetLabel(_("Verified"));
382             
383                 } else if (CardDAVConn.SSLVerify() == FALSE && CardDAVConn.CanDoSSL() == TRUE && CardDAVConn.IsSelfSigned() == TRUE){
384         
385                         lblServerSSLValid->SetLabel(_("Verified (user)"));
386         
387                 } else if (CardDAVConn.SSLVerify() == FALSE && CardDAVConn.CanDoSSL() == TRUE) {
388             
389                         lblServerSSLValid->SetLabel(_("Unable to verify"));
390             
391                 }
392                 
393 #endif
394                 
395                 if (CardDAVConn.CanDoCardDAV() == TRUE){
396             
397                         lblCardDAVSupportResult->SetLabel(_("Supported"));
398             
399                 } else {
400             
401                         lblCardDAVSupportResult->SetLabel(_("Unsupported"));
402                         SetErrorMessageLabel();
403                         return;
404             
405                 }
406         
407                 if (CardDAVConn.AbleToLogin() == TRUE){
408             
409                         lblAbleToLoginResult->SetLabel(_("Yes"));
410             
411                 } else {
412             
413                         lblAbleToLoginResult->SetLabel(_("No"));
414                         SetErrorMessageLabel();
415                         return;
416             
417                 }
418         
419                 // Get the address to process CardDAV requests.
420         
421                 ServerPrefix = CardDAVConn.GetDefaultAddressBookURL();
422         
423                 if (CardDAVConn.HasValidResponse() == TRUE){
424             
425                         lblServerResponse->SetLabel(_("Yes"));
426             
427                 } else {
428             
429                         lblServerResponse->SetLabel(_("No"));
430                         SetErrorMessageLabel();
431                         return;
432             
433                 }
434         
435                 if (ServerResult == TRUE && CardDAVConn.HasValidResponse() == TRUE &&
437                         CardDAVConn.CanDoCardDAV() == TRUE && CardDAVConn.AbleToLogin() == TRUE){
438             
439                         btnNext->Enable();
440             
441                         lblConnectionResultText->SetLabel(_("Click on Next to set the account name."));
442             
443                 } else {
444             
445                         SetErrorMessageLabel();
446             
447                 }
448         
449         } else if (PageSeek == 2){
450         
451                 // Finish screen.
452         
453                 tabType->Hide();
454                 tabConn->Hide();
455                 tabFinish->Show();
456                 szrNewAccount->RecalcSizes();
457         
458                 btnNext->Disable();
459                 btnNext->SetLabel(_("Finish"));
460         
461                 if (txtAccountName->IsEmpty() && PageSeek == 2){
462         
463                         btnNext->Disable();
464             
465                 } else {
466         
467                         btnNext->Enable();
468             
469                 }
470         
471         } else if (PageSeek == 3){
472         
473                 // Finished.
474         
475                 wxString XestiaABPrefDirectory;
476                 wxString XestiaABDirectory;
477                 wxString AccountSettingsFile;
478                 //wxFile ASFile;
479                 wxString RandomNumberSuffix = wxString::Format(wxT("%i"), rand() % 32767);
480                 bool DirectoryCreated = FALSE;
481         
482 #if defined(__HAIKU__)
483         
484                 //preffilename = wxT("noo");
485         
486 #elif defined(__WIN32__)
487         
488                 XestiaABPrefDirectory = GetUserPrefDir();
489                 XestiaABDirectory = GetUserDir();
490         
491                 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
492         
493                 // Open the file for writing.
494         
495                 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
496         
497                 // Check if account name already exists and return an error message
498                 // if this is the case.
499         
500                 wxString AccountName;
501                 long itemindex = 0;
502                 bool ContinueAcc;
503                 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
504         
505                 while (ContinueAcc){
506             
507                         if (txtAccountName->GetValue() == AccountName){
508                 
509                                 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
510                                 return;
511                 
512                         }
513             
514                         cfgfile->SetPath(wxT("/"));
515                         ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
516             
517                 }
518         
519                 if (cmbServerType->GetCurrentSelection() == 1){
520             
521                         // Create the account directory.
522             
523                         wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
524             
525                         if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
526                 
527                                 DirectoryCreated = TRUE;
528                 
529                         }
530             
531                         if (DirectoryCreated == TRUE){
532                 
533                                 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
534                 
535                         } else {
536                 
537                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
538                                 return;
539                 
540                         }
541             
542                 } else if (cmbServerType->GetCurrentSelection() == 0){
543             
544                         // Create the account directory.
545             
546                         wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
547             
548                         if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".local"), 0740) == TRUE){
549                 
550                                 DirectoryCreated = TRUE;
551                 
552                         }
553             
554                         if (DirectoryCreated == TRUE){
555                 
556                                 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
557                 
558                         } else {
559                 
560                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
561                                 return;
562                 
563                         }
564             
565                 }
566         
567                 delete cfgfile;
568                 cfgfile = NULL;
569         
570                 *ReloadAccountConfig = TRUE;
571         
572 #else
573         
574                 XestiaABPrefDirectory = GetUserPrefDir();
575                 XestiaABDirectory = GetUserDir();
576         
577                 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
578         
579                 // Open the file for writing.
580         
581                 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
582         
583                 // Check if account name already exists and return an error message
584                 // if this is the case.
585         
586                 wxString AccountName;
587                 long itemindex = 0;
588                 bool ContinueAcc;
589                 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
590         
591                 while (ContinueAcc){
592             
593                         if (txtAccountName->GetValue() == AccountName){
594                 
595                                 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
596                                 return;
597                 
598                         }
599             
600                         cfgfile->SetPath(wxT("/"));
601                         ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
602             
603                 }
604         
605                 if (cmbServerType->GetCurrentSelection() == 1){
606             
607                         // Create the account directory.
608             
609                         wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
610             
611                         if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
612                 
613                                 DirectoryCreated = TRUE;
614                 
615                         }
616             
617                         if (DirectoryCreated == TRUE){
618                 
619                                 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
620                 
621                         } else {
622                 
623                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
624                                 return;
625                 
626                         }
627             
628                 } else if (cmbServerType->GetCurrentSelection() == 0){
629             
630                         // Create the account directory.
631             
632                         wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
633             
634                         if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".local"), 0740) == TRUE){
635                 
636                                 DirectoryCreated = TRUE;
637                 
638                         }
639             
640                         if (DirectoryCreated == TRUE){
641                 
642                                 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
643                 
644                         } else {
645                 
646                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
647                                 return;
648                 
649                         }
650             
651                 }
652         
653                 delete cfgfile;
654                 cfgfile = NULL;
655         
656                 *ReloadAccountConfig = TRUE;
657         
658 #endif
659         
660                 this->Close();
661         
662         }
663     
666 void frmNewAccount::WriteAccountDetails(wxFileConfig *cfgfilein, wxString AccountType, wxString DirectoryName){
667     
668         // Write the new account details.
669     
670         cfgfilein->SetPath(txtAccountName->GetValue());
671         cfgfilein->Write(wxT("address"), txtServerAddress->GetValue());
672         cfgfilein->Write(wxT("port"), txtServerPort->GetValue());
673         cfgfilein->Write(wxT("username"), txtUsername->GetValue());
674         cfgfilein->Write(wxT("password"), txtPassword->GetValue());
675         cfgfilein->Write(wxT("prefix"), ServerPrefix);
676         cfgfilein->Write(wxT("accountdir"), DirectoryName);
677     
678         if (chkUseSSL->GetValue() == TRUE){
679         
680                 cfgfilein->Write(wxT("ssl"), wxT("true"));
681         
682         } else {
683         
684                 cfgfilein->Write(wxT("ssl"), wxT("false"));
685         
686         }
687     
688         cfgfilein->Write(wxT("refresh"), wxT("1800"));
689         cfgfilein->Write(wxT("type"), AccountType);
690     
693 void frmNewAccount::CloseWindow( wxCommandEvent& event )
696         // Close the window.
698         *ReloadAccountConfig = FALSE;
699         this->Close();
700         
703 void frmNewAccount::UpdateRequirements( wxCommandEvent& event )
705     
706         // Update the options.
707     
708         if (cmbServerType->GetCurrentSelection() == 1){
709         
710                 txtServerAddress->Enable();
711                 txtServerPort->Enable();
712                 txtUsername->Enable();
713                 txtPassword->Enable();
714                 chkUseSSL->Enable();
715         
716         } else if (cmbServerType->GetCurrentSelection() == 0){
717         
718                 txtServerAddress->Disable();
719                 txtServerPort->Disable();
720                 txtUsername->Disable();
721                 txtPassword->Disable();
722                 chkUseSSL->Disable();
723         
724         }
725     
728 void frmNewAccount::SetupPointers(bool *ReloadAccountInc){
730         // Setup the pointers for the new account window.
731    
732         ReloadAccountConfig = ReloadAccountInc;
733     
736 void frmNewAccount::SetErrorMessageLabel(){
737         
738         lblConnectionResultText->SetLabel(_("A problem has occured whilst connecting to the CardDAV server.\nPlease review the above information and change the server details if needed.\nIf there are still problems, please speak to your system administrator(s)."));
739         
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy