Windows: AFSTearDownExtents may experience active extents
[openafs.git] / src / WINNT / client_creds / mounttab.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 #include <winsock2.h>
11 #include <ws2tcpip.h>
12
13 extern "C" {
14 #include <afsconfig.h>
15 #include <afs/param.h>
16 #include <roken.h>
17
18 #include <afs/stds.h>
19 #include <afs/fs_utils.h>
20 }
21
22 #include "afscreds.h"
23
24
25 /*
26  * VARIABLES __________________________________________________________________
27  *
28  */
29
30 static struct l
31 {
32     int iDriveSelectLast;
33 } l;
34
35
36 /*
37  * PROTOTYPES _________________________________________________________________
38  *
39  */
40
41 void Mount_OnInitDialog (HWND hDlg);
42 void Mount_OnUpdate (HWND hDlg, BOOL fOnInitDialog = FALSE);
43 void Mount_OnSelect (HWND hDlg);
44 void Mount_OnCheck (HWND hDlg);
45 void Mount_OnRemove (HWND hDlg);
46 void Mount_OnAdd (HWND hDlg);
47 void Mount_OnEdit (HWND hDlg);
48
49 void Mount_AdjustMapping (HWND hDlg, int iDrive);
50 int Mount_DriveFromItem (HWND hDlg, int iItem);
51
52 BOOL CALLBACK Mapping_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp);
53 void Mapping_OnInitDialog (HWND hDlg);
54 void Mapping_OnOK (HWND hDlg);
55 void Mapping_OnEnable (HWND hDlg);
56
57
58 /*
59  * ROUTINES ___________________________________________________________________
60  *
61  */
62
63 BOOL CALLBACK Mount_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
64 {
65    switch (msg)
66       {
67       case WM_INITDIALOG:
68          RECT rTab;
69          GetClientRect (GetParent(hDlg), &rTab);
70          TabCtrl_AdjustRect (GetParent (hDlg), FALSE, &rTab);
71          SetWindowPos (hDlg, NULL, rTab.left, rTab.top, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER);
72
73          Mount_OnInitDialog (hDlg);
74          break;
75
76       case WM_COMMAND:
77          switch (LOWORD(wp))
78             {
79             case IDC_ADD:
80                Mount_OnAdd (hDlg);
81                break;
82
83             case IDC_EDIT:
84                Mount_OnEdit (hDlg);
85                break;
86
87             case IDC_REMOVE:
88                Mount_OnRemove (hDlg);
89                break;
90
91             case IDC_LIST:
92                if (HIWORD(wp) == LBN_CLICKED)
93                   Mount_OnCheck (hDlg);
94                else if ((HIWORD(wp) == LBN_SELCHANGE) || (HIWORD(wp) == LBN_SELCANCEL))
95                   Mount_OnSelect (hDlg);
96                break;
97
98             case IDHELP:
99                Mount_DlgProc (hDlg, WM_HELP, 0, 0);
100                break;
101             }
102          break;
103
104       case WM_HELP:
105          WinHelp (hDlg, g.szHelpFile, HELP_CONTEXT, IDH_AFSCREDS_TAB_DRIVES);
106          break;
107       }
108    return FALSE;
109 }
110
111
112 void Mount_OnInitDialog (HWND hDlg)
113 {
114    int xTabStop = 250;
115    SendDlgItemMessage (hDlg, IDC_LIST, LB_SETTABSTOPS, 1, (LPARAM)&xTabStop);
116
117    Mount_OnUpdate (hDlg, TRUE);
118 }
119
120
121 void Mount_OnUpdate (HWND hDlg, BOOL fOnInitDialog)
122 {
123     DRIVEMAPLIST List;
124     memset(&List, 0, sizeof(DRIVEMAPLIST));
125     QueryDriveMapList (&List);
126
127     HWND hList = GetDlgItem (hDlg, IDC_LIST);
128     int iItemSel = LB_GetSelected(hList);
129     int iDataSel = Mount_DriveFromItem (hDlg, iItemSel);
130     iItemSel = -1;
131
132     if (fOnInitDialog && (iDataSel == -1))
133         iDataSel = l.iDriveSelectLast;
134
135     LB_StartChange(hList, TRUE);
136
137     for (int iDrive = 25; iDrive >= 0; --iDrive)
138     {
139         if (!List.aDriveMap[ iDrive ].szMapping[0])
140             continue;
141
142         TCHAR szAfsPath[ MAX_PATH ];
143         AdjustAfsPath (szAfsPath, List.aDriveMap[ iDrive ].szMapping, TRUE, FALSE);
144
145         LPTSTR psz = FormatString (IDS_DRIVE_MAP, TEXT("%c%s"), List.aDriveMap[ iDrive ].chDrive, szAfsPath);
146         int iItem = LB_AddItem(hList, psz, List.aDriveMap[ iDrive ].fActive);
147         FreeString (psz);
148
149         int iCount = SendMessage(hList, LB_GETCOUNT, 0, 0);
150
151         /* This really shouldn't work except that we are adding
152          * the strings in alphabetical order.  Otherwise, we could
153          * identify an index value for a string that could then change.
154          */
155         if (iDrive == iDataSel)
156             iItemSel = iItem;
157     }
158
159     LB_EndChange(hList, NULL);
160
161     LB_SetSelected(hList, iItemSel);
162
163     Mount_OnSelect (hDlg);
164     FreeDriveMapList (&List);
165 }
166
167
168 void Mount_OnSelect (HWND hDlg)
169 {
170     BOOL fServiceRunning = IsServiceRunning();
171
172    HWND hList = GetDlgItem (hDlg, IDC_LIST);
173    int iItemSel = LB_GetSelected(hList);
174    int iDataSel = Mount_DriveFromItem (hDlg, iItemSel);
175
176    l.iDriveSelectLast = iDataSel;
177
178    EnableWindow (GetDlgItem (hDlg, IDC_ADD), fServiceRunning);
179    EnableWindow (GetDlgItem (hDlg, IDC_EDIT), fServiceRunning && (iDataSel != -1));
180    EnableWindow (GetDlgItem (hDlg, IDC_REMOVE), fServiceRunning && (iDataSel != -1));
181 }
182
183
184 void Mount_OnCheck (HWND hDlg)
185 {
186    DRIVEMAPLIST List;
187    QueryDriveMapList (&List);
188
189    HWND hList = GetDlgItem (hDlg, IDC_LIST);
190    int iItemSel = LB_GetSelected(hList);
191    int iDriveSel = Mount_DriveFromItem (hDlg, iItemSel);
192    BOOL fChecked = SendMessage (hList, LB_GETITEMDATA, iItemSel, 0);
193
194    if (iDriveSel != -1)
195       {
196       DWORD dwStatus;
197       if (fChecked && List.aDriveMap[ iDriveSel ].szMapping[0] && !List.aDriveMap[ iDriveSel ].fActive)
198          {
199          if (!ActivateDriveMap (List.aDriveMap[ iDriveSel ].chDrive, List.aDriveMap[ iDriveSel ].szMapping, List.aDriveMap[ iDriveSel ].szSubmount, List.aDriveMap[ iDriveSel ].fPersistent, &dwStatus))
200             Message (MB_OK | MB_ICONHAND, IDS_ERROR_MAP, IDS_ERROR_MAP_DESC, TEXT("%08lX"), dwStatus);
201          Mount_OnUpdate (hDlg);
202          }
203       else if (!fChecked && List.aDriveMap[ iDriveSel ].fActive)
204          {
205          if (!InactivateDriveMap (List.aDriveMap[ iDriveSel ].chDrive, &dwStatus))
206             Message (MB_OK | MB_ICONHAND, IDS_ERROR_UNMAP, IDS_ERROR_UNMAP_DESC, TEXT("%08lX"), dwStatus);
207          Mount_OnUpdate (hDlg);
208          }
209       WriteActiveMap(List.aDriveMap[ iDriveSel ].chDrive, fChecked && List.aDriveMap[ iDriveSel ].fPersistent );
210       }
211
212    FreeDriveMapList (&List);
213 }
214
215
216 void Mount_OnRemove (HWND hDlg)
217 {
218     HWND hList = GetDlgItem (hDlg, IDC_LIST);
219     int iItemSel = LB_GetSelected(hList);
220    int iDriveSel = Mount_DriveFromItem (hDlg, iItemSel);
221
222    if (iDriveSel != -1)
223       {
224       DRIVEMAPLIST List;
225       QueryDriveMapList (&List);
226
227       if (List.aDriveMap[ iDriveSel ].szMapping[0])
228          {
229          if (List.aDriveMap[ iDriveSel ].fActive)
230             {
231             DWORD dwStatus;
232             if (!InactivateDriveMap (List.aDriveMap[ iDriveSel ].chDrive, &dwStatus))
233                {
234                Message (MB_OK | MB_ICONHAND, IDS_ERROR_UNMAP, IDS_ERROR_UNMAP_DESC, TEXT("%08lX"), dwStatus);
235                return;
236                }
237             }
238          List.aDriveMap[ iDriveSel ].szMapping[0] = TEXT('\0');
239          WriteDriveMappings (&List);
240
241          Mount_OnUpdate (hDlg);
242          }
243           WriteActiveMap(List.aDriveMap[ iDriveSel ].chDrive, FALSE );
244       FreeDriveMapList (&List);
245       }
246 }
247
248
249 void Mount_OnAdd (HWND hDlg)
250 {
251    Mount_AdjustMapping (hDlg, -1);
252 }
253
254
255 void Mount_OnEdit (HWND hDlg)
256 {
257    HWND hList = GetDlgItem (hDlg, IDC_LIST);
258    int iItemSel = LB_GetSelected(hList);
259    int iDriveSel = Mount_DriveFromItem (hDlg, iItemSel);
260
261    Mount_AdjustMapping (hDlg, iDriveSel);
262 }
263
264
265 void Mount_AdjustMapping (HWND hDlg, int iDrive)
266 {
267    DRIVEMAPLIST List;
268    QueryDriveMapList (&List);
269
270    DRIVEMAP DriveMapOrig;
271    memset (&DriveMapOrig, 0x00, sizeof(DRIVEMAP));
272
273    if (iDrive != -1)
274       {
275       memcpy (&DriveMapOrig, &List.aDriveMap[ iDrive ], sizeof(DRIVEMAP));
276       }
277
278    DRIVEMAP DriveMap;
279    memcpy (&DriveMap, &DriveMapOrig, sizeof(DRIVEMAP));
280
281    if (ModalDialogParam (IDD_MAPPING, hDlg, (DLGPROC)Mapping_DlgProc, (LPARAM)&DriveMap) == IDOK)
282       {
283       TCHAR szAfsPathOrig[ MAX_PATH ];
284       if (iDrive != -1)
285          AdjustAfsPath (szAfsPathOrig, DriveMapOrig.szMapping, TRUE, TRUE);
286
287       TCHAR szAfsPathNew[ MAX_PATH ];
288       AdjustAfsPath (szAfsPathNew, DriveMap.szMapping, TRUE, TRUE);
289
290       if ( (lstrcmpi (szAfsPathOrig, szAfsPathNew)) ||
291            (lstrcmpi (DriveMapOrig.szSubmount, DriveMap.szSubmount)) ||
292            (DriveMapOrig.chDrive != DriveMap.chDrive) ||
293            (DriveMapOrig.fPersistent != DriveMap.fPersistent) )
294          {
295          DWORD dwStatus;
296
297          if ((iDrive != -1) && (DriveMapOrig.fActive))
298             {
299             if (!InactivateDriveMap (DriveMapOrig.chDrive, &dwStatus))
300                {
301                Message (MB_OK | MB_ICONHAND, IDS_ERROR_UNMAP, IDS_ERROR_UNMAP_DESC, TEXT("%08lX"), dwStatus);
302                Mount_OnUpdate (hDlg);
303                return;
304                }
305             }
306
307          if (!ActivateDriveMap (DriveMap.chDrive, szAfsPathNew, DriveMap.szSubmount, DriveMap.fPersistent, &dwStatus))
308             {
309             Message (MB_OK | MB_ICONHAND, IDS_ERROR_MAP, IDS_ERROR_MAP_DESC, TEXT("%08lX"), dwStatus);
310             Mount_OnUpdate (hDlg);
311             return;
312             }
313
314          if (DriveMap.szSubmount[0])
315             {
316             TCHAR szSubmountNow[ MAX_PATH ];
317             if (GetDriveSubmount (DriveMap.chDrive, szSubmountNow))
318                {
319                if (lstrcmpi (DriveMap.szSubmount, szSubmountNow))
320                   {
321                   int idsTitle = (g.fIsWinNT) ? IDS_NEWSUB_TITLE : IDS_NEWSUB_TITLE_95;
322                   Message (MB_OK | MB_ICONASTERISK, idsTitle, IDS_NEWSUB_DESC);
323                   }
324                }
325             }
326
327          if (iDrive != -1)
328             memset (&List.aDriveMap[ iDrive ], 0x00, sizeof(DRIVEMAP));
329          memcpy (&List.aDriveMap[ DriveMap.chDrive-chDRIVE_A ], &DriveMap, sizeof(DRIVEMAP));
330          lstrcpy (List.aDriveMap[ DriveMap.chDrive-chDRIVE_A ].szMapping, szAfsPathNew);
331          WriteDriveMappings (&List);
332
333          Mount_OnUpdate (hDlg);
334          }
335       }
336 }
337
338
339 int Mount_DriveFromItem (HWND hDlg, int iItem)
340 {
341     TCHAR szItem[ 1024 ] = TEXT("");
342     SendDlgItemMessage (hDlg, IDC_LIST, LB_GETTEXT, iItem, (LPARAM)szItem);
343
344     LPTSTR pch;
345     if ((pch = (LPTSTR)lstrchr (szItem, TEXT(':'))) != NULL)
346     {
347         if (pch > szItem)
348         {
349             pch--;
350             if ((*pch >= TEXT('A')) && (*pch <= TEXT('Z')))
351                 return (*pch) - TEXT('A');
352         }
353     }
354
355     return -1;
356 }
357
358
359 BOOL CALLBACK Mapping_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
360 {
361    switch (msg)
362       {
363       case WM_INITDIALOG:
364          SetWindowLongPtr (hDlg, DWLP_USER, lp);
365          Mapping_OnInitDialog (hDlg);
366          break;
367
368       case WM_COMMAND:
369          switch (LOWORD(wp))
370             {
371             case IDOK:
372                Mapping_OnOK (hDlg);
373                break;
374
375             case IDCANCEL:
376                EndDialog (hDlg, IDCANCEL);
377                break;
378
379             case IDC_MAP_PATH:
380                Mapping_OnEnable (hDlg);
381                break;
382
383             case IDHELP:
384                Mapping_DlgProc (hDlg, WM_HELP, 0, 0);
385                break;
386             }
387          break;
388
389       case WM_HELP:
390          WinHelp (hDlg, g.szHelpFile, HELP_CONTEXT, IDH_AFSCREDS_MAPDRIVE);
391          break;
392       }
393    return FALSE;
394 }
395
396
397 void Mapping_OnInitDialog (HWND hDlg)
398 {
399    PDRIVEMAP pMap = (PDRIVEMAP)GetWindowLongPtr (hDlg, DWLP_USER);
400
401    // Fill in the combo box
402    //
403    DWORD dwDrives = GetLogicalDrives() | 0x07; // Always pretend A,B,C: are used
404
405    if (pMap->chDrive != 0)
406       dwDrives &= ~( 1 << (pMap->chDrive - chDRIVE_A) );
407
408    int iItemSel = -1;
409    HWND hCombo = GetDlgItem (hDlg, IDC_MAP_LETTER);
410    SendMessage (hCombo, WM_SETREDRAW, FALSE, 0);
411
412    for (int ii = 0; ii < 26; ++ii)
413       {
414       if (!(dwDrives & (1<<ii)))
415          {
416          TCHAR szText[ cchRESOURCE ];
417          GetString (szText, IDS_MAP_LETTER);
418
419          LPTSTR pch;
420          if ((pch = (LPTSTR)lstrchr (szText, TEXT('*'))) != NULL)
421             *pch = TEXT('A') + ii;
422
423          int iItem = SendMessage (hCombo, CB_ADDSTRING, 0, (LPARAM)szText);
424          SendMessage (hCombo, CB_SETITEMDATA, iItem, ii);
425          if (pMap->chDrive && (ii == pMap->chDrive - chDRIVE_A))
426             iItemSel = iItem;
427          else if ((!pMap->chDrive) && (iItemSel == -1))
428             iItemSel = iItem;
429          }
430       }
431
432    SendMessage (hCombo, WM_SETREDRAW, TRUE, 0);
433    SendMessage (hCombo, CB_SETCURSEL, iItemSel, 0);
434
435    TCHAR szMapping[ MAX_PATH ];
436     AdjustAfsPath (szMapping, ((pMap->szMapping[0]) ? pMap->szMapping : cm_slash_mount_root), TRUE, FALSE);
437     CHAR msg[256], msgf[256];
438     if (GetDlgItemText(hDlg,IDC_STATICSUBMOUNT,(LPSTR)msg,sizeof(msg)-1)>0)
439     {
440         wsprintf(msgf,msg,cm_back_slash_mount_root,cm_back_slash_mount_root);
441         SetDlgItemText (hDlg, IDC_STATICSUBMOUNT, msgf);
442    }
443    SetDlgItemText (hDlg, IDC_MAP_PATH, szMapping);
444    SetDlgItemText (hDlg, IDC_MAP_DESC, pMap->szSubmount);
445
446    CheckDlgButton (hDlg, IDC_MAP_PERSISTENT, (pMap->chDrive == 0) ? TRUE : (pMap->fPersistent));
447
448    Mapping_OnEnable (hDlg);
449 }
450
451
452 void Mapping_OnOK (HWND hDlg)
453 {
454    PDRIVEMAP pMap = (PDRIVEMAP)GetWindowLongPtr (hDlg, DWLP_USER);
455
456    int iItem = SendDlgItemMessage (hDlg, IDC_MAP_LETTER, CB_GETCURSEL, 0, 0);
457    int iDrive = SendDlgItemMessage (hDlg, IDC_MAP_LETTER, CB_GETITEMDATA, iItem, 0);
458
459    pMap->chDrive = chDRIVE_A + iDrive;
460    GetDlgItemText (hDlg, IDC_MAP_PATH, pMap->szMapping, MAX_PATH);
461    GetDlgItemText (hDlg, IDC_MAP_DESC, pMap->szSubmount, MAX_PATH);
462    pMap->fPersistent = IsDlgButtonChecked (hDlg, IDC_MAP_PERSISTENT);
463
464    if (pMap->szSubmount[0] && !IsValidSubmountName (pMap->szSubmount))
465       {
466       int idsTitle = (g.fIsWinNT) ? IDS_BADSUB_TITLE : IDS_BADSUB_TITLE_95;
467       Message (MB_ICONHAND, idsTitle, IDS_BADSUB_DESC);
468       return;
469       }
470
471    if ( (lstrncmpi (pMap->szMapping, cm_slash_mount_root, lstrlen(cm_slash_mount_root))) &&     /*TEXT("/afs")*/
472         (lstrncmpi (pMap->szMapping, cm_back_slash_mount_root, lstrlen(cm_back_slash_mount_root))) ) /*TEXT("\\afs")*/
473    {
474      Message (MB_ICONHAND, IDS_BADMAP_TITLE, IDS_BADMAP_DESC);
475      return;
476    }
477
478    WriteActiveMap(pMap->chDrive, pMap->fPersistent);
479    EndDialog (hDlg, IDOK);
480 }
481
482
483 void Mapping_OnEnable (HWND hDlg)
484 {
485    TCHAR szPath[ MAX_PATH ];
486    GetDlgItemText (hDlg, IDC_MAP_PATH, szPath, MAX_PATH);
487    EnableWindow (GetDlgItem (hDlg, IDOK), (szPath[0] != TEXT('\0')));
488 }
489