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