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