venus: Remove dedebug
[openafs.git] / src / WINNT / afsadmsvr / TaAfsAdmSvrUser.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 <afs/stds.h>
17 #include <roken.h>
18 }
19
20 #include "TaAfsAdmSvrInternal.h"
21
22
23 /*
24  * ROUTINES ___________________________________________________________________
25  *
26  */
27
28
29       // AfsAdmSvr_ChangeUser
30       // ...changes a user account's properties.
31       //
32 extern "C" int AfsAdmSvr_ChangeUser (UINT_PTR idClient, ASID idCell, ASID idUser, LPAFSADMSVR_CHANGEUSER_PARAMS pChange, ULONG *pStatus)
33 {
34    ASACTION Action;
35    Action.Action = ACTION_USER_CHANGE;
36    Action.idClient = idClient;
37    Action.idCell = idCell;
38    Action.u.User_Change.idUser = idUser;
39    size_t iOp = AfsAdmSvr_BeginOperation (idClient, &Action);
40
41    Print (dlDETAIL, TEXT("Client 0x%08lX: ChangeUser (idUser=0x%08lX)"), idClient, idUser);
42
43    if (!AfsAdmSvr_fIsValidClient (idClient))
44       return FALSE_(ERROR_INVALID_PARAMETER,pStatus,iOp);
45
46    // Find this user's current properties
47    //
48    LPASOBJPROP pCurrentProperties;
49    if ((pCurrentProperties = AfsAdmSvr_GetCurrentProperties (idUser, pStatus)) == NULL)
50       {
51       Print (dlERROR, TEXT("Client 0x%08lX: ChangeUser failed; no properties"), idClient);
52       AfsAdmSvr_EndOperation (iOp);
53       return FALSE;
54       }
55
56    // Build an AFSCLASS-style USERPROPERTIES structure that reflects the
57    // new properties for the user; mark the structure's dwMask bit to indicate
58    // what we're changing.
59    //
60    USERPROPERTIES NewProperties;
61    memset (&NewProperties, 0x00, sizeof(NewProperties));
62
63    if ((NewProperties.fAdmin = pChange->fIsAdmin) != pCurrentProperties->u.UserProperties.KASINFO.fIsAdmin)
64       NewProperties.dwMask |= MASK_USERPROP_fAdmin;
65    if ((NewProperties.fGrantTickets = pChange->fCanGetTickets) != pCurrentProperties->u.UserProperties.KASINFO.fCanGetTickets)
66       NewProperties.dwMask |= MASK_USERPROP_fGrantTickets;
67    if ((NewProperties.fCanEncrypt = pChange->fEncrypt) != pCurrentProperties->u.UserProperties.KASINFO.fEncrypt)
68       NewProperties.dwMask |= MASK_USERPROP_fCanEncrypt;
69    if ((NewProperties.fCanChangePassword = pChange->fCanChangePassword) != pCurrentProperties->u.UserProperties.KASINFO.fCanChangePassword)
70       NewProperties.dwMask |= MASK_USERPROP_fCanChangePassword;
71    if ((NewProperties.fCanReusePasswords = pChange->fCanReusePasswords) != pCurrentProperties->u.UserProperties.KASINFO.fCanReusePasswords)
72       NewProperties.dwMask |= MASK_USERPROP_fCanReusePasswords;
73    if ((NewProperties.cdayPwExpires = pChange->cdayPwExpire) != pCurrentProperties->u.UserProperties.KASINFO.cdayPwExpire)
74       NewProperties.dwMask |= MASK_USERPROP_cdayPwExpires;
75    if ((NewProperties.csecTicketLifetime = pChange->csecTicketLifetime) != pCurrentProperties->u.UserProperties.KASINFO.csecTicketLifetime)
76       NewProperties.dwMask |= MASK_USERPROP_csecTicketLifetime;
77    if ((NewProperties.nFailureAttempts = pChange->cFailLogin) != pCurrentProperties->u.UserProperties.KASINFO.cFailLogin)
78       NewProperties.dwMask |= MASK_USERPROP_nFailureAttempts;
79    if ((NewProperties.csecFailedLoginLockTime = pChange->csecFailLoginLock) != pCurrentProperties->u.UserProperties.KASINFO.csecFailLoginLock)
80       NewProperties.dwMask |= MASK_USERPROP_csecFailedLoginLockTime;
81    if ((NewProperties.cGroupCreationQuota = pChange->cgroupCreationQuota) != pCurrentProperties->u.UserProperties.PTSINFO.cgroupCreationQuota)
82       NewProperties.dwMask |= MASK_USERPROP_cGroupCreationQuota;
83    if ((NewProperties.aaListStatus = pChange->aaListStatus) != pCurrentProperties->u.UserProperties.PTSINFO.aaListStatus)
84       NewProperties.dwMask |= MASK_USERPROP_aaListStatus;
85    if ((NewProperties.aaGroupsOwned = pChange->aaGroupsOwned) != pCurrentProperties->u.UserProperties.PTSINFO.aaGroupsOwned)
86       NewProperties.dwMask |= MASK_USERPROP_aaGroupsOwned;
87    if ((NewProperties.aaMembership = pChange->aaMembership) != pCurrentProperties->u.UserProperties.PTSINFO.aaMembership)
88       NewProperties.dwMask |= MASK_USERPROP_aaMembership;
89    memcpy (&NewProperties.timeAccountExpires, &pChange->timeExpires, sizeof(SYSTEMTIME));
90    if (memcmp (&NewProperties.timeAccountExpires, &pCurrentProperties->u.UserProperties.KASINFO.timeExpires, sizeof(SYSTEMTIME)))
91       NewProperties.dwMask |= MASK_USERPROP_timeAccountExpires;
92
93    // If we've decided to change anything, call AfsClass to actually do it
94    //
95    if (NewProperties.dwMask == 0)
96       {
97       Print (dlDETAIL, TEXT("Client 0x%08lX: ChangeUser succeeded (nothing to do)"), idClient);
98       }
99    else
100       {
101       ULONG status;
102       if (!AfsClass_SetUserProperties ((LPIDENT)idUser, &NewProperties, &status))
103          {
104          Print (dlERROR, TEXT("Client 0x%08lX: ChangeUser failed; error 0x%08lX"), idClient, status);
105          return FALSE_(status,pStatus,iOp);
106          }
107
108       Print (dlDETAIL, TEXT("Client 0x%08lX: ChangeUser succeeded"), idClient);
109       }
110
111    AfsAdmSvr_EndOperation (iOp);
112    return TRUE;
113 }
114
115
116       // AfsAdmSvr_SetUserPassword
117       // ...changes the password for the specified user account. Pass a non-empty
118       //    string in {keyString} to encrypt the specified string; otherwise,
119       //    pass a valid encryption key in {keyData}.
120       //
121 extern "C" int AfsAdmSvr_SetUserPassword (UINT_PTR idClient, ASID idCell, ASID idUser, int keyVersion, STRING keyString, BYTE keyData[ ENCRYPTIONKEYLENGTH ], ULONG *pStatus)
122 {
123    BOOL rc = TRUE;
124    ULONG status = 0;
125
126    ASACTION Action;
127    Action.Action = ACTION_USER_PW_CHANGE;
128    Action.idClient = idClient;
129    Action.idCell = idCell;
130    Action.u.User_Pw_Change.idUser = idUser;
131    size_t iOp = AfsAdmSvr_BeginOperation (idClient, &Action);
132
133    Print (dlDETAIL, TEXT("Client 0x%08lX: SetUserPassword (idUser=0x%08lX)"), idClient, idUser);
134
135    if (!AfsAdmSvr_fIsValidClient (idClient))
136       return FALSE_(ERROR_INVALID_PARAMETER,pStatus,iOp);
137
138    // Change the user's password
139    //
140    if (keyString && keyString[0])
141       {
142       rc = AfsClass_SetUserPassword ((LPIDENT)idUser, keyVersion, keyString, &status);
143       }
144    else // (!keyString || !keyString[0])
145       {
146       rc = AfsClass_SetUserPassword ((LPIDENT)idUser, keyVersion, (LPENCRYPTIONKEY)keyData, &status);
147       }
148
149    if (!rc)
150       return FALSE_(status,pStatus,iOp);
151
152    Print (dlDETAIL, TEXT("Client 0x%08lX: SetUserPassword succeeded"), idClient);
153    AfsAdmSvr_EndOperation (iOp);
154    return TRUE;
155 }
156
157
158       // AfsAdmSvr_UnlockUser
159       // ...unlocks a user's account
160       //
161 extern "C" int AfsAdmSvr_UnlockUser (UINT_PTR idClient, ASID idCell, ASID idUser, ULONG *pStatus)
162 {
163    ASACTION Action;
164    Action.Action = ACTION_USER_UNLOCK;
165    Action.idClient = idClient;
166    Action.idCell = idCell;
167    Action.u.User_Unlock.idUser = idUser;
168    size_t iOp = AfsAdmSvr_BeginOperation (idClient, &Action);
169
170    Print (dlDETAIL, TEXT("Client 0x%08lX: UnlockUser (idUser=0x%08lX)"), idClient, idUser);
171
172    if (!AfsAdmSvr_fIsValidClient (idClient))
173       return FALSE_(ERROR_INVALID_PARAMETER,pStatus,iOp);
174
175    // Unlock the user's account
176    //
177    ULONG status;
178    if (!AfsClass_UnlockUser ((LPIDENT)idUser, &status))
179       return FALSE_(status,pStatus,iOp);
180
181    Print (dlDETAIL, TEXT("Client 0x%08lX: UnlockUser succeeded"), idClient);
182    AfsAdmSvr_EndOperation (iOp);
183    return TRUE;
184 }
185
186
187       // AfsAdmSvr_CreateUser
188       // ...creates a new user account
189       //
190 extern "C" int AfsAdmSvr_CreateUser (UINT_PTR idClient, ASID idCell, LPAFSADMSVR_CREATEUSER_PARAMS pCreate, ASID *pidUser, ULONG *pStatus)
191 {
192    ASACTION Action;
193    Action.Action = ACTION_USER_CREATE;
194    Action.idClient = idClient;
195    Action.idCell = idCell;
196    lstrcpy (Action.u.User_Create.szUser, pCreate->szName);
197    lstrcpy (Action.u.User_Create.szInstance, pCreate->szInstance);
198    size_t iOp = AfsAdmSvr_BeginOperation (idClient, &Action);
199
200    Print (dlDETAIL, TEXT("Client 0x%08lX: CreateUser (szUser=%s)"), idClient, pCreate->szName);
201
202    if (!AfsAdmSvr_fIsValidClient (idClient))
203       return FALSE_(ERROR_INVALID_PARAMETER,pStatus,iOp);
204
205    // Create the user account
206    //
207    ULONG status;
208    LPIDENT lpiUser;
209    if ((lpiUser = AfsClass_CreateUser ((LPIDENT)idCell, pCreate->szName, pCreate->szInstance, pCreate->szPassword, pCreate->idUser, pCreate->fCreateKAS, pCreate->fCreatePTS, &status)) == NULL)
210       {
211       Print (dlERROR, TEXT("Client 0x%08lX: CreateUser failed; error 0x%08lX"), idClient, status);
212       return FALSE_(status,pStatus,iOp);
213       }
214
215    if (pidUser)
216       *pidUser = (ASID)lpiUser;
217
218    // Creating a user account may change the max user ID
219    AfsAdmSvr_TestProperties (idCell);
220
221    Print (dlDETAIL, TEXT("Client 0x%08lX: CreateUser succeeded"), idClient);
222    AfsAdmSvr_EndOperation (iOp);
223    return TRUE;
224 }
225
226
227       // AfsAdmSvr_DeleteUser
228       // ...deletes a user's account
229       //
230 extern "C" int AfsAdmSvr_DeleteUser (UINT_PTR idClient, ASID idCell, ASID idUser, LPAFSADMSVR_DELETEUSER_PARAMS pDelete, ULONG *pStatus)
231 {
232    ASACTION Action;
233    Action.Action = ACTION_USER_DELETE;
234    Action.idClient = idClient;
235    Action.idCell = idCell;
236    Action.u.User_Delete.idUser = idUser;
237    size_t iOp = AfsAdmSvr_BeginOperation (idClient, &Action);
238
239    Print (dlDETAIL, TEXT("Client 0x%08lX: DeleteUser (idUser=0x%08lX)"), idClient, idUser);
240
241    if (!AfsAdmSvr_fIsValidClient (idClient))
242       return FALSE_(ERROR_INVALID_PARAMETER,pStatus,iOp);
243
244    // Delete the user's accounts
245    //
246    ULONG status;
247    if (!AfsClass_DeleteUser ((LPIDENT)idUser, pDelete->fDeleteKAS, pDelete->fDeletePTS, &status))
248       {
249       Print (dlERROR, TEXT("Client 0x%08lX: DeleteUser failed; error 0x%08lX"), idClient, status);
250       return FALSE_(status,pStatus,iOp);
251       }
252
253    Print (dlDETAIL, TEXT("Client 0x%08lX: DeleteUser succeeded"), idClient);
254    AfsAdmSvr_EndOperation (iOp);
255    return TRUE;
256 }
257