Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Random number generation for account directories wasn't working properly.
[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                 
480                 srand(time(0));
481                 int RandomNumber = rand() % 32767;
482                 wxString RandomNumberSuffix = wxString::Format(wxT("%i"), RandomNumber);
483                 bool DirectoryCreated = FALSE;
484         
485 #if defined(__HAIKU__)
486         
487                 //preffilename = wxT("noo");
488         
489 #elif defined(__WIN32__)
490         
491                 XestiaABPrefDirectory = GetUserPrefDir();
492                 XestiaABDirectory = GetUserDir();
493         
494                 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
495         
496                 // Open the file for writing.
497         
498                 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
499         
500                 // Check if account name already exists and return an error message
501                 // if this is the case.
502         
503                 wxString AccountName;
504                 long itemindex = 0;
505                 bool ContinueAcc;
506                 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
507         
508                 while (ContinueAcc){
509             
510                         if (txtAccountName->GetValue() == AccountName){
511                 
512                                 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
513                                 return;
514                 
515                         }
516             
517                         cfgfile->SetPath(wxT("/"));
518                         ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
519             
520                 }
521         
522                 if (cmbServerType->GetCurrentSelection() == 1){
523             
524                         // Create the account directory.
525             
526                         wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
527             
528                         if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
529                 
530                                 DirectoryCreated = TRUE;
531                 
532                         }
533             
534                         if (DirectoryCreated == TRUE){
535                 
536                                 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
537                 
538                         } else {
539                 
540                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
541                                 return;
542                 
543                         }
544             
545                 } else if (cmbServerType->GetCurrentSelection() == 0){
546             
547                         // Create the account directory.
548             
549                         wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
550             
551                         if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".local"), 0740) == TRUE){
552                 
553                                 DirectoryCreated = TRUE;
554                 
555                         }
556             
557                         if (DirectoryCreated == TRUE){
558                 
559                                 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
560                 
561                         } else {
562                 
563                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
564                                 return;
565                 
566                         }
567             
568                 }
569         
570                 delete cfgfile;
571                 cfgfile = NULL;
572         
573                 *ReloadAccountConfig = TRUE;
574         
575 #else
576         
577                 XestiaABPrefDirectory = GetUserPrefDir();
578                 XestiaABDirectory = GetUserDir();
579         
580                 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
581         
582                 // Open the file for writing.
583         
584                 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
585         
586                 // Check if account name already exists and return an error message
587                 // if this is the case.
588         
589                 wxString AccountName;
590                 long itemindex = 0;
591                 bool ContinueAcc;
592                 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
593         
594                 while (ContinueAcc){
595             
596                         if (txtAccountName->GetValue() == AccountName){
597                 
598                                 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
599                                 return;
600                 
601                         }
602             
603                         cfgfile->SetPath(wxT("/"));
604                         ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
605             
606                 }
607         
608                 if (cmbServerType->GetCurrentSelection() == 1){
609             
610                         // Create the account directory.
611             
612                         wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
613             
614                         if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
615                 
616                                 DirectoryCreated = TRUE;
617                 
618                         }
619             
620                         if (DirectoryCreated == TRUE){
621                 
622                                 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
623                 
624                         } else {
625                 
626                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
627                                 return;
628                 
629                         }
630             
631                 } else if (cmbServerType->GetCurrentSelection() == 0){
632             
633                         // Create the account directory.
634             
635                         wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
636             
637                         if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".local"), 0740) == TRUE){
638                 
639                                 DirectoryCreated = TRUE;
640                 
641                         }
642             
643                         if (DirectoryCreated == TRUE){
644                 
645                                 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
646                 
647                         } else {
648                 
649                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
650                                 return;
651                 
652                         }
653             
654                 }
655         
656                 delete cfgfile;
657                 cfgfile = NULL;
658         
659                 *ReloadAccountConfig = TRUE;
660         
661 #endif
662         
663                 this->Close();
664         
665         }
666     
669 void frmNewAccount::WriteAccountDetails(wxFileConfig *cfgfilein, wxString AccountType, wxString DirectoryName){
670     
671         // Write the new account details.
672     
673         cfgfilein->SetPath(txtAccountName->GetValue());
674         cfgfilein->Write(wxT("address"), txtServerAddress->GetValue());
675         cfgfilein->Write(wxT("port"), txtServerPort->GetValue());
676         cfgfilein->Write(wxT("username"), txtUsername->GetValue());
677         cfgfilein->Write(wxT("password"), txtPassword->GetValue());
678         cfgfilein->Write(wxT("prefix"), ServerPrefix);
679         cfgfilein->Write(wxT("accountdir"), DirectoryName);
680     
681         if (chkUseSSL->GetValue() == TRUE){
682         
683                 cfgfilein->Write(wxT("ssl"), wxT("true"));
684         
685         } else {
686         
687                 cfgfilein->Write(wxT("ssl"), wxT("false"));
688         
689         }
690     
691         cfgfilein->Write(wxT("refresh"), wxT("1800"));
692         cfgfilein->Write(wxT("type"), AccountType);
693     
696 void frmNewAccount::CloseWindow( wxCommandEvent& event )
699         // Close the window.
701         *ReloadAccountConfig = FALSE;
702         this->Close();
703         
706 void frmNewAccount::UpdateRequirements( wxCommandEvent& event )
708     
709         // Update the options.
710     
711         if (cmbServerType->GetCurrentSelection() == 1){
712         
713                 txtServerAddress->Enable();
714                 txtServerPort->Enable();
715                 txtUsername->Enable();
716                 txtPassword->Enable();
717                 chkUseSSL->Enable();
718         
719         } else if (cmbServerType->GetCurrentSelection() == 0){
720         
721                 txtServerAddress->Disable();
722                 txtServerPort->Disable();
723                 txtUsername->Disable();
724                 txtPassword->Disable();
725                 chkUseSSL->Disable();
726         
727         }
728     
731 void frmNewAccount::SetupPointers(bool *ReloadAccountInc){
733         // Setup the pointers for the new account window.
734    
735         ReloadAccountConfig = ReloadAccountInc;
736     
739 void frmNewAccount::SetErrorMessageLabel(){
740         
741         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)."));
742         
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