Windows: remove trailing whitespace
[openafs.git] / src / WINNT / afsusrmgr / usr_create.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 "TaAfsUsrMgr.h"
16 #include "usr_prop.h"
17 #include "usr_create.h"
18
19
20 /*
21  * DEFINITIONS ________________________________________________________________
22  *
23  */
24
25 typedef struct
26    {
27    TCHAR szPassword[ cchRESOURCE ];
28    int uid;
29    USERPROPINFO Advanced;
30    } CREATEUSERDLG, *LPCREATEUSERDLG;
31
32 #define UID_AUTOSELECT  ((int)0)
33
34
35 /*
36  * PROTOTYPES _________________________________________________________________
37  *
38  */
39
40 BOOL CALLBACK User_Create_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp);
41 void User_Create_OnInitDialog (HWND hDlg);
42 void User_Create_OnNames (HWND hDlg);
43 void User_Create_OnID (HWND hDlg);
44 void User_Create_OnAdvanced (HWND hDlg);
45 BOOL User_Create_OnOK (HWND hDlg);
46 void User_Create_OnEndTask_ObjectGet (HWND hDlg, LPTASKPACKET ptp);
47
48
49 /*
50  * ROUTINES ___________________________________________________________________
51  *
52  */
53
54 void User_SetDefaultCreateParams (LPUSERPROPINFO lpp)
55 {
56    memset (lpp, 0x00, sizeof(USERPROPINFO));
57    lpp->fSeal = FALSE;
58    lpp->fAdmin = FALSE;
59    lpp->fGrantTickets = TRUE;
60    lpp->csecLifetime = csecTICKETLIFETIME_DEFAULT;
61    lpp->cGroupQuota = cGROUPQUOTA_DEFAULT;
62    lpp->fExpires = FALSE;
63    lpp->aaStatus = aaANYONE;
64    lpp->aaOwned = aaANYONE;
65    lpp->aaMember = aaANYONE;
66    lpp->fCreateKAS = TRUE;
67    lpp->fCreatePTS = TRUE;
68    lpp->fCanChangePw = TRUE;
69    lpp->fCanReusePw = FALSE;
70    lpp->cdayPwExpires = 0;
71    lpp->cFailLock = 0;
72    lpp->csecFailLock = 0;
73 }
74
75
76 void User_ShowCreate (HWND hParent)
77 {
78    LPCREATEUSERDLG lpp = New (CREATEUSERDLG);
79    memset (lpp, 0x00, sizeof(CREATEUSERDLG));
80    memcpy (&lpp->Advanced, &gr.CreateUser, sizeof(USERPROPINFO));
81    lpp->uid = UID_AUTOSELECT;
82    lpp->Advanced.pGroupsMember = NULL;
83    lpp->Advanced.pGroupsOwner = NULL;
84
85    (void)ModalDialogParam (IDD_NEWUSER, hParent, (DLGPROC)User_Create_DlgProc, (LPARAM)lpp);
86
87    if (lpp->Advanced.pGroupsMember)
88       asc_AsidListFree (&lpp->Advanced.pGroupsMember);
89    if (lpp->Advanced.pGroupsOwner)
90       asc_AsidListFree (&lpp->Advanced.pGroupsOwner);
91    Delete (lpp);
92 }
93
94
95 BOOL CALLBACK User_Create_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
96 {
97    if (AfsAppLib_HandleHelp (IDD_NEWUSER, hDlg, msg, wp, lp))
98       return TRUE;
99
100    switch (msg)
101       {
102       case WM_INITDIALOG:
103          SetWindowLongPtr (hDlg, DWLP_USER, lp);
104          User_Create_OnInitDialog (hDlg);
105          break;
106
107       case WM_ENDTASK:
108          LPTASKPACKET ptp;
109          if ((ptp = (LPTASKPACKET)lp) != NULL)
110             {
111             if (ptp->idTask == taskOBJECT_GET)
112                User_Create_OnEndTask_ObjectGet (hDlg, ptp);
113             FreeTaskPacket (ptp);
114             }
115          break;
116
117       case WM_COMMAND:
118          switch (LOWORD(wp))
119             {
120             case IDOK:
121                if (User_Create_OnOK (hDlg))
122                   EndDialog (hDlg, IDOK);
123                break;
124
125             case IDCANCEL:
126                EndDialog (hDlg, IDCANCEL);
127                break;
128
129             case IDC_ADVANCED:
130                User_Create_OnAdvanced (hDlg);
131                break;
132
133             case IDC_NEWUSER_NAME:
134                User_Create_OnNames (hDlg);
135                break;
136
137             case IDC_NEWUSER_ID_AUTO:
138             case IDC_NEWUSER_ID_MANUAL:
139                User_Create_OnID (hDlg);
140                break;
141             }
142          break;
143       }
144
145    return FALSE;
146 }
147
148
149 void User_Create_OnInitDialog (HWND hDlg)
150 {
151    LPCREATEUSERDLG lpp = (LPCREATEUSERDLG)GetWindowLongPtr (hDlg, DWLP_USER);
152
153    // Fix the title of the dialog
154    //
155    ULONG status;
156    TCHAR szName[ cchNAME ];
157    asc_CellNameGet_Fast (g.idClient, g.idCell, szName, &status);
158
159    TCHAR szText[ cchRESOURCE ];
160    GetDlgItemText (hDlg, IDC_NEWUSER_TITLE, szText, cchRESOURCE);
161
162    LPTSTR pszText = FormatString (szText, TEXT("%s"), szName);
163    SetDlgItemText (hDlg, IDC_NEWUSER_TITLE, pszText);
164    FreeString (pszText);
165
166    // Attach a spinner to the ID control
167    //
168    CheckDlgButton (hDlg, IDC_NEWUSER_ID_AUTO, TRUE);
169    CreateSpinner (GetDlgItem (hDlg, IDC_NEWUSER_ID), 10, FALSE, 1, 1, (int)-1);
170    User_Create_OnID (hDlg);
171
172    StartTask (taskOBJECT_GET, hDlg, (PVOID)(g.idCell));
173 }
174
175
176 void User_Create_OnNames (HWND hDlg)
177 {
178    TCHAR szSeparators[ cchRESOURCE ];
179    GetString (szSeparators, IDS_SEPARATORS);
180    lstrcat (szSeparators, TEXT(" \t"));
181
182    LPTSTR pszNames = GetEditText (GetDlgItem (hDlg, IDC_NEWUSER_NAME));
183    EnableWindow (GetDlgItem (hDlg, IDOK), (pszNames && *pszNames));
184
185    BOOL fMultiple = FALSE;
186    for (LPTSTR psz = pszNames; !fMultiple && psz && *psz; ++psz)
187       {
188       if (lstrchr (szSeparators, *psz))
189          fMultiple = TRUE;
190       }
191    FreeString (pszNames);
192
193    EnableWindow (GetDlgItem (hDlg, IDC_NEWUSER_ID_AUTO), !fMultiple);
194    EnableWindow (GetDlgItem (hDlg, IDC_NEWUSER_ID_MANUAL), !fMultiple);
195    if (fMultiple)
196       {
197       CheckDlgButton (hDlg, IDC_NEWUSER_ID_AUTO, TRUE);
198       CheckDlgButton (hDlg, IDC_NEWUSER_ID_MANUAL, FALSE);
199       User_Create_OnID (hDlg);
200       }
201 }
202
203
204 void User_Create_OnID (HWND hDlg)
205 {
206    BOOL fEnable = IsDlgButtonChecked (hDlg, IDC_NEWUSER_ID_MANUAL);
207    EnableWindow (GetDlgItem (hDlg, IDC_NEWUSER_ID), fEnable);
208 }
209
210
211 void User_Create_OnAdvanced (HWND hDlg)
212 {
213    LPCREATEUSERDLG lpp = (LPCREATEUSERDLG)GetWindowLongPtr (hDlg, DWLP_USER);
214    lpp->Advanced.pUserList = NULL;
215    lpp->Advanced.fDeleteMeOnClose = FALSE;
216    lpp->Advanced.fShowModal = TRUE;
217    lpp->Advanced.hParent = hDlg;
218    lpp->Advanced.fMachine = FALSE;
219    lpp->Advanced.fApplyGeneral = FALSE;
220    lpp->Advanced.fApplyAdvanced = FALSE;
221    User_ShowProperties (&lpp->Advanced, uptMEMBERSHIP);
222 }
223
224
225 BOOL User_Create_OnOK (HWND hDlg)
226 {
227    LPCREATEUSERDLG lpp = (LPCREATEUSERDLG)GetWindowLongPtr (hDlg, DWLP_USER);
228
229    // First do a little validation of the dialog's entries
230    //
231    TCHAR szPw1[ cchRESOURCE ];
232    GetDlgItemText (hDlg, IDC_NEWUSER_PW1, szPw1, cchRESOURCE);
233    TCHAR szPw2[ cchRESOURCE ];
234    GetDlgItemText (hDlg, IDC_NEWUSER_PW2, szPw2, cchRESOURCE);
235
236    if (!szPw1[0])
237       {
238       ErrorDialog (ERROR_INVALID_PASSWORDNAME, IDS_ERROR_NO_PASSWORD_GIVEN);
239       return FALSE;
240       }
241    if (lstrcmp (szPw1, szPw2))
242       {
243       ErrorDialog (ERROR_INVALID_PASSWORDNAME, IDS_ERROR_MISMATCH_PASSWORD_GIVEN);
244       return FALSE;
245       }
246
247    // Then start a background task to do all this work.
248    //
249    LPUSER_CREATE_PARAMS pTask = New (USER_CREATE_PARAMS);
250    memset (pTask, 0x00, sizeof(USER_CREATE_PARAMS));
251
252    lstrcpy (pTask->szPassword, szPw1);
253
254    if (IsDlgButtonChecked (hDlg, IDC_NEWUSER_ID_AUTO))
255       pTask->idUser = UID_AUTOSELECT;
256    else // (IsDlgButtonChecked (hDlg, IDC_NEWUSER_ID_MANUAL))
257       pTask->idUser = SP_GetPos (GetDlgItem (hDlg, IDC_NEWUSER_ID));
258
259    pTask->Properties.fIsAdmin = lpp->Advanced.fAdmin;
260    pTask->Properties.fCanGetTickets = lpp->Advanced.fGrantTickets;
261    pTask->Properties.fEncrypt = lpp->Advanced.fSeal;
262    pTask->Properties.fCanChangePassword = lpp->Advanced.fCanChangePw;
263    pTask->Properties.fCanReusePasswords = lpp->Advanced.fCanReusePw;
264    memcpy (&pTask->Properties.timeExpires, &lpp->Advanced.stExpires, sizeof(SYSTEMTIME));
265    pTask->Properties.cdayPwExpire = lpp->Advanced.cdayPwExpires;
266    pTask->Properties.csecTicketLifetime = lpp->Advanced.csecLifetime;
267    pTask->Properties.cFailLogin = lpp->Advanced.cFailLock;
268    pTask->Properties.csecFailLoginLock = lpp->Advanced.csecFailLock;
269    pTask->Properties.cgroupCreationQuota = lpp->Advanced.cGroupQuota;
270    pTask->Properties.aaListStatus = lpp->Advanced.aaStatus;
271    pTask->Properties.aaGroupsOwned = lpp->Advanced.aaOwned;
272    pTask->Properties.aaMembership = lpp->Advanced.aaMember;
273    pTask->fCreateKAS = lpp->Advanced.fCreateKAS;
274    pTask->fCreatePTS = lpp->Advanced.fCreatePTS;
275
276    if (lpp->Advanced.pGroupsMember)
277       asc_AsidListCopy (&pTask->pGroupsMember, &lpp->Advanced.pGroupsMember);
278    else
279       pTask->pGroupsMember = NULL;
280
281    if (lpp->Advanced.pGroupsOwner)
282       asc_AsidListCopy (&pTask->pGroupsOwner, &lpp->Advanced.pGroupsOwner);
283    else
284       pTask->pGroupsOwner = NULL;
285
286    // Crack the specified list of user names into a multi-string
287    //
288    TCHAR szSeparators[ cchRESOURCE ];
289    GetString (szSeparators, IDS_SEPARATORS);
290    lstrcat (szSeparators, TEXT(" \t"));
291
292    LPTSTR pszNames = GetEditText (GetDlgItem (hDlg, IDC_NEWUSER_NAME));
293    LPCTSTR pszStart = pszNames;
294    while (lstrchr (szSeparators, *pszStart))
295       ++pszStart;
296
297    while (*pszStart)
298       {
299       // Find the first non-name character
300       //
301       LPCTSTR pszEnd = pszStart;
302       while (*pszEnd && !lstrchr(szSeparators, *pszEnd))
303          ++pszEnd;
304
305       // Copy off this particular name
306       //
307       TCHAR szName[ cchNAME ];
308       lstrcpy (szName, pszStart);
309       szName[ pszEnd - pszStart ] = TEXT('\0');
310
311       if (szName[0])
312          FormatMultiString  (&pTask->mszNames, FALSE, TEXT("%1"), TEXT("%s"), szName);
313
314       // Find the next valid-name character
315       //
316       pszStart = pszEnd;
317       while (lstrchr(szSeparators, *pszStart))
318          ++pszStart;
319       }
320    FreeString (pszNames);
321
322    // Do the real work of creating the user(s)
323    //
324    StartTask (taskUSER_CREATE, NULL, pTask);
325
326    // Store these creation parameters as the new defaults
327    //
328    memcpy (&gr.CreateUser, &lpp->Advanced, sizeof(USERPROPINFO));
329    return TRUE;
330 }
331
332
333 void User_Create_OnEndTask_ObjectGet (HWND hDlg, LPTASKPACKET ptp)
334 {
335    if (ptp->rc)
336       {
337       TCHAR szText[ cchRESOURCE ];
338       GetDlgItemText (hDlg, IDC_NEWUSER_ID_AUTO, szText, cchRESOURCE);
339
340       LPTSTR pszText = FormatString (TEXT("%1 (%2)"), TEXT("%s%ld"), szText, TASKDATA(ptp)->Properties.u.CellProperties.idUserMax+1);
341       SetDlgItemText (hDlg, IDC_NEWUSER_ID_AUTO, pszText);
342       FreeString (pszText);
343
344       if (!IsWindowEnabled (GetDlgItem (hDlg, IDC_NEWUSER_ID)))
345          SP_SetPos (GetDlgItem (hDlg, IDC_NEWUSER_ID), TASKDATA(ptp)->Properties.u.CellProperties.idUserMax+1);
346       }
347 }
348