1 // **************************************************************************************
2 // File: KrbConfigOptions.cpp
3 // By: Arthur David Leather
5 // Copyright @1998 Massachusetts Institute of Technology - All rights reserved.
6 // Description: CPP file for KrbProperties.h. Contains variables and functions
7 // for Kerberos Four and Five Properties
11 // MM/DD/YY Inits Description of Change
12 // 2/01/98 ADL Original
13 // **************************************************************************************
18 #include "KrbProperties.h"
19 #include "KrbConfigOptions.h"
20 #include "LeashFileDialog.h"
21 #include "LeashMessageBox.h"
31 static char THIS_FILE[] = __FILE__;
35 ///////////////////////////////////////////////////////////////////////
36 // CKrbConfigOptions property page
38 CString CKrbConfigOptions::m_newDefaultRealm;
39 CString CKrbConfigOptions::m_hostServer;
40 CComboBox CKrbConfigOptions::m_krbRealmEditbox;
41 BOOL CKrbConfigOptions::m_profileError;
42 BOOL CKrbConfigOptions::m_dupEntriesError;
44 IMPLEMENT_DYNCREATE(CKrbConfigOptions, CPropertyPage)
46 CKrbConfigOptions::CKrbConfigOptions() : CPropertyPage(CKrbConfigOptions::IDD)
48 m_initDefaultRealm = _T("");
49 m_newDefaultRealm = _T("");
50 m_startupPage2 = TRUE;
51 m_noKrbFileError = FALSE;
52 m_noKrbhostWarning = FALSE;
53 m_dupEntriesError = FALSE;
54 m_profileError = FALSE;
57 //{{AFX_DATA_INIT(CKrbConfigOptions)
61 CKrbConfigOptions::~CKrbConfigOptions()
65 VOID CKrbConfigOptions::DoDataExchange(CDataExchange* pDX)
67 TRACE("Entering CKrbConfigOptions::DoDataExchange -- %d\n",
68 pDX->m_bSaveAndValidate);
69 CPropertyPage::DoDataExchange(pDX);
70 //{{AFX_DATA_MAP(CKrbConfigOptions)
71 DDX_Control(pDX, IDC_EDIT_DEFAULT_REALM, m_krbRealmEditbox);
75 static char far * near parse_str(char far*buffer,char far*result)
77 while (isspace(*buffer))
79 while (!isspace(*buffer))
86 int krb_get_krbhst(char* h, char* r, int n)
88 char lbstorage[BUFSIZ];
90 static FILE *cnffile; /*XXX pbh added static because of MS bug in fgets() */
91 static char FAR *linebuf; /*XXX pbh added static because of MS bug in fgets() */
95 //static char buffer[80];
96 //krb_get_krbconf(buffer);
97 memset(lbstorage, '\0', BUFSIZ ); /* 4-22-94 */
98 linebuf = &lbstorage[0];
100 if ((cnffile = fopen(CKrbProperties::m_krbPath,"r")) == NULL) {
102 (void) strcpy(h,KRB_HOST);
108 /* linebuf=(char FAR *)malloc(BUFSIZ); */ /*4-22-94*/
109 if (fgets(linebuf,BUFSIZ,cnffile)==NULL) {
110 /* free(linebuf); */ /* 4-22-94 */
114 /* bzero( tr, sizeof(tr) ); */ /* pbh 2-24-93 */
115 memset(tr, '\0', sizeof(tr) );
116 parse_str(linebuf,tr);
120 /* run through the file, looking for the nth server for this realm */
121 for (i = 1; i <= n;) {
122 if (fgets(linebuf, BUFSIZ, cnffile) == NULL) {
123 /* free(linebuf); */ /*4-22-94*/
124 (void) fclose(cnffile);
127 /* bzero( tr, sizeof(tr) ); */ /* pbh 2-24-93 */
128 memset(tr, '\0', sizeof(tr) );
129 p=parse_str(linebuf,tr);
132 memset(h, '\0', lstrlen(h) );
139 /* free(linebuf); */ /*4-22-94*/
140 (void) fclose(cnffile);
145 BOOL CKrbConfigOptions::OnInitDialog()
147 m_initDefaultRealm = _T("");
148 m_newDefaultRealm = _T("");
149 m_noKrbFileError = FALSE;
150 m_noKrbhostWarning = FALSE;
151 m_dupEntriesError = FALSE;
152 m_profileError = FALSE;
155 CPropertyPage::OnInitDialog();
158 if (CLeashApp::m_hKrb4DLL && !CLeashApp::m_hKrb5DLL)
160 // Fill in all edit boxes
161 char krbRealm[REALM_SZ + 1];
162 char krbhst[MAX_HSTNM + 1];
164 if (!krbCon.Open(CKrbProperties::m_krbPath, CFile::modeRead))
166 SetDlgItemText(IDC_EDIT_DEFAULT_REALM, KRB_REALM);
167 SetDlgItemText(IDC_EDIT_REALM_HOSTNAME, KRB_MASTER);
168 //CheckRadioButton(IDC_RADIO_ADMIN_SERVER, IDC_RADIO_NO_ADMIN_SERVER, IDC_RADIO_NO_ADMIN_SERVER);
169 m_initDefaultRealm = m_newDefaultRealm = KRB_REALM;
172 { // place krbRealm in Edit box
173 memset(krbRealm, '\0', sizeof(krbRealm));
174 if (!krbCon.ReadString(krbRealm, sizeof(krbRealm)) || '\r' == *krbRealm ||
175 '\n' == *krbRealm || '\0' == *krbRealm)
177 SetDlgItemText(IDC_EDIT_DEFAULT_REALM, KRB_REALM);
178 SetDlgItemText(IDC_EDIT_REALM_HOSTNAME, KRB_MASTER);
179 m_initDefaultRealm = m_newDefaultRealm = KRB_REALM;
183 *(krbRealm + strlen(krbRealm) - 1) = 0;
184 LPSTR pSpace = strchr(krbRealm, ' ');
188 m_initDefaultRealm = m_newDefaultRealm = krbRealm;
190 memset(krbhst, '\0', sizeof(krbhst));
194 // don't use KRB4 - krb_get_krbhst - would have to re-logon, on file location
195 // change, to use this function
196 if (KFAILURE == pkrb_get_krbhst(krbhst, krbRealm, 1))
198 m_noKrbhostWarning = TRUE;
201 { // place hostname in Edit Box
202 //SetDlgItemText(IDC_EDIT_REALM_HOSTNAME, krbhst);
204 m_hostServer = krbhst;
206 // New stuff to put realms in Combo Box
208 if (!krbCon.Open(CKrbProperties::m_krbPath, CFile::modeRead))
210 m_noKrbFileError = TRUE;
215 CHAR lineBuf[REALM_SZ + MAX_HSTNM + 20];
216 CHAR localRealm[REALM_SZ + MAX_HSTNM + 20];
217 memset(lineBuf, '\0', sizeof(lineBuf));
218 memset(localRealm, '\0', sizeof(localRealm));
220 if (krbCon.ReadString(localRealm, sizeof(localRealm)))
221 *(localRealm + strlen(localRealm) - 1) = 0;
225 space = strchr(localRealm, ' ');
231 if (!krbCon.ReadString(lineBuf, sizeof(lineBuf)))
234 *(lineBuf + sizeof(lineBuf) - 1) = 0;
236 if (strlen(lineBuf) == 0)
239 space = strchr(lineBuf, ' ');
240 if (!space) space = strchr(lineBuf, '\t');
246 // skip Kerberos Options
247 if ( !strncmp(".KERBEROS.OPTION.",lineBuf,17) )
250 if (CB_ERR == m_krbRealmEditbox.FindStringExact(-1, lineBuf))
252 if (LB_ERR == m_krbRealmEditbox.AddString(lineBuf))
254 MessageBox("OnInitDialog::Can't add to Kerberos Realm Combobox",
261 m_krbRealmEditbox.SelectString(-1, krbRealm);
264 } // end of 'place hostname in Edit Box' else statement
265 } // end of 'Check for Host' else statement
266 } // end of 'place krbRealm in Edit box' else
270 if (CLeashApp::m_hKrb5DLL)
271 { // Krb5 OR krb5 AND krb4
273 pkrb5_get_default_realm(CLeashApp::m_krbv5_context, &realm);
278 m_initDefaultRealm = m_newDefaultRealm = realm;
280 if ( !CLeashApp::m_krbv5_profile ) {
281 CHAR confname[MAX_PATH];
282 if (!CLeashApp::GetProfileFile(confname, sizeof(confname)))
284 const char *filenames[2];
285 filenames[0] = confname;
287 pprofile_init(filenames, &CLeashApp::m_krbv5_profile);
291 CHAR selRealm[REALM_SZ];
292 strcpy(selRealm, m_newDefaultRealm);
293 const char* Section[] = {"realms", selRealm, "kdc", NULL};
294 const char** section = Section;
295 char **values = NULL;
298 long retval = pprofile_get_values(CLeashApp::m_krbv5_profile,
301 if (!retval && values)
302 m_hostServer = *values;
305 // Determine if we are using DNS for KDC lookups
306 retval = pprofile_get_string(CLeashApp::m_krbv5_profile, "libdefaults",
307 "dns_lookup_kdc", 0, 0, &value);
308 if (value == 0 && retval == 0)
309 retval = pprofile_get_string(CLeashApp::m_krbv5_profile, "libdefaults",
310 "dns_fallback", 0, 0, &value);
314 dns_in_use = config_boolean_to_int(value);
315 pprofile_release_string(value);
318 m_hostServer = "DNS SRV record lookups will be used to find KDC";
320 m_hostServer = "No KDC information available";
323 SetDlgItemText(IDC_EDIT_REALM_HOSTNAME, m_hostServer);
326 pkrb5_free_default_realm(CLeashApp::m_krbv5_context, realm);
329 // Set host and domain names in their Edit Boxes, respectively.
330 char hostName[80]="";
331 char domainName[80]="";
332 int ckHost = wsh_gethostname(hostName, sizeof(hostName));
333 int ckdomain = wsh_getdomainname(domainName, sizeof(domainName));
334 CString dot_DomainName = ".";
335 dot_DomainName += domainName;
337 SetDlgItemText(IDC_EDIT_HOSTNAME, ckHost == 0 ? hostName : "");
338 SetDlgItemText(IDC_EDIT_DOMAINNAME, ckdomain == 0 ? dot_DomainName : "");
343 BOOL CKrbConfigOptions::OnApply()
345 // If no changes were made, quit this function
346 if (0 == m_initDefaultRealm.CompareNoCase(m_newDefaultRealm))
349 m_newDefaultRealm.TrimLeft();
350 m_newDefaultRealm.TrimRight();
352 if (m_newDefaultRealm.IsEmpty())
354 MessageBox("OnApply::Your Kerberos Realm field must be filled in!",
356 m_newDefaultRealm = m_initDefaultRealm;
357 SetDlgItemText(IDC_EDIT_DEFAULT_REALM, m_newDefaultRealm);
362 if (!krbCon.Open(CKrbProperties::m_krbPath, CFile::modeCreate |
363 CFile::modeNoTruncate |
366 LeashErrorBox("OnApply::Can't open configuration file",
367 CKrbProperties::m_krbPath);
372 CString krbCon2File = CKrbProperties::m_krbPath;
373 krbCon2File += "___";
374 if (!krbCon2.Open(krbCon2File, CFile::modeCreate | CFile::modeWrite))
376 LeashErrorBox("OnApply:: Can't open configuration file",
377 CKrbProperties::m_krbPath);
382 krbCon.ReadString(readWrite);
383 krbCon2.WriteString(m_newDefaultRealm);
384 krbCon2.WriteString("\n");
385 while (krbCon.ReadString(readWrite))
387 krbCon2.WriteString(readWrite);
388 krbCon2.WriteString("\n");
393 krbCon2.Remove(CKrbProperties::m_krbPath);
394 krbCon2.Rename(krbCon2File, CKrbProperties::m_krbPath);
396 if (CLeashApp::m_hKrb5DLL)
397 { // Krb5 OR krb5 AND krb4
398 if ( !CLeashApp::m_krbv5_profile ) {
399 CHAR confname[MAX_PATH];
400 if (!CLeashApp::GetProfileFile(confname, sizeof(confname)))
402 const char *filenames[2];
403 filenames[0] = confname;
405 pprofile_init(filenames, &CLeashApp::m_krbv5_profile);
409 const char* Names[] = {"libdefaults", "default_realm", NULL};
410 const char** names = Names;
412 long retval = pprofile_update_relation(CLeashApp::m_krbv5_profile,
413 names, m_initDefaultRealm, m_newDefaultRealm);
417 MessageBox("OnApply::The previous value cannot be found, the profile will not be saved!!!\
418 \nIf this error persists after restarting Leash, contact your administrator.",
423 // Save to Kerberos Five config. file "Krb5.ini"
424 retval = pprofile_flush(CLeashApp::m_krbv5_profile);
427 m_initDefaultRealm = m_newDefaultRealm;
431 void CKrbConfigOptions::OnSelchangeEditDefaultRealm()
435 GetDlgItemText(IDC_EDIT_DEFAULT_REALM, m_newDefaultRealm);
438 if (CLeashApp::m_hKrb5DLL)
440 CHAR selRealm[REALM_SZ];
441 strcpy(selRealm, m_newDefaultRealm);
442 const char* Section[] = {"realms", selRealm, "kdc", NULL};
443 const char** section = Section;
444 char **values = NULL;
447 long retval = pprofile_get_values(CLeashApp::m_krbv5_profile,
450 if (!retval && values)
451 SetDlgItemText(IDC_EDIT_REALM_HOSTNAME, *values);
454 // Determine if we are using DNS for KDC lookups
455 retval = pprofile_get_string(CLeashApp::m_krbv5_profile, "libdefaults",
456 "dns_lookup_kdc", 0, 0, &value);
457 if (value == 0 && retval == 0)
458 retval = pprofile_get_string(CLeashApp::m_krbv5_profile, "libdefaults",
459 "dns_fallback", 0, 0, &value);
463 dns_in_use = config_boolean_to_int(value);
464 pprofile_release_string(value);
467 SetDlgItemText(IDC_EDIT_REALM_HOSTNAME, "DNS SRV record lookups will be used to find KDC");
469 SetDlgItemText(IDC_EDIT_REALM_HOSTNAME, "No KDC information available");
475 CHAR krbhst[MAX_HSTNM + 1];
476 CHAR krbrlm[REALM_SZ + 1];
478 strcpy(krbrlm, CKrbConfigOptions::m_newDefaultRealm);
479 memset(krbhst, '\0', sizeof(krbhst));
482 // don't use KRB4 - krb_get_krbhst - would have to re-logon, on file location
483 // change, to use this function
484 if (KFAILURE == pkrb_get_krbhst(krbhst, krbrlm, 1))
486 MessageBox("OnSelchangeEditDefaultRealm::Unable to find the Host Server for your Default Realm!!!\
487 \n 'Apply' your changes and try again.",
489 SetDlgItemText(IDC_EDIT_REALM_HOSTNAME, "");
493 m_hostServer = krbhst;
495 SetDlgItemText(IDC_EDIT_REALM_HOSTNAME, m_hostServer);
501 void CKrbConfigOptions::OnEditchangeEditDefaultRealm()
505 GetDlgItemText(IDC_EDIT_DEFAULT_REALM, m_newDefaultRealm);
510 void CKrbConfigOptions::OnShowWindow(BOOL bShow, UINT nStatus)
512 CPropertyPage::OnShowWindow(bShow, nStatus);
514 if (CLeashApp::m_hKrb5DLL)
515 ResetDefaultRealmComboBox();
517 SetDlgItemText(IDC_EDIT_REALM_HOSTNAME, m_hostServer);
520 void CKrbConfigOptions::ResetDefaultRealmComboBox()
522 // Reset Config Tab's Default Realm Combo Editbox
523 const char* rootSection[] = {"realms", NULL};
524 const char** rootsec = rootSection;
525 char **sections = NULL,
530 long retval = pprofile_get_string(CLeashApp::m_krbv5_profile, "libdefaults",
531 "dns_lookup_kdc", 0, 0, &value);
532 if (value == 0 && retval == 0)
533 retval = pprofile_get_string(CLeashApp::m_krbv5_profile, "libdefaults",
534 "dns_fallback", 0, 0, &value);
538 dns = config_boolean_to_int(value);
539 pprofile_release_string(value);
542 retval = pprofile_get_subsection_names(CLeashApp::m_krbv5_profile,
543 rootsec , §ions);
547 m_hostServer = _T("");
549 // This is not a fatal error if DNS KDC Lookup is being used.
550 // Determine the starting value for DNS KDC Lookup Checkbox
554 m_profileError = TRUE;
557 m_krbRealmEditbox.ResetContent();
559 if ( !m_profileError ) {
560 for (cpp = sections; *cpp; cpp++)
562 if (CB_ERR == m_krbRealmEditbox.FindStringExact(-1, *cpp))
564 if (CB_ERR == m_krbRealmEditbox.AddString(*cpp))
566 ::MessageBox(NULL, "ResetDefaultRealmComboBox::Can't add to Kerberos Realm Combobox",
572 m_dupEntriesError = TRUE;
576 if (!m_newDefaultRealm.IsEmpty()) {
578 if (CB_ERR == m_krbRealmEditbox.FindStringExact(-1, m_newDefaultRealm))
580 m_krbRealmEditbox.AddString(m_newDefaultRealm);
582 m_krbRealmEditbox.SelectString(-1, m_newDefaultRealm);
584 const char* Section[] = {"realms", m_newDefaultRealm, "kdc", NULL}; //theSection
585 const char** section = Section;
586 char **values = NULL;
588 retval = pprofile_get_values(CLeashApp::m_krbv5_profile,
591 if (!retval && values)
592 m_hostServer = *values;
595 m_hostServer = "DNS SRV record lookups will be used to find KDC";
597 m_hostServer = "No KDC information available";
603 BOOL CKrbConfigOptions::PreTranslateMessage(MSG* pMsg)
607 if (m_noKrbFileError)
609 LeashErrorBox("PreTranslateMessage::Unable to open configuration file",
610 !strlen(CKrbProperties::m_krbPath) ? KRB_FILE :
611 CKrbProperties::m_krbPath);
612 m_noKrbFileError = FALSE;
615 if (m_noKrbhostWarning)
617 MessageBox("PreTranslateMessage::Unable to locate the Kerberos Host for your Kerberos Realm!",
619 m_noKrbhostWarning = FALSE;
622 if (m_dupEntriesError)
624 MessageBox("PreTranslateMessage::Found duplicate entries in the Kerberos 5 Config. File!!!\
625 \nPlease contact your Administrator.",
628 m_dupEntriesError = FALSE;
633 MessageBox("PreTranslateMessage::Unable to open Kerberos 5 Config. File!!!\
634 \nIf this error persists, contact your administrator.",
636 m_profileError = FALSE;
641 MessageBox("PreTranslateMessage::Unable to determine the Default Realm.\
642 \n Contact your Administrator!",
649 m_startupPage2 = FALSE;
650 return CPropertyPage::PreTranslateMessage(pMsg);
654 BEGIN_MESSAGE_MAP(CKrbConfigOptions, CPropertyPage)
655 //{{AFX_MSG_MAP(CKrbConfigOptions)
657 ON_CBN_EDITCHANGE(IDC_EDIT_DEFAULT_REALM, OnEditchangeEditDefaultRealm)
658 ON_CBN_SELCHANGE(IDC_EDIT_DEFAULT_REALM, OnSelchangeEditDefaultRealm)
659 ON_BN_CLICKED(IDC_BUTTON_KRB_HELP, OnButtonKrbHelp)
660 ON_BN_CLICKED(IDC_BUTTON_KRBREALM_HELP, OnButtonKrbrealmHelp)
666 void CKrbConfigOptions::OnButtonKrbHelp()
668 MessageBox("No Help Available!", "Leash", MB_OK);
671 void CKrbConfigOptions::OnButtonKrbrealmHelp()
673 MessageBox("No Help Available!", "Leash", MB_OK);