Imported Upstream version 1.10.2
[platform/upstream/krb5.git] / src / windows / leash / Krb5Properties.cpp
1 //****************************************************************************
2 // File:        Krb5Properties.cpp
3 // By:          Arthur David Leather
4 // Created:     12/02/98
5 // Copyright:   1998 Massachusetts Institute of Technology - All rights
6 //              reserved.
7 // Description: CPP file for Krb5Properties.h. Contains variables and functions
8 //              for Kerberos Five Properties
9 //
10 // History:
11 //
12 // MM/DD/YY     Inits   Description of Change
13 // 12/02/98     ADL     Original
14 //*****************************************************************************
15
16 #include "stdafx.h"
17 #include "leash.h"
18 #include "LeashFileDialog.h"
19 #include "Krb5Properties.h"
20 #include "win-mac.h"
21 #include "lglobals.h"
22 #include "LeashView.h"
23
24 #ifdef _DEBUG
25 #define new DEBUG_NEW
26 #undef THIS_FILE
27 static char THIS_FILE[] = __FILE__;
28 #endif
29
30
31 /////////////////////////////////////////////////////////////////////////////
32 // CKrb5ConfigFileLocation dialog
33
34 IMPLEMENT_DYNCREATE(CKrb5ConfigFileLocation, CPropertyPage)
35
36 CKrb5ConfigFileLocation::CKrb5ConfigFileLocation()
37     : CPropertyPage(CKrb5ConfigFileLocation::IDD)
38 {
39     m_initConfigFile = _T("");
40     m_initTicketFile = _T("");
41     m_newConfigFile = _T("");
42     m_newTicketFile = _T("");
43     m_startupPage1 = TRUE;
44
45     //{{AFX_DATA_INIT(CKrb5ConfigFileLocation)
46     //}}AFX_DATA_INIT
47 }
48
49 void CKrb5ConfigFileLocation::DoDataExchange(CDataExchange* pDX)
50 {
51     CDialog::DoDataExchange(pDX);
52     //{{AFX_DATA_MAP(CKrb5ConfigFileLocation)
53     DDX_Control(pDX, IDC_EDIT_KRB5_TXT_FILE, m_ticketEditBox);
54     //}}AFX_DATA_MAP
55 }
56
57 BEGIN_MESSAGE_MAP(CKrb5ConfigFileLocation, CDialog)
58     //{{AFX_MSG_MAP(CKrb5ConfigFileLocation)
59     ON_BN_CLICKED(IDC_BUTTON_KRB5INI_BROWSE, OnButtonKrb5iniBrowse)
60     ON_BN_CLICKED(IDC_BUTTON_KRB5_TICKETFILE_BROWSE, OnButtonKrb5TicketfileBrowse)
61     ON_EN_CHANGE(IDC_EDIT_KRB5_TXT_FILE, OnChangeEditKrb5TxtFile)
62     ON_EN_CHANGE(IDC_EDIT_KRB5INI_LOCATION, OnChangeEditKrb5iniLocation)
63     ON_WM_SHOWWINDOW()
64     //}}AFX_MSG_MAP
65 END_MESSAGE_MAP()
66
67
68 BOOL CKrb5ConfigFileLocation::OnApply()
69 {
70     BOOL tooManySlashes = FALSE;
71     BOOL foundError = FALSE;
72
73     if( getenv("RENEW_TILL") !=  NULL)
74     {
75         MessageBox("The ticket renewable time is being controlled by the environment"
76                    "variable RENEW_TILL instead of the registry. Leash cannot modify"
77                    "the environment. Use the System control panel instead.",
78                     "Leash", MB_OK);
79         return(FALSE);
80     }
81
82     if( getenv("RENEWABLE") !=  NULL)
83     {
84         MessageBox("Ticket renewability is being controlled by the environment"
85                    "variable RENEWABLE instead of the registry. Leash cannot modify"
86                    "the environment. Use the System control panel instead.",
87                     "Leash", MB_OK);
88         return(FALSE);
89     }
90
91     if( getenv("FORWARDABLE") !=  NULL)
92     {
93         MessageBox("Ticket forwarding is being controlled by the environment"
94                    "variable FORWARDABLE instead of the registry. Leash cannot modify"
95                    "the environment. Use the System control panel instead.",
96                     "Leash", MB_OK);
97         return(FALSE);
98     }
99
100     if( getenv("PROXIABLE") !=  NULL)
101     {
102         MessageBox("Ticket proxying is being controlled by the environment"
103                    "variable PROXIABLE instead of the registry. Leash cannot modify"
104                    "the environment. Use the System control panel instead.",
105                     "Leash", MB_OK);
106         return(FALSE);
107     }
108
109     if( getenv("NOADDRESSES") !=  NULL)
110     {
111         MessageBox("Addressless tickets are being controlled by the environment"
112                    "variable NOADDRESSES instead of the registry. Leash cannot modify"
113                    "the environment. Use the System control panel instead.",
114                     "Leash", MB_OK);
115         return(FALSE);
116     }
117
118
119     // KRB5.INI file
120     if (!CLeashApp::m_krbv5_profile ||
121         0 != m_newConfigFile.CompareNoCase(m_initConfigFile))
122     { // Different path for Krb5.ini
123
124         if (IsDlgButtonChecked(IDC_CHECK_CONFIRM_KRB5_EXISTS))
125         {
126             // Check for extra slashes at end of path
127             LPSTR pSlash = strrchr(m_newConfigFile.GetBuffer(0), '\\');
128             if (pSlash && *(pSlash - 1) == '\\')
129             { // don't commit changes
130                 tooManySlashes = TRUE;
131             }
132             else if (pSlash && *(pSlash + 1) == '\0')
133             { // commit changes, but take out slash at the end of path
134                 *pSlash = 0;
135             }
136
137             m_newConfigFile.ReleaseBuffer(-1);
138
139             // Check for invalid path
140             Directory directory(m_newConfigFile);
141             if (tooManySlashes || !directory.IsValidFile())
142             { // don't commit changes
143                 foundError = TRUE;
144
145                 if (tooManySlashes)
146                     LeashErrorBox("OnApply::Too Many Slashes At End of "
147                                   "Selected Directory",
148                                   m_newConfigFile);
149                 else
150                     LeashErrorBox("OnApply::Selected file doesn't exist",
151                                   m_newConfigFile);
152
153                 SetDlgItemText(IDC_EDIT_KRB5INI_LOCATION, m_initConfigFile);
154             }
155             else
156             {
157                 // more error checking
158                 CHAR confname[MAX_PATH];
159
160                 const char *filenames[2];
161                 filenames[0] = m_newConfigFile;
162                 filenames[1] = NULL;
163
164                 const char*  rootSection[] = {"realms", NULL};
165                 const char** rootsec = rootSection;
166                 char **sections = NULL;
167
168                 long retval = pprofile_init(filenames, &CLeashApp::m_krbv5_profile);
169                 if (!retval)
170                     retval = pprofile_get_subsection_names(CLeashApp::m_krbv5_profile,
171                                                            rootsec, &sections
172                                                            );
173                 if (retval || !*sections )
174                 {
175                     foundError = TRUE;
176                     MessageBox("Your file selection is either corrupt or not a Kerberos Five Config. file",
177                                "Leash", MB_OK);
178
179                     pprofile_free_list(sections);
180
181                     // Restore old 'valid' config. file
182                     if (CLeashApp::GetProfileFile(confname, sizeof(confname)))
183                     {
184                         foundError = TRUE;
185                         MessageBox("Can't locate Kerberos Five Config. file!",
186                                    "Error", MB_OK);
187                         return TRUE;
188                     }
189
190                     filenames[0] = confname;
191                     filenames[1] = NULL;
192
193                     retval = pprofile_init(filenames, &CLeashApp::m_krbv5_profile);
194                     if (!retval)
195                         retval = pprofile_get_subsection_names(CLeashApp::m_krbv5_profile,
196                                                                rootsec, &sections);
197                     if (retval || !*sections)
198                     {
199                         foundError = TRUE;
200                         MessageBox("OnApply::There is a problem with your "
201                                    "Kerberos Five Config. file!\n"
202                                    "Contact your Administrator.",
203                                    "Leash", MB_OK);
204                     }
205
206                     pprofile_free_list(sections);
207                     SetDlgItemText(IDC_EDIT_KRB5INI_LOCATION, m_initConfigFile);
208
209                     pprofile_release(CLeashApp::m_krbv5_profile);
210                     return TRUE;
211                 }
212
213                 pprofile_free_list(sections);
214             }
215         }
216
217         // Commit changes
218         if (!foundError)
219         {
220             if (SetRegistryVariable("config", m_newConfigFile,
221                                     "Software\\MIT\\Kerberos5"))
222             {
223                 MessageBox("Failed to set \"Krb.conf\"!", "Error", MB_OK);
224             }
225
226             m_initConfigFile = m_newConfigFile;
227             SetModified(TRUE);
228         }
229     }
230
231     // Credential cache (ticket) file
232     // Ticket file
233     if (0 != m_initTicketFile.CompareNoCase(m_newTicketFile))
234     {
235         if (getenv("KRB5_ENV_CCNAME"))
236         {
237             // Just in case they set (somehow) KRB5_ENV_CCNAME while this box is up
238             MessageBox("OnApply::Ticket file is set in your System's"
239                        "Environment!\nYou must first remove it.",
240                        "Error", MB_OK);
241
242             return TRUE;
243         }
244
245         // Commit changes
246         if (SetRegistryVariable("ccname", m_newTicketFile,
247                                 "Software\\MIT\\Kerberos5"))
248         {
249             MessageBox("Failed to set \"ccname\"!", "Error", MB_OK);
250         }
251         if ( CLeashApp::m_krbv5_context )
252             pkrb5_cc_set_default_name(CLeashApp::m_krbv5_context,m_newTicketFile);
253
254         m_initTicketFile = m_newTicketFile;
255     }
256
257     return TRUE;
258 }
259
260
261 BOOL CKrb5ConfigFileLocation::OnInitDialog()
262 {
263     CDialog::OnInitDialog();
264
265     CHAR confname[MAX_PATH];
266     CHAR ticketName[MAX_PATH];
267
268     CheckDlgButton(IDC_CHECK_CONFIRM_KRB5_EXISTS, TRUE);
269
270     // Config. file (Krb5.ini)
271     if (CLeashApp::GetProfileFile(confname, sizeof(confname)))
272     {
273         MessageBox("Can't locate Kerberos Five config. file!", "Error", MB_OK);
274         return TRUE;
275     }
276
277     m_initConfigFile = m_newConfigFile = confname;
278     SetDlgItemText(IDC_EDIT_KRB5INI_LOCATION, m_initConfigFile);
279
280     if (pLeash_get_lock_file_locations() || getenv("KRB5_CONFIG"))
281     {
282         GetDlgItem(IDC_EDIT_KRB5INI_LOCATION)->EnableWindow(FALSE);
283         GetDlgItem(IDC_BUTTON_KRB5INI_BROWSE)->EnableWindow(FALSE);
284         GetDlgItem(IDC_CHECK_CONFIRM_KRB5_EXISTS)->EnableWindow(FALSE);
285     }
286     else if ( !(getenv("KRB5_CONFIG")) )
287     {
288         GetDlgItem(IDC_STATIC_INIFILES)->ShowWindow(FALSE);
289     }
290
291
292     // Set TICKET.KRB file Editbox
293     *ticketName = NULL;
294     if (CLeashApp::m_krbv5_context)
295     {
296         const char *pticketName = pkrb5_cc_default_name(CLeashApp::m_krbv5_context);
297
298         if (pticketName)
299             strcpy(ticketName, pticketName);
300     }
301
302     if (!*ticketName)
303     {
304         MessageBox("OnInitDialog::Can't locate Kerberos Five ticket file!",
305                    "Error", MB_OK);
306         return TRUE;
307     }
308     else
309     {
310         m_initTicketFile = m_newTicketFile = ticketName;
311         SetDlgItemText(IDC_EDIT_KRB5_TXT_FILE, m_initTicketFile);
312     }
313
314     if (getenv("KRB5CCNAME"))
315         GetDlgItem(IDC_EDIT_KRB5_TXT_FILE)->EnableWindow(FALSE);
316     else
317         GetDlgItem(IDC_STATIC_TICKETFILE)->ShowWindow(FALSE);
318
319     return TRUE;
320 }
321
322 void CKrb5ConfigFileLocation::OnButtonKrb5iniBrowse()
323 {
324     CLeashFileDialog dlgFile(TRUE, NULL, "*.*",
325                              "Kerbereos Five Config. File (.ini)");
326     dlgFile.m_ofn.lpstrTitle = "Select the Kerberos Five Config. File";
327     while (TRUE)
328     {
329         if (IDOK == dlgFile.DoModal())
330         {
331             m_newConfigFile = dlgFile.GetPathName();
332             SetDlgItemText(IDC_EDIT_KRB5INI_LOCATION, m_newConfigFile);
333             break;
334         }
335         else
336             break;
337     }
338 }
339
340 void CKrb5ConfigFileLocation::OnButtonKrb5TicketfileBrowse()
341 {
342     CString ticket_path = "*.*";
343     CLeashFileDialog dlgFile(TRUE, NULL, ticket_path,
344                              "Kerbereos Five Ticket File (Krb5cc)");
345     dlgFile.m_ofn.lpstrTitle = "Select Credential Cache (Ticket) File";
346
347     if (IDOK == dlgFile.DoModal())
348     {
349         m_newTicketFile = dlgFile.GetPathName();
350         SetDlgItemText(IDC_EDIT_KRB5_TXT_FILE, m_newTicketFile);
351     }
352 }
353
354 void CKrb5ConfigFileLocation::OnChangeEditKrb5iniLocation()
355 {
356     if (!m_startupPage1)
357     {
358         GetDlgItemText(IDC_EDIT_KRB5INI_LOCATION, m_newConfigFile);
359         SetModified(TRUE);
360     }
361 }
362
363 void CKrb5ConfigFileLocation::OnChangeEditKrb5TxtFile()
364 {
365     if (!m_startupPage1)
366     {
367         GetDlgItemText(IDC_EDIT_KRB5_TXT_FILE, m_newTicketFile);
368         SetModified(TRUE);
369     }
370 }
371
372 void CKrb5ConfigFileLocation::OnShowWindow(BOOL bShow, UINT nStatus)
373 {
374     CDialog::OnShowWindow(bShow, nStatus);
375     m_startupPage1 = FALSE;
376 }
377
378
379 /////////////////////////////////////////////////////////////////////////////
380 // CKrb5ConfigOptions dialog
381
382 IMPLEMENT_DYNCREATE(CKrb5ConfigOptions, CPropertyPage)
383
384 CKrb5ConfigOptions::CKrb5ConfigOptions()
385         : CPropertyPage(CKrb5ConfigOptions::IDD)
386 {
387     m_initForwardable = 0;
388     m_newForwardable = 0;
389     m_initProxiable = 0;
390     m_newProxiable = 0;
391     m_initRenewable = 0;
392     m_newRenewable = 0;
393     m_initNoAddress = 0;
394     m_newNoAddress = 0;
395     m_initIPAddress = 0;
396 #ifdef SET_PUBLIC_IP
397     m_newIPAddress = 0;
398 #endif /* SET_PUBLIC_IP */
399
400     //{{AFX_DATA_INIT(CKrb5ConfigOptions)
401     // NOTE: the ClassWizard will add member initialization here
402     //}}AFX_DATA_INIT
403 }
404
405
406 void CKrb5ConfigOptions::DoDataExchange(CDataExchange* pDX)
407 {
408     CDialog::DoDataExchange(pDX);
409
410     //{{AFX_DATA_MAP(CKrb5ConfigOptions)
411     // NOTE: the ClassWizard will add DDX and DDV calls here
412     //}}AFX_DATA_MAP
413 }
414
415
416 BEGIN_MESSAGE_MAP(CKrb5ConfigOptions, CDialog)
417     //{{AFX_MSG_MAP(CKrb5ConfigOptions)
418     ON_BN_CLICKED(IDC_CHECK_FORWARDABLE, OnCheckForwardable)
419     ON_BN_CLICKED(IDC_CHECK_PROXIABLE, OnCheckProxiable)
420     ON_BN_CLICKED(IDC_CHECK_RENEWABLE, OnCheckRenewable)
421     ON_BN_CLICKED(IDC_CHECK_NO_ADDRESS, OnCheckNoAddress)
422     ON_WM_HELPINFO()
423     //}}AFX_MSG_MAP
424 END_MESSAGE_MAP()
425
426
427 BOOL CKrb5ConfigOptions::OnApply()
428 {
429 #ifdef SET_PUBLIC_IP
430     SendDlgItemMessage( IDC_IPADDRESS_PUBLIC,
431                         IPM_GETADDRESS,
432                         0,
433                         (LPARAM)(LPDWORD)&m_newIPAddress
434                       );
435 #endif /* SET_PUBLIC_IP */
436
437     if ((m_initForwardable == m_newForwardable) &&
438         (m_initProxiable == m_newProxiable) &&
439         (m_initRenewable == m_newRenewable) &&
440         (m_initNoAddress == m_newNoAddress)
441 #ifdef SET_PUBLIC_IP
442          && (m_initIPAddress == m_newIPAddress)
443 #endif /* SET_PUBLIC_IP */
444          )
445         return TRUE;
446
447     CWinApp *pApp = NULL;
448     pApp = AfxGetApp();
449     if (!pApp)
450     {
451         MessageBox("There is a problem finding Leash application "
452                    "information!",
453                    "Error", MB_OK);
454         return FALSE;
455     }
456
457     if ( m_newNoAddress == FALSE ) {
458         CHAR confname[MAX_PATH];
459         if (!CLeashApp::GetProfileFile(confname, sizeof(confname)))
460         {
461             const char *filenames[2];
462             char *value=NULL;
463             long retval, noaddresses = 1;
464             filenames[0] = confname;
465             filenames[1] = NULL;
466             retval = pprofile_init(filenames, &CLeashApp::m_krbv5_profile);
467             if (!retval) {
468                 retval = pprofile_get_string(CLeashApp::m_krbv5_profile, "libdefaults","noaddresses", 0, "true", &value);
469                 if ( value ) {
470                     noaddresses = config_boolean_to_int(value);
471                     pprofile_release_string(value);
472                 }
473                 pprofile_release(CLeashApp::m_krbv5_profile);
474             }
475
476             if ( noaddresses )
477             {
478                 MessageBox("The No Addresses setting cannot be disabled unless the setting\n"
479                            "    noaddresses=false\n"
480                            "is added to the [libdefaults] section of the KRB5.INI file.",
481                             "Error", MB_OK);
482                 return FALSE;
483
484             }
485         }
486     }
487
488     pLeash_set_default_forwardable(m_newForwardable);
489     pLeash_set_default_proxiable(m_newProxiable);
490     pLeash_set_default_renewable(m_newRenewable);
491     pLeash_set_default_noaddresses(m_newNoAddress);
492 #ifdef SET_PUBLIC_IP
493     pLeash_set_default_publicip(m_newIPAddress);
494 #endif /* SET_PUBLIC_IP */
495
496     CLeashView::m_forwardableTicket = m_initForwardable = m_newForwardable;
497     CLeashView::m_proxiableTicket = m_initProxiable = m_newProxiable;
498     CLeashView::m_renewableTicket = m_initRenewable = m_newRenewable;
499     CLeashView::m_noaddressTicket = m_initNoAddress = m_newNoAddress;
500 #ifdef SET_PUBLIC_IP
501     CLeashView::m_publicIPAddress = m_initIPAddress = m_newIPAddress;
502 #endif /* SET_PUBLIC_IP */
503     return TRUE;
504 }
505
506 BOOL CKrb5ConfigOptions::OnInitDialog()
507 {
508     CDialog::OnInitDialog();
509
510     CWinApp *pApp = NULL;
511     pApp = AfxGetApp();
512     if (!pApp)
513     {
514         MessageBox("There is a problem finding Leash application "
515                    "information!",
516                    "Error", MB_OK);
517     }
518     else
519     {
520         m_initForwardable = pLeash_get_default_forwardable();
521         m_initProxiable = pLeash_get_default_proxiable();
522         m_initRenewable = pLeash_get_default_renewable();
523         m_initNoAddress = pLeash_get_default_noaddresses();
524         m_initIPAddress = pLeash_get_default_publicip();
525     }
526
527     CheckDlgButton(IDC_CHECK_FORWARDABLE, m_initForwardable);
528     m_newForwardable = m_initForwardable;
529
530     CheckDlgButton(IDC_CHECK_PROXIABLE, m_initProxiable);
531     m_newProxiable = m_initProxiable;
532
533     CheckDlgButton(IDC_CHECK_RENEWABLE, m_initRenewable);
534     m_newRenewable = m_initRenewable;
535
536     CheckDlgButton(IDC_CHECK_NO_ADDRESS, m_initNoAddress);
537     m_newNoAddress = m_initNoAddress;
538
539     if ( m_initNoAddress ) {
540         // Disable the control - jaltman
541
542         SendDlgItemMessage( IDC_IPADDRESS_PUBLIC,
543                             IPM_CLEARADDRESS,
544                             0,
545                             0
546                             );
547     }
548     else {
549         SendDlgItemMessage( IDC_IPADDRESS_PUBLIC,
550                             IPM_SETADDRESS,
551                             0,
552                             (LPARAM)m_initIPAddress
553                             );
554     }
555 #ifdef SET_PUBLIC_IP
556     m_newIPAddress = m_initIPAddress;
557 #endif /* SET_PUBLIC_IP */
558
559     return TRUE;  // return TRUE unless you set the focus to a control
560                   // EXCEPTION: OCX Property Pages should return FALSE
561 }
562
563 void CKrb5ConfigOptions::OnCheckForwardable()
564 {
565     m_newForwardable = (BOOL)IsDlgButtonChecked(IDC_CHECK_FORWARDABLE);
566     SetModified(TRUE);
567 }
568
569 void CKrb5ConfigOptions::OnCheckProxiable()
570 {
571     m_newProxiable = (BOOL)IsDlgButtonChecked(IDC_CHECK_PROXIABLE);
572     SetModified(TRUE);
573 }
574
575 void CKrb5ConfigOptions::OnCheckRenewable()
576 {
577     m_newRenewable = (BOOL)IsDlgButtonChecked(IDC_CHECK_RENEWABLE);
578     SetModified(TRUE);
579 }
580
581 void CKrb5ConfigOptions::OnCheckNoAddress()
582 {
583     m_newNoAddress = (BOOL)IsDlgButtonChecked(IDC_CHECK_NO_ADDRESS);
584     SetModified(TRUE);
585
586     if ( m_newNoAddress ) {
587         // Disable the control - jaltman
588
589         SendDlgItemMessage( IDC_IPADDRESS_PUBLIC,
590                             IPM_CLEARADDRESS,
591                             0,
592                             0
593                             );
594     } else {
595         // Enable the IP Address Control - jaltman
596
597         SendDlgItemMessage( IDC_IPADDRESS_PUBLIC,
598                             IPM_SETADDRESS,
599                             0,
600                             (LPARAM)m_initIPAddress
601                             );
602     }
603 }
604
605 ///////////////////////////////////////////////////////////////////////
606 // CKrb5Properties
607
608 IMPLEMENT_DYNAMIC(CKrb5Properties, CPropertySheet)
609
610 CKrb5Properties::CKrb5Properties(UINT nIDCaption, CWnd* pParentWnd,
611                                  UINT iSelectPage)
612     :CPropertySheet(nIDCaption, pParentWnd, iSelectPage)
613 {
614 }
615
616 CKrb5Properties::CKrb5Properties(LPCTSTR pszCaption, CWnd* pParentWnd,
617                                  UINT iSelectPage)
618     :CPropertySheet(pszCaption, pParentWnd, iSelectPage)
619 {
620     AddPage(&m_fileLocation);
621     AddPage(&m_configOptions);
622 }
623
624 CKrb5Properties::~CKrb5Properties()
625 {
626 }
627
628 void CKrb5Properties::OnHelp()
629 {
630 #ifdef CALL_HTMLHELP
631     AfxGetApp()->HtmlHelp(HID_KRB5_PROPERTIES_COMMAND);
632 #else
633     AfxGetApp()->WinHelp(HID_KRB5_PROPERTIES_COMMAND);
634 #endif
635 }
636
637
638
639 BEGIN_MESSAGE_MAP(CKrb5Properties, CPropertySheet)
640     //{{AFX_MSG_MAP(CKrb5Properties)
641     // NOTE - the ClassWizard will add and remove mapping macros here.
642     ON_COMMAND(ID_HELP, OnHelp)
643     //}}AFX_MSG_MAP
644 END_MESSAGE_MAP()