summaryrefslogtreecommitdiffstats
path: root/private/windows/gina/policy/poledit/user.c
blob: e54cf3259acff485633e1e41d05fa01cbc80e1ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//*********************************************************************
//*                  Microsoft Windows                               **
//*            Copyright(c) Microsoft Corp., 1994                    **
//*********************************************************************

#include "admincfg.h"
#include "regstr.h"

BOOL InitSettingData(TABLEENTRY * pTableEntry,SETTINGDATA *pSettingData);
HGLOBAL AllocateUser(CHAR * szName,DWORD dwType);
VOID RemoveDeletedUser(USERHDR * pUserHdr);

USERHDR * pDeletedUserList = NULL;
UINT nDeletedUserCount = 0;

/*******************************************************************

	NAME:		AddUser

	SYNOPSIS:	Allocates a user and adds it to the list control.

********************************************************************/
HGLOBAL AddUser(HWND hwndList,CHAR * szName,DWORD dwType)
{
	HGLOBAL hUser;
	LV_ITEM lvi;
	UINT uStrID=0;

	if (!(hUser = AllocateUser(szName,dwType))) return NULL;

	lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_PARAM;
	lvi.iItem = 0;
	lvi.iSubItem = 0;
	lvi.state = 0;
        lvi.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
	lvi.pszText = szName;
	lvi.cchTextMax = lstrlen(szName)+1;
	lvi.iImage = GetUserImageIndex(dwType);
	lvi.lParam = (LPARAM) hUser;

	if (dwAppState & AS_POLICYFILE) {
		// if a "default user/default workstation" entry exists, init the new
		// user/computer with that data
		switch (dwType & UT_MASK) {
			case UT_USER:
				uStrID = IDS_DEFAULTUSER;
				break;

			case UT_MACHINE:
				uStrID = IDS_DEFAULTCOMPUTER;
			break;
		}

		if (uStrID) {
			LV_FINDINFO lvfi;
			CHAR szDefaultName[USERNAMELEN+1];
			int iSel=-1;
			HGLOBAL hDefaultUser;
			USERDATA * pUserData;
			BOOL fFound=FALSE;

			LoadSz(uStrID,szDefaultName,sizeof(szDefaultName));

			lvfi.flags = LVFI_STRING | LVFI_NOCASE;
			lvfi.psz = szDefaultName;

			while (!fFound && (iSel=ListView_FindItem(hwndUser,iSel,&lvfi)) >= 0) {
				hDefaultUser=(HGLOBAL) ListView_GetItemParm(hwndUser,iSel);
				if (hDefaultUser && (pUserData = (USERDATA *)
					GlobalLock(hDefaultUser))) {
					if ( (pUserData->hdr.dwType & UT_MASK) == dwType)
						fFound = TRUE;
					GlobalUnlock(hDefaultUser);
				}
			}

			// copy default user data to new user
			if (fFound)
				CopyUser(hDefaultUser,hUser);

		}
	}

	if (ListView_InsertItem(hwndList,&lvi) < 0) {
	  	FreeUser(hUser);
		return NULL;
	}

	return hUser;	
}

/*******************************************************************

	NAME:		AllocateUser

	SYNOPSIS:	Allocates memory for user information and initializes
				the data area

********************************************************************/
HGLOBAL AllocateUser(CHAR * szName,DWORD dwType)
{
	HGLOBAL hMem;
	USERDATA * pUserData;
	DWORD dwNeeded;
	TABLEENTRY * pTableEntry;

	dwNeeded = sizeof(USERDATA) + sizeof(SETTINGDATA) *
		( (dwType & UT_USER) ? gClassList.nUserDataItems :
			gClassList.nMachineDataItems );

	if (!(hMem=GlobalAlloc(GHND,dwNeeded))) return NULL;

	if (!(pUserData = (USERDATA *) GlobalLock(hMem))) {
		GlobalFree(hMem);
		return NULL;
	}

	// init the data structure
	pUserData->dwSize = dwNeeded;
	pUserData->hdr.dwType = dwType;
	pUserData->nSettings = ( (dwType & UT_USER)
		? gClassList.nUserDataItems : gClassList.nMachineDataItems );
	lstrcpy(pUserData->hdr.szName,szName);

	pTableEntry = ( ( dwType & UT_USER ) ? gClassList.pUserCategoryList :
		gClassList.pMachineCategoryList);

	if (!InitSettingData(pTableEntry,pUserData->SettingData)) {
		GlobalUnlock(hMem);
		GlobalFree(hMem);
		return NULL;						 		
	}

	// remove this user from the deleted list, if it's on it
	RemoveDeletedUser(&pUserData->hdr);

	GlobalUnlock(hMem);
	return hMem;
}

/*******************************************************************

	NAME:		CloneUser

	SYNOPSIS:	Clones the specified user by allocating a duplicate buffer,
				copying the data to the 2nd buffer and storing a handle to
				the new buffer in the original.  This is so the initial state
				of a user can be preserved so we can do non-destructive saves.

********************************************************************/
BOOL CloneUser(HGLOBAL hUser)
{
	USERDATA * pUserData = (USERDATA *) GlobalLock(hUser);
	USERDATA * pUserDataClone;
	HGLOBAL  hUserClone;
	
	if (!pUserData) return FALSE;

	// free existing clone if there is one
	if (pUserData->hClone) {
		GlobalFree(pUserData->hClone);
		pUserData->hClone = NULL;
	}

	hUserClone = GlobalAlloc(GHND,pUserData->dwSize);
	if (!hUserClone || !(pUserDataClone = (USERDATA *) GlobalLock(hUserClone))) {
		if (hUserClone)
			GlobalFree(hUserClone);
		GlobalUnlock(hUser);
		return FALSE;
	}

	// copy the user data to clone and save handle to clone in original user
	memcpy (pUserDataClone,pUserData,pUserData->dwSize);
	pUserData->hClone = hUserClone;

	GlobalUnlock(hUser);
	GlobalUnlock(hUserClone);

	return TRUE;
}

BOOL CopyUser(HGLOBAL hUserSrc,HGLOBAL hUserDst)
{
	BOOL fRet=FALSE;
	USERDATA * pUserDataSrc,* pUserDataDst;
	HGLOBAL hCloneDst;
	USERHDR UserhdrDst;

	if (!(pUserDataSrc = (USERDATA *) GlobalLock(hUserSrc)) ||
		!(pUserDataDst = (USERDATA *) GlobalLock(hUserDst))) {
			goto cleanup;
		}

	// save destination user header & clone handle.. don't want to overwrite
	// this stuff
	UserhdrDst = pUserDataDst->hdr;
	hCloneDst = pUserDataDst->hClone;

	// resize buffer if necessary
	if (pUserDataDst->dwSize != pUserDataSrc->dwSize) {
		if (!(pUserDataDst = (USERDATA *) ResizeBuffer((CHAR *) pUserDataDst,
			hUserDst,pUserDataSrc->dwSize,&pUserDataDst->dwSize))) {
			goto cleanup;
		}
	}

	// copy src to dest
	memcpy(pUserDataDst,pUserDataSrc,pUserDataSrc->dwSize);
	fRet = TRUE;

	// put the destination user's old header & clone handle back in the
	// user data
	pUserDataDst->hClone = hCloneDst;
	pUserDataDst->hdr = UserhdrDst;
	
cleanup:
	if (pUserDataSrc)
		GlobalUnlock(hUserSrc);
	if (pUserDataDst)
		GlobalUnlock(hUserDst);

	return fRet;
}

/*******************************************************************

	NAME:		InitSettingData

	SYNOPSIS:	Walks a template tree and initializes user's data buffer
				according to template tree

	NOTES:		Sets checkboxes to "indeterminate", puts default text
				in data buffer, etc.

********************************************************************/
BOOL InitSettingData(TABLEENTRY * pTableEntry,SETTINGDATA *pSettingData)
{
	while (pTableEntry) {

		if (pTableEntry->pChild &&
			!InitSettingData(pTableEntry->pChild,pSettingData))
			return FALSE;

		if (pTableEntry->dwType == ETYPE_POLICY) {

			pSettingData[ ( (POLICY *) pTableEntry)->uDataIndex].dwType =
				pTableEntry->dwType;

			pSettingData[ ( (POLICY *) pTableEntry)->uDataIndex].uData
				= (dwAppState & AS_POLICYFILE ? IMG_INDETERMINATE :
				IMG_UNCHECKED);

		} else if ((pTableEntry->dwType & ETYPE_MASK) == ETYPE_SETTING) {
			SETTINGS * pSetting = (SETTINGS *) pTableEntry;

			pSettingData[pSetting->uDataIndex].dwType =
				pTableEntry->dwType;

			switch (pTableEntry->dwType & STYPE_MASK) {
				
				case STYPE_EDITTEXT:
				case STYPE_COMBOBOX:
					if (pSetting->dwFlags & DF_USEDEFAULT) {
						pSettingData[pSetting->uDataIndex].uData =
						INIT_WITH_DEFAULT;
					break;

				case STYPE_CHECKBOX:
					pSettingData[pSetting->uDataIndex].uData =
						( (pSetting->dwFlags & DF_USEDEFAULT) ? 1 : 0);
					break;

				case STYPE_NUMERIC:
					pSettingData[pSetting->uDataIndex].uData =
						( (pSetting->dwFlags & DF_USEDEFAULT) ?
						(((NUMERICINFO *) GETOBJECTDATAPTR(pSetting)))
						->uDefValue : NO_VALUE);
					break;

				case STYPE_DROPDOWNLIST:
					pSettingData[pSetting->uDataIndex].uData =
						( (pSetting->dwFlags & DF_USEDEFAULT) ?
						(((DROPDOWNINFO *) GETOBJECTDATAPTR(pSetting)))
						->uDefaultItemIndex : NO_VALUE);

					break;

				}

			}
		}

		pTableEntry = pTableEntry->pNext;
	}

	return TRUE;
}

/*******************************************************************

	NAME:		FreeUser

	SYNOPSIS:	Frees a user buffer.  If it has a clone, frees the clone too.

********************************************************************/
BOOL FreeUser(HGLOBAL hUser)
{
	USERDATA * pUserData;

	// free user's clone if one exists
	if (pUserData = (USERDATA *) GlobalLock(hUser)) {
		if (pUserData->hClone) {
			if (GlobalFlags(pUserData->hClone))
				GlobalUnlock(pUserData->hClone);
			GlobalFree(pUserData->hClone);
		}
		GlobalUnlock(hUser);
	}
	if (GlobalFlags(hUser))
		GlobalUnlock(hUser);
	GlobalFree(hUser);

	return TRUE;
}

BOOL RemoveAllUsers(HWND hwndList)
{
	UINT nIndex,nCount = ListView_GetItemCount(hwndList);

	for (nIndex=0;nIndex<nCount;nIndex++)
		RemoveUser(hwndList,0,FALSE);

#ifdef INCL_GROUP_SUPPORT
	FreeGroupPriorityList();
#endif

	return TRUE;
}

BOOL RemoveUser(HWND hwndList,UINT nIndex,BOOL fMarkDeleted)
{
	HGLOBAL hUser;

	hUser = (HGLOBAL) ListView_GetItemParm(hwndList,nIndex);
	if (!hUser) return FALSE;

	if (fMarkDeleted) {
		USERHDR UserHdr;

		if (!GetUserHeader(hUser,&UserHdr))
			return FALSE;
		if (!AddDeletedUser(&UserHdr))
			return FALSE;
	}
	
	FreeUser(hUser);
	ListView_DeleteItem(hwndList,nIndex);

        return TRUE;
}

BOOL SortUsers(VOID)
{

	ListView_Arrange(hwndUser,LVA_SORTASCENDING);

	return TRUE;
}

BOOL AddDefaultUsers(HWND hwndList)
{	
	if (!AddUser(hwndList,LoadSz(IDS_DEFAULTUSER,szSmallBuf,sizeof(szSmallBuf)),
		UT_USER)) return FALSE;
	if (!AddUser(hwndList,LoadSz(IDS_DEFAULTCOMPUTER,szSmallBuf,sizeof(szSmallBuf)),
		UT_MACHINE)) return FALSE;

	return TRUE;
}

BOOL GetUserHeader(HGLOBAL hUser,USERHDR * pUserHdr)
{
	USERDATA * pUserData;

	if (!(pUserData = (USERDATA *) GlobalLock(hUser)))
		return FALSE;

	*pUserHdr = pUserData->hdr;

	GlobalUnlock(hUser);

	return TRUE;
}

UINT GetUserImageIndex(DWORD dwUserType)
{
	return ( (dwUserType & UT_USER ?
		( (dwUserType & UF_GROUP) ? IMG_USERS : IMG_USER) :
		  (dwUserType & UF_GROUP) ? IMG_MACHINES : IMG_MACHINE ) );
}

// when a user is deleted from the main window, we can't just delete it from
// the file right away, so add the user to a list of people we've deleted.
// If/when the user saves, we'll commit the changes and delete them from the 
// file.
BOOL AddDeletedUser(USERHDR * pUserHdr)
{
	// alloc/realloc buffer to hold copy of the user header
	if (!pDeletedUserList) {
		pDeletedUserList = (USERHDR *) GlobalAlloc(GPTR,sizeof(USERHDR));
	} else {
	 	pDeletedUserList = (USERHDR *) GlobalReAlloc((HGLOBAL) pDeletedUserList,
			sizeof(USERHDR) * (nDeletedUserCount + 1),GMEM_ZEROINIT | GMEM_MOVEABLE);
	}

	if (!pDeletedUserList)
		return FALSE;	// out of memory

	pDeletedUserList[nDeletedUserCount] = *pUserHdr;
	nDeletedUserCount++;

        return TRUE;
}

USERHDR * GetDeletedUser(UINT nIndex)
{
	if (!pDeletedUserList || nIndex >= nDeletedUserCount)
		return NULL;
	
	return &pDeletedUserList[nIndex];
}

// when adding a user, check to see if it's on the deleted list
// and if so remove it.  (ex: add "joe", remove "joe", add "joe".
// on 2nd add, have to remove "joe" from deleted list)
VOID RemoveDeletedUser(USERHDR * pUserHdr)
{
	UINT nIndex;

	for (nIndex=0;nIndex<nDeletedUserCount;nIndex++) {
		// got a match?
		if (!lstrcmpi(pDeletedUserList[nIndex].szName,
			pUserHdr->szName) && (pDeletedUserList[nIndex].dwType ==
			pUserHdr->dwType)) {

			// move subsequent entries up one slot in the array
			nDeletedUserCount --;
			while (nIndex<nDeletedUserCount) {
				pDeletedUserList[nIndex] = pDeletedUserList[nIndex+1];
				nIndex++;
			}
		}
	}
}

VOID ClearDeletedUserList(VOID)
{
	if (pDeletedUserList)
		GlobalFree(pDeletedUserList);

	pDeletedUserList = NULL;
	nDeletedUserCount = 0;
}

// maps the localized default entries in resource ("default user",
// "default computer") to hard-coded non-localized name (".default").
// Other names are unchanged (szMappedName returned same as szUserName)
VOID MapUserName(CHAR * szUserName,CHAR * szMappedName)
{
	if (!lstrcmpi(szUserName,LoadSz(IDS_DEFAULTUSER,szSmallBuf,
		sizeof(szSmallBuf))) ||
		!lstrcmpi(szUserName,LoadSz(IDS_DEFAULTCOMPUTER,szSmallBuf,
		sizeof(szSmallBuf)))) {
		lstrcpy(szMappedName,szDEFAULTENTRY);
	}
	else lstrcpy(szMappedName,szUserName);

}

// unmaps the ".default" hard-coded entry to the localized text
// "default user" or "default computer"
VOID UnmapUserName(CHAR * szMappedName,CHAR * szUserName,BOOL fUser)
{

	if (!lstrcmpi(szMappedName,szDEFAULTENTRY)) {
		lstrcpy(szUserName,LoadSz( (fUser ? IDS_DEFAULTUSER :
			IDS_DEFAULTCOMPUTER),szSmallBuf,sizeof(szSmallBuf)));
	} else {
		lstrcpy(szUserName,szMappedName);
	}
}