jbuehler-add-missing-extern-c-patch-20031207
[openafs.git] / src / WINNT / client_exp / set_afs_acl.cpp
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 extern "C" {
11 #include <afs/param.h>
12 #include <afs/stds.h>
13 }
14
15 #include "stdafx.h"
16 #include "afs_shl_ext.h"
17 #include "set_afs_acl.h"
18 #include "clear_acl_dlg.h"
19 #include "add_acl_entry_dlg.h"
20 #include "copy_acl_dlg.h"
21 #include "gui2fs.h"
22
23 #ifdef _DEBUG
24 #define new DEBUG_NEW
25 #undef THIS_FILE
26 static char THIS_FILE[] = __FILE__;
27 #endif
28
29 /////////////////////////////////////////////////////////////////////////////
30 // CSetAfsAcl dialog
31
32
33 CSetAfsAcl::CSetAfsAcl(CWnd* pParent /*=NULL*/)
34         : CDialog()
35 {
36         InitModalIndirect (TaLocale_GetDialogResource (CSetAfsAcl::IDD), pParent);
37
38         //{{AFX_DATA_INIT(CSetAfsAcl)
39         //}}AFX_DATA_INIT
40
41         m_bChanges = FALSE;
42         m_nCurSel = -1;
43 }
44
45 void CSetAfsAcl::DoDataExchange(CDataExchange* pDX)
46 {
47         CDialog::DoDataExchange(pDX);
48         //{{AFX_DATA_MAP(CSetAfsAcl)
49         DDX_Control(pDX, IDC_REMOVE, m_Remove);
50         DDX_Control(pDX, IDC_ADMINISTER, m_AdminPerm);
51         DDX_Control(pDX, IDC_LOCK, m_LockPerm);
52         DDX_Control(pDX, IDC_INSERT, m_InsertPerm);
53         DDX_Control(pDX, IDC_DELETE, m_DeletePerm);
54         DDX_Control(pDX, IDC_LOOKUP, m_LookupPerm);
55         DDX_Control(pDX, IDC_WRITE, m_WritePerm);
56         DDX_Control(pDX, IDC_READ, m_ReadPerm);
57         DDX_Control(pDX, IDC_DIR_NAME, m_DirName);
58         DDX_Control(pDX, IDC_NEGATIVE_ENTRIES, m_NegativeRights);
59         DDX_Control(pDX, IDC_NORMAL_RIGHTS, m_NormalRights);
60         //}}AFX_DATA_MAP
61 }
62
63 BEGIN_MESSAGE_MAP(CSetAfsAcl, CDialog)
64         //{{AFX_MSG_MAP(CSetAfsAcl)
65         ON_BN_CLICKED(IDC_CLEAR, OnClear)
66         ON_BN_CLICKED(IDC_ADD, OnAdd)
67         ON_BN_CLICKED(IDC_COPY, OnCopy)
68         ON_LBN_SELCHANGE(IDC_NORMAL_RIGHTS, OnSelChangeNormalRights)
69         ON_LBN_SELCHANGE(IDC_NEGATIVE_ENTRIES, OnSelChangeNegativeEntries)
70         ON_BN_CLICKED(IDC_ADMINISTER, OnPermChange)
71         ON_BN_CLICKED(IDC_REMOVE, OnRemove)
72         ON_BN_CLICKED(IDC_CLEAN, OnClean)
73         ON_BN_CLICKED(IDC_DELETE, OnPermChange)
74         ON_BN_CLICKED(IDC_INSERT, OnPermChange)
75         ON_BN_CLICKED(IDC_LOCK, OnPermChange)
76         ON_BN_CLICKED(IDC_LOOKUP, OnPermChange)
77         ON_BN_CLICKED(IDC_READ, OnPermChange)
78         ON_BN_CLICKED(IDC_WRITE, OnPermChange)
79         ON_BN_CLICKED(IDHELP, OnHelp)
80         //}}AFX_MSG_MAP
81 END_MESSAGE_MAP()
82
83 /////////////////////////////////////////////////////////////////////////////
84 // CSetAfsAcl message handlers
85
86 BOOL CSetAfsAcl::OnInitDialog() 
87 {
88         CDialog::OnInitDialog();
89
90         if (!GetRights(m_strDir, m_Normal, m_Negative)) {
91                 EndDialog(0);
92                 return TRUE;
93         }
94
95         m_DirName.SetWindowText(m_strDir);
96
97         EnablePermChanges(FALSE);
98
99         m_NormalRights.SetTabStops(58);
100         m_NegativeRights.SetTabStops(58);
101
102         for (int i = 0; i < m_Normal.GetSize(); i += 2)
103                 m_NormalRights.AddString(m_Normal[i + 1] + "\t" + m_Normal[i]);
104
105         for (i = 0; i < m_Negative.GetSize(); i += 2)
106                 m_NegativeRights.AddString(m_Negative[i + 1] + "\t" + m_Negative[i]);
107
108         CenterWindow();
109
110         return TRUE;  // return TRUE unless you set the focus to a control
111                       // EXCEPTION: OCX Property Pages should return FALSE
112 }
113
114 void CSetAfsAcl::OnClear() 
115 {
116         CClearAclDlg dlg;
117
118         if (dlg.DoModal() == IDCANCEL)
119                 return;
120
121         BOOL bNormal, bNegative, bClearRights = FALSE;
122
123         dlg.GetSettings(bNormal, bNegative);
124
125         if (bNormal) {
126                 m_Normal.RemoveAll();
127                 m_NormalRights.ResetContent();
128                 if (m_bShowingNormal)
129                         bClearRights = TRUE;
130         }
131
132         if (bNegative) {
133                 m_Negative.RemoveAll();
134                 m_NegativeRights.ResetContent();
135                 if (!m_bShowingNormal)
136                         bClearRights = TRUE;
137         }
138         
139         if (bClearRights)
140                 OnNothingSelected();
141 }
142
143 void CSetAfsAcl::OnAdd() 
144 {
145         CAddAclEntryDlg dlg;
146
147         dlg.SetAclDlg(this);
148
149         if (dlg.DoModal() == IDCANCEL)
150                 return;
151
152         OnNothingSelected();
153
154         CString name = dlg.GetName();
155         CString rights = dlg.GetRights();
156         BOOL bNormal = dlg.IsNormal();
157
158         if (bNormal) {
159                 m_Normal.Add(name);
160                 m_Normal.Add(rights);
161                 m_nCurSel = m_NormalRights.AddString(rights + "\t" + name);
162                 m_NormalRights.SetSel(m_nCurSel);
163                 m_bShowingNormal = TRUE;
164         } else {
165                 m_Negative.Add(name);
166                 m_Negative.Add(rights);
167                 m_nCurSel = m_NegativeRights.AddString(rights + "\t" + name);
168                 m_NegativeRights.SetSel(m_nCurSel);
169                 m_bShowingNormal = FALSE;
170         }
171
172         ShowRights(rights);
173         EnablePermChanges(TRUE);
174
175         m_bChanges = TRUE;
176 }
177
178 void CSetAfsAcl::OnCopy() 
179 {
180         CCopyAclDlg dlg;
181
182         dlg.SetFromDir(m_strDir);
183
184         if (dlg.DoModal() == IDCANCEL)
185                 return;
186
187         CString strToDir = dlg.GetToDir();
188         BOOL bClear = dlg.GetClear();
189
190         CopyACL(strToDir, m_Normal, m_Negative, bClear);
191 }
192
193 void CSetAfsAcl::ShowRights(const CString& strRights)
194 {
195         m_ReadPerm.SetCheck((strRights.Find("r") == -1) ? UNCHECKED : CHECKED);
196         m_WritePerm.SetCheck((strRights.Find("w") == -1) ? UNCHECKED : CHECKED);
197         m_LookupPerm.SetCheck((strRights.Find("l") == -1) ? UNCHECKED : CHECKED);
198         m_DeletePerm.SetCheck((strRights.Find("d") == -1) ? UNCHECKED : CHECKED);
199         m_InsertPerm.SetCheck((strRights.Find("i") == -1) ? UNCHECKED : CHECKED);
200         m_LockPerm.SetCheck((strRights.Find("k") == -1) ? UNCHECKED : CHECKED);
201         m_AdminPerm.SetCheck((strRights.Find("a") == -1) ? UNCHECKED : CHECKED);
202 }
203
204 void CSetAfsAcl::OnSelChangeNormalRights() 
205 {
206         m_NegativeRights.SetSel(-1, FALSE);
207
208         m_bShowingNormal = TRUE;
209
210         int nNum = m_NormalRights.GetSelCount();
211         if (nNum != 1) {
212                 ShowRights("");
213                 EnablePermChanges(FALSE);
214                 return;
215         }
216
217         ASSERT(nNum == 1);              // I'm paranoid
218
219         VERIFY(m_NormalRights.GetSelItems(1, &m_nCurSel) == 1);
220
221         CString strRights = m_Normal[(m_nCurSel * 2) + 1];
222         ShowRights(strRights);
223
224         OnSelection();
225 }
226
227 void CSetAfsAcl::OnSelChangeNegativeEntries() 
228 {
229         m_NormalRights.SetSel(-1, FALSE);
230
231         m_bShowingNormal = FALSE;
232
233         int nNum = m_NegativeRights.GetSelCount();
234         if (nNum != 1) {
235                 ShowRights("");
236                 EnablePermChanges(FALSE);
237                 return;
238         }
239
240         ASSERT(nNum == 1);              // I'm paranoid
241
242         VERIFY(m_NegativeRights.GetSelItems(1, &m_nCurSel) == 1);
243
244         CString strRights = m_Negative[(m_nCurSel * 2) + 1];
245         ShowRights(strRights);
246
247         OnSelection();
248 }
249
250 CString CSetAfsAcl::MakeRightsString()
251 {
252         CString str;
253
254         if (m_ReadPerm.GetCheck() == CHECKED)
255                 str += "r";
256         if (m_LookupPerm.GetCheck() == CHECKED)
257                 str += "l";
258         if (m_InsertPerm.GetCheck() == CHECKED)
259                 str += "i";
260         if (m_DeletePerm.GetCheck() == CHECKED)
261                 str += "d";
262         if (m_WritePerm.GetCheck() == CHECKED)
263                 str += "w";
264         if (m_LockPerm.GetCheck() == CHECKED)
265                 str += "k";
266         if (m_AdminPerm.GetCheck() == CHECKED)
267                 str += "a";
268
269         return str;
270 }
271
272 void CSetAfsAcl::OnPermChange() 
273 {
274         CListBox *pRightsList;
275         CStringArray *pRights;
276
277         if (m_bShowingNormal) {
278                 pRightsList = &m_NormalRights;
279                 pRights = &m_Normal;
280         } else {
281                 pRightsList = &m_NegativeRights;
282                 pRights = &m_Negative;
283         }
284
285         ASSERT(m_nCurSel >= 0);
286
287         CString str = MakeRightsString();
288         (*pRights)[(2 * m_nCurSel) + 1] = str;
289         str += "\t" + (*pRights)[(2 * m_nCurSel)];
290
291         pRightsList->DeleteString(m_nCurSel);
292         pRightsList->InsertString(m_nCurSel, str);
293         pRightsList->SetSel(m_nCurSel);
294
295         m_bChanges = TRUE;
296 }
297
298 void CSetAfsAcl::OnRemove() 
299 {
300         CListBox *pRightsList;
301         CStringArray *pRights;
302
303         if (m_bShowingNormal) {
304                 pRightsList = &m_NormalRights;
305                 pRights = &m_Normal;
306         } else {
307                 pRightsList = &m_NegativeRights;
308                 pRights = &m_Negative;
309         }
310
311         int nNum = pRightsList->GetSelCount();
312         if (nNum < 0) {
313                 ASSERT(FALSE);
314                 return;
315         }
316
317         if (nNum == 0) {
318                 ASSERT(FALSE);          // How can this ever be 0?
319                 ASSERT(m_nCurSel >= 0);
320                 pRightsList->DeleteString(m_nCurSel);
321                 pRights->RemoveAt(m_nCurSel * 2, 2);
322         } else {
323                 int *pIndexes = new int[nNum];
324                 VERIFY(pRightsList->GetSelItems(nNum, pIndexes) != LB_ERR);
325
326                 for (int i = nNum - 1; i >= 0; i--) {
327                         pRightsList->DeleteString(pIndexes[i]);
328                         pRights->RemoveAt(pIndexes[i] * 2, 2);
329                 }
330
331                 delete [] pIndexes;
332         }
333
334         OnNothingSelected();
335
336         m_bChanges = TRUE;
337 }
338
339 void CSetAfsAcl::EnablePermChanges(BOOL bEnable)
340 {
341         m_ReadPerm.EnableWindow(bEnable);
342         m_WritePerm.EnableWindow(bEnable);
343         m_LookupPerm.EnableWindow(bEnable);
344         m_DeletePerm.EnableWindow(bEnable);
345         m_InsertPerm.EnableWindow(bEnable);
346         m_LockPerm.EnableWindow(bEnable);
347         m_AdminPerm.EnableWindow(bEnable);
348 }
349
350 void CSetAfsAcl::OnNothingSelected()
351 {
352         m_NormalRights.SetSel(-1, FALSE);       // Unselect any selected normal rights
353         m_NegativeRights.SetSel(-1, FALSE);     // Unselect any selected negative rights
354         ShowRights("");                                         // Show no rights
355         EnablePermChanges(FALSE);                       // Allow no rights changes
356         m_Remove.EnableWindow(FALSE);           // Disable remove button
357 }
358
359 void CSetAfsAcl::OnSelection()
360 {
361         EnablePermChanges(TRUE);
362         m_Remove.EnableWindow(TRUE);
363 }
364
365 void CSetAfsAcl::OnOK() 
366 {
367         if (m_bChanges && !SaveACL(m_strCellName, m_strDir, m_Normal, m_Negative))
368                 return;
369         
370         CDialog::OnOK();
371 }
372
373 void CSetAfsAcl::OnClean() 
374 {
375         CStringArray dirs;
376
377         dirs.Add(m_strDir);
378
379         CleanACL(dirs);
380 }
381
382 BOOL CSetAfsAcl::IsNameInUse(BOOL bNormal, const CString& strName)
383 {
384         if (bNormal) {
385                 for (int i = 0; i < m_Normal.GetSize(); i += 2)
386                         if (m_Normal[i] == strName)
387                                 return TRUE;
388
389                 return FALSE;
390         }
391
392         for (int i = 0; i < m_Negative.GetSize(); i += 2)
393                 if (m_Negative[i] == strName)
394                         return TRUE;
395
396         return FALSE;
397 }
398
399 void CSetAfsAcl::OnHelp() 
400 {
401         ShowHelp(m_hWnd, SET_AFS_ACL_HELP_ID);
402 }
403