Initial IBM OpenAFS 1.0 tree
[openafs.git] / src / WINNT / afsusrmgr / usr_cpw.cpp
1 extern "C" {
2 #include <afs/param.h>
3 #include <afs/stds.h>
4 }
5
6 #include "TaAfsUsrMgr.h"
7 #include "usr_cpw.h"
8 #include "usr_col.h"
9
10
11 /*
12  * DEFINITIONS ________________________________________________________________
13  *
14  */
15
16
17 /*
18  * PROTOTYPES _________________________________________________________________
19  *
20  */
21
22 BOOL CALLBACK User_Password_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp);
23 void User_Password_OnInitDialog (HWND hDlg);
24 BOOL User_Password_OnOK (HWND hDlg);
25 void User_Password_OnSelectVer (HWND hDlg);
26 void User_Password_OnSelectType (HWND hDlg);
27 void User_Password_OnType (HWND hDlg);
28 void User_Password_OnRandom (HWND hDlg);
29 void User_Password_OnEndTask_Random (HWND hDlg, LPTASKPACKET ptp);
30
31
32 /*
33  * ROUTINES ___________________________________________________________________
34  *
35  */
36
37 void User_ShowChangePassword (HWND hParent, ASID idUser)
38 {
39    ModalDialogParam (IDD_USER_PASSWORD, hParent, (DLGPROC)User_Password_DlgProc, (LPARAM)idUser);
40 }
41
42
43 BOOL CALLBACK User_Password_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
44 {
45    if (AfsAppLib_HandleHelp (IDD_USER_PASSWORD, hDlg, msg, wp, lp))
46       return TRUE;
47
48    switch (msg)
49       {
50       case WM_INITDIALOG:
51          SetWindowLong (hDlg, DWL_USER, lp);
52          User_Password_OnInitDialog (hDlg);
53          break;
54
55       case WM_ENDTASK:
56          LPTASKPACKET ptp;
57          if ((ptp = (LPTASKPACKET)lp) != NULL)
58             {
59             if (ptp->idTask == taskGET_RANDOM_KEY)
60                User_Password_OnEndTask_Random (hDlg, ptp);
61             FreeTaskPacket (ptp);
62             }
63          break;
64
65       case WM_COMMAND:
66          switch (LOWORD(wp))
67             {
68             case IDOK:
69                if (User_Password_OnOK (hDlg))
70                   EndDialog (hDlg, IDOK);
71                break;
72
73             case IDCANCEL:
74                EndDialog (hDlg, IDCANCEL);
75                break;
76
77             case IDC_CPW_VERSION_AUTO:
78             case IDC_CPW_VERSION_MANUAL:
79                User_Password_OnSelectVer (hDlg);
80                break;
81
82             case IDC_CPW_BYSTRING:
83             case IDC_CPW_BYDATA:
84                User_Password_OnSelectType (hDlg);
85                break;
86
87             case IDC_CPW_STRING:
88             case IDC_CPW_DATA:
89                User_Password_OnType (hDlg);
90                break;
91
92             case IDC_CPW_RANDOM:
93                User_Password_OnRandom (hDlg);
94                break;
95             }
96          break;
97       }
98
99    return FALSE;
100 }
101
102
103 void User_Password_OnInitDialog (HWND hDlg)
104 {
105    ASID idUser = (ASID)GetWindowLong (hDlg, DWL_USER);
106
107    // Get the current properties for this user
108    //
109    ULONG status;
110    TCHAR szName[ cchNAME ];
111    User_GetDisplayName (szName, idUser);
112
113    ASOBJPROP Properties;
114    if (!asc_ObjectPropertiesGet_Fast (g.idClient, g.idCell, idUser, &Properties, &status))
115       {
116       ErrorDialog (status, IDS_ERROR_CANT_GET_USERPROP, TEXT("%s"), szName);
117       EndDialog (hDlg, IDCANCEL);
118       return;
119       }
120
121    // Fill in the text at the top of the dialog
122    //
123    TCHAR szOld[ cchRESOURCE ];
124    GetDlgItemText (hDlg, IDC_CPW_TITLE, szOld, cchRESOURCE);
125
126    LPTSTR pszText = FormatString (szOld, TEXT("%s"), szName);
127    SetDlgItemText (hDlg, IDC_CPW_TITLE, pszText);
128    FreeString (pszText);
129
130    // Check the appropriate radio buttons, etc
131    //
132    CheckDlgButton (hDlg, IDC_CPW_VERSION_AUTO, TRUE);
133    CheckDlgButton (hDlg, IDC_CPW_BYSTRING, TRUE);
134    CreateSpinner (GetDlgItem (hDlg, IDC_CPW_VERSION), 10, FALSE, 1, 1+Properties.u.UserProperties.KASINFO.keyVersion, 255);
135    User_Password_OnSelectType (hDlg);
136    User_Password_OnSelectVer (hDlg);
137 }
138
139
140 void User_Password_OnSelectVer (HWND hDlg)
141 {
142    EnableWindow (GetDlgItem (hDlg, IDC_CPW_VERSION), IsDlgButtonChecked (hDlg, IDC_CPW_VERSION_MANUAL));
143 }
144
145
146 void User_Password_OnSelectType (HWND hDlg)
147 {
148    EnableWindow (GetDlgItem (hDlg, IDC_CPW_STRING), IsDlgButtonChecked (hDlg, IDC_CPW_BYSTRING));
149    EnableWindow (GetDlgItem (hDlg, IDC_CPW_DATA),   IsDlgButtonChecked (hDlg, IDC_CPW_BYDATA));
150    EnableWindow (GetDlgItem (hDlg, IDC_CPW_RANDOM), IsDlgButtonChecked (hDlg, IDC_CPW_BYDATA));
151    User_Password_OnType (hDlg);
152 }
153
154
155 void User_Password_OnType (HWND hDlg)
156 {
157    BOOL fEnable = FALSE;
158
159    if (IsDlgButtonChecked (hDlg, IDC_CPW_BYSTRING))
160       {
161       TCHAR szKey[ cchRESOURCE ];
162       GetDlgItemText (hDlg, IDC_CPW_STRING, szKey, cchRESOURCE);
163       if (szKey[0] != TEXT('\0'))
164          fEnable = TRUE;
165       }
166    else // (IsDlgButtonChecked (hDlg, IDC_CPW_BYDATA))
167       {
168       TCHAR szKey[ cchRESOURCE ];
169       GetDlgItemText (hDlg, IDC_CPW_DATA, szKey, cchRESOURCE);
170
171       BYTE key[ ENCRYPTIONKEYLENGTH ];
172       if (ScanServerKey (key, szKey))
173          fEnable = TRUE;
174       }
175
176    EnableWindow (GetDlgItem (hDlg, IDOK), fEnable);
177 }
178
179
180 void User_Password_OnRandom (HWND hDlg)
181 {
182    StartTask (taskGET_RANDOM_KEY, hDlg, 0);
183 }
184
185
186 BOOL User_Password_OnOK (HWND hDlg)
187 {
188    ASID idUser = (ASID)GetWindowLong (hDlg, DWL_USER);
189
190    LPUSER_CPW_PARAMS lpp = New (USER_CPW_PARAMS);
191    memset (lpp, 0x00, sizeof(USER_CPW_PARAMS));
192    lpp->idUser = idUser;
193
194    if (IsDlgButtonChecked (hDlg, IDC_CPW_VERSION_MANUAL))
195       lpp->keyVersion = SP_GetPos (GetDlgItem (hDlg, IDC_CPW_VERSION));
196    else // (IsDlgButtonChecked (hDlg, IDC_CPW_VERSION_AUTO))
197       lpp->keyVersion = 0;
198
199    if (IsDlgButtonChecked (hDlg, IDC_CPW_BYSTRING))
200       {
201       GetDlgItemText (hDlg, IDC_CPW_STRING, lpp->keyString, cchRESOURCE);
202       }
203    else // (IsDlgButtonChecked (hDlg, IDC_CPW_BYDATA))
204       {
205       TCHAR szKey[ cchRESOURCE ];
206       GetDlgItemText (hDlg, IDC_CPW_DATA, szKey, cchRESOURCE);
207
208       if (!ScanServerKey (lpp->keyData, szKey))
209          {
210          Delete (lpp);
211          return FALSE;
212          }
213       }
214
215    StartTask (taskUSER_CPW, NULL, lpp);
216    return TRUE;
217 }
218
219
220 void User_Password_OnEndTask_Random (HWND hDlg, LPTASKPACKET ptp)
221 {
222    if (!ptp->rc)
223       {
224       ErrorDialog (ptp->status, IDS_ERROR_CANT_GET_RANDOM_KEY);
225       EnableWindow (GetDlgItem (hDlg, IDC_CPW_RANDOM), FALSE);
226       }
227    else
228       {
229       TCHAR szKey[ cchRESOURCE ];
230       FormatServerKey (szKey, TASKDATA(ptp)->key);
231       SetDlgItemText (hDlg, IDC_CPW_DATA, szKey);
232       }
233 }
234