summaryrefslogtreecommitdiffstats
path: root/src/windows/leash/Krb4DomainRealmMaintenance.cpp
blob: 466b31a4bf85776787c8e675c783e815dfc62644 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//	**************************************************************************************
//	File:			Krb4DomainRealmMaintenance.cpp
//	By:				Arthur David Leather
//	Created:		12/02/98
//	Copyright		@1998 Massachusetts Institute of Technology - All rights reserved.
//	Description:	CPP file for Krb4DomainRealmMaintenance.h. Contains variables and functions
//					for Kerberos Four Properties
//
//	History:
//
//	MM/DD/YY	Inits	Description of Change
//	12/02/98	ADL		Original
//	**************************************************************************************

#include "stdafx.h"
#include "leash.h"
#include "KrbProperties.h"
#include "Krb4Properties.h"
#include "Krb4AddToDomainRealmList.h"
#include "Krb4EditDomainRealmList.h"
#include "Krb4DomainRealmMaintenance.h"
#include "lglobals.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CKrb4DomainRealmMaintenance dialog


IMPLEMENT_DYNCREATE(CKrb4DomainRealmMaintenance, CPropertyPage)

CKrb4DomainRealmMaintenance::CKrb4DomainRealmMaintenance() :
  CPropertyPage(CKrb4DomainRealmMaintenance ::IDD)
{
	m_defectiveLines = 0;
}

CKrb4DomainRealmMaintenance::~CKrb4DomainRealmMaintenance()
{
}

void CKrb4DomainRealmMaintenance::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CKrb4DomainRealmMaintenance)
	DDX_Control(pDX, IDC_LIST_DOMAINREALM, m_realmDomainList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CKrb4DomainRealmMaintenance, CPropertyPage)
	//{{AFX_MSG_MAP(CKrb4DomainRealmMaintenance)
	ON_BN_CLICKED(IDC_BUTTON_REALM_HOST_ADD, OnButtonRealmHostAdd)
	ON_BN_CLICKED(ID_BUTTON_REALM_HOST_REMOVE, OnButtonRealmHostRemove)
	ON_BN_CLICKED(IDC_BUTTON_REALM_HOST_EDIT, OnButtonRealmHostEdit)
	ON_LBN_SELCHANGE(IDC_LIST_DOMAINREALM, OnSelchangeListDomainrealm)
	ON_LBN_DBLCLK(IDC_LIST_DOMAINREALM, OnDblclkListDomainrealm)
	ON_BN_CLICKED(IDC_BUTTON_HOSTMAINT_HELP, OnButtonHostmaintHelp)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CKrb4DomainRealmMaintenance message handlers

BOOL CKrb4DomainRealmMaintenance::OnApply()
{
	CStdioFile krbrealmCon;
	if (!krbrealmCon.Open(CKrbProperties::m_krbrealmPath, CFile::modeCreate |
														  CFile::modeNoTruncate |
														  CFile::modeReadWrite))
	{
		LeashErrorBox("OnApply::Can't open Configuration File",
					  CKrbProperties::m_krbrealmPath);
		return TRUE;
	}

	memset(lineBuf, '\0', sizeof(lineBuf));
	krbrealmCon.SetLength(0);
	krbrealmCon.WriteString(lineBuf);
	for (INT maxItems = m_realmDomainList.GetCount(), item = 0; item < maxItems; item++)
	{
		memset(lineBuf, '\0', sizeof(lineBuf));
		if (!m_realmDomainList.GetText(item, lineBuf))
          break;

		krbrealmCon.WriteString(lineBuf);
		krbrealmCon.WriteString("\n");
	}

	krbrealmCon.Close();

	return TRUE;
}

BOOL CKrb4DomainRealmMaintenance::OnInitDialog()
{
	CPropertyPage::OnInitDialog();
	CStdioFile krbrealmCon;

	if (!krbrealmCon.Open(CKrbProperties::m_krbrealmPath, CFile::modeReadWrite))
	{ // can't find file, so lets set some defaults
		CString defaultStr;
		defaultStr.Format("%s %s", "MIT.EDU", KRB_REALM);
		m_realmDomainList.AddString(defaultStr);
	}
	else
	{
		while (TRUE)
		{
			if (!krbrealmCon.ReadString(lineBuf, sizeof(lineBuf)))
			  break;

			*(lineBuf + strlen(lineBuf) - 1) = 0;

			if (!strchr(lineBuf, ' ') && !strchr(lineBuf, '\t'))
			{ // found a defective line
				m_defectiveLines++;
			}

			if (LB_ERR == m_realmDomainList.AddString(lineBuf))
			{
				LeashErrorBox("OnInitDialog::Can't read Configuration File",
							  CKrbProperties::m_krbrealmPath);
				krbrealmCon.Close();
				return FALSE;
			}
		}

		krbrealmCon.Close();
	}

	m_realmDomainList.SetCurSel(0);

	if (!m_realmDomainList.GetCount())
	{
		GetDlgItem(ID_BUTTON_REALM_HOST_REMOVE)->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_REALM_HOST_EDIT)->EnableWindow(FALSE);
	}

	return TRUE;
}

void CKrb4DomainRealmMaintenance::OnButtonRealmHostAdd()
{
	CKrb4AddToDomainRealmList addToDomainRealmList;
	if (IDOK == addToDomainRealmList.DoModal())
	{
		if (addToDomainRealmList.GetNewRealm().IsEmpty())
		  ASSERT(0);

		CString newLine;
		newLine = addToDomainRealmList.GetNewDomainHost() + " " + addToDomainRealmList.GetNewRealm();

		// We don't want duplicate items in Listbox
		CString ckDups;
		for (INT item = 0; item < m_realmDomainList.GetCount(); item++)
		{
			m_realmDomainList.GetText(item, ckDups);
			if (0 == ckDups.CompareNoCase(newLine))
			{ // found duplicate item in Listbox
				LeashErrorBox("OnButtonRealmHostAdd::Found a Duplicate Item\nCan't add to List",
							  ckDups);
				return;
			}
		}

		m_realmDomainList.InsertString(0, newLine);
		m_realmDomainList.SetCurSel(0);
		SetModified(TRUE);

		if (1 == m_realmDomainList.GetCount())
		{
			GetDlgItem(ID_BUTTON_REALM_HOST_REMOVE)->EnableWindow();GetDlgItem(IDC_BUTTON_REALM_HOST_EDIT)->EnableWindow();
		}
	}
}

void CKrb4DomainRealmMaintenance::OnButtonRealmHostRemove()
{
	if (IDYES != AfxMessageBox("Your about to remove an item from the list!\n\nContinue?",
							   MB_YESNO))
	  return;

	INT curSel = m_realmDomainList.GetCurSel();
	m_realmDomainList.DeleteString(curSel);  // Single Sel Listbox

	if (-1 == m_realmDomainList.SetCurSel(curSel))
	  m_realmDomainList.SetCurSel(curSel - 1);

	if (!m_realmDomainList.GetCount())
	{
		GetDlgItem(ID_BUTTON_REALM_HOST_REMOVE)->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_REALM_HOST_EDIT)->EnableWindow(FALSE);
	}

	SetModified(TRUE);
}

void CKrb4DomainRealmMaintenance::OnButtonRealmHostEdit()
{
	INT selItemIndex = m_realmDomainList.GetCurSel();
	LPSTR pSelItem = new char[m_realmDomainList.GetTextLen(selItemIndex) + 1];
	if (!pSelItem)
	  ASSERT(0);

	CString selItem;
	m_realmDomainList.GetText(selItemIndex, selItem);
	strcpy(pSelItem, selItem);

	CKrb4EditDomainRealmList editDomainRealmList(pSelItem);
	delete [] pSelItem;

	if (IDOK == editDomainRealmList.DoModal())
	{
		CString editedItem = editDomainRealmList.GetEditedItem();
		if (0 != selItem.CompareNoCase(editedItem) &&
			LB_ERR != m_realmDomainList.FindStringExact(-1, editedItem))
		{
			LeashErrorBox("OnButtonRealmHostEdit::Found a Duplicate!\nCan't add to List",
							  editedItem);

			return;
		}

		m_realmDomainList.DeleteString(selItemIndex);
		m_realmDomainList.InsertString(selItemIndex, editDomainRealmList.GetEditedItem());
		m_realmDomainList.SetCurSel(selItemIndex);
		SetModified(TRUE);
	}
}

void CKrb4DomainRealmMaintenance::OnSelchangeListDomainrealm()
{
	//SetModified(TRUE);
}

void CKrb4DomainRealmMaintenance::OnDblclkListDomainrealm()
{
	OnButtonRealmHostEdit();
}

BOOL CKrb4DomainRealmMaintenance::PreTranslateMessage(MSG* pMsg)
{
	if (m_defectiveLines)
	{
		if (m_defectiveLines == 1)
		  LeashErrorBox("Found a defective entry in file",
						CKrbProperties::m_krbrealmPath, "Warning");
	    else if (m_defectiveLines > 1)
	      LeashErrorBox("Found more then one defective entry in file",
						CKrbProperties::m_krbrealmPath, "Warning");
	}

	m_defectiveLines = 0;
	return CPropertyPage::PreTranslateMessage(pMsg);
}




void CKrb4DomainRealmMaintenance::OnButtonHostmaintHelp()
{
	MessageBox("No Help Available!", "Leash", MB_OK);
}