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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
|
//*********************************************************************
//* Microsoft Windows **
//* Copyright(c) Microsoft Corp., 1993 **
//*********************************************************************
#include "admincfg.h"
BOOL CALLBACK ConnectDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam,
LPARAM lParam);
BOOL InitConnectDlg(HWND hDlg);
BOOL ProcessConnectDlg(HWND hDlg);
TCHAR szRemoteName[COMPUTERNAMELEN+1];
#define MAX_KEY_NAME 200
typedef struct _KEYNODE {
LPTSTR lpUserName;
struct _KEYNODE *pNext;
} KEYNODE, * LPKEYNODE;
BOOL AddKeyNode (LPKEYNODE *lpList, LPTSTR lpUserName);
BOOL FreeKeyList (LPKEYNODE lpList);
typedef struct _USERINFO {
LPTSTR lpComputerName;
LPKEYNODE lpList;
LPKEYNODE lpSelectedItem;
HKEY hkeyRemoteHLM;
} USERINFO, * LPUSERINFO;
// HKEYs to remote registry
HKEY hkeyRemoteHLM = NULL; // remote HKEY_LOCAL_MACHINE
HKEY hkeyRemoteHCU = NULL; // remote HKEY_CURRENT_USER
HKEY hkeyVirtHLM = HKEY_LOCAL_MACHINE; // virtual HKEY_LOCAL_MACHINE
HKEY hkeyVirtHCU = HKEY_CURRENT_USER; // virtual HKEY_CURRENT_USER
BOOL OnConnect(HWND hwndApp,HWND hwndList)
{
if (dwAppState & AS_FILEDIRTY) {
if (!QueryForSave(hwndApp,hwndList)) return TRUE; // user cancelled
}
if (DialogBox(ghInst,MAKEINTRESOURCE(DLG_CONNECT),hwndApp,
ConnectDlgProc)) {
if (dwAppState & AS_FILELOADED) {
// Free dirty file and free users
RemoveAllUsers(hwndList);
}
if (!LoadFromRegistry(hwndApp,hwndList,TRUE)) {
OnDisconnect(hwndApp);
return FALSE;
}
lstrcpy(szDatFilename,szNull);
dwAppState |= AS_FILELOADED | AS_FILEHASNAME | AS_REMOTEREGISTRY;
dwAppState &= (~AS_CANOPENTEMPLATE & ~AS_LOCALREGISTRY & ~AS_POLICYFILE);
EnableMenuItems(hwndApp,dwAppState);
SetTitleBar(hwndApp,szRemoteName);
EnableWindow(hwndList,TRUE);
}
return TRUE;
}
BOOL CALLBACK ConnectDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg) {
case WM_INITDIALOG:
if (!InitConnectDlg(hDlg)) {
EndDialog(hDlg,0);
return FALSE;
}
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK:
if (ProcessConnectDlg(hDlg))
EndDialog(hDlg,TRUE);
return TRUE;
break;
case IDCANCEL:
EndDialog(hDlg,FALSE);
return TRUE;
break;
}
break;
}
return FALSE;
}
BOOL ProcessConnectDlg(HWND hDlg)
{
TCHAR szComputerName[COMPUTERNAMELEN+1];
hkeyRemoteHLM=NULL;
// get computer name from dialog
if (!GetDlgItemText(hDlg,IDD_COMPUTERNAME,szComputerName,
ARRAYSIZE(szComputerName))) {
SetFocus(GetDlgItem(hDlg,IDD_COMPUTERNAME));
MsgBox(hDlg,IDS_NEEDCOMPUTERNAME,MB_OK,MB_ICONINFORMATION);
return FALSE;
}
// make the connection
if (RemoteConnect(hDlg,szComputerName,TRUE)) {
HKEY hkeyState;
// save this name to fill in UI as default in future connect dlgs
if (RegCreateKey(HKEY_CURRENT_USER,szAPPREGKEY,&hkeyState) ==
ERROR_SUCCESS) {
RegSetValueEx(hkeyState,szLASTCONNECTION,0,REG_SZ,szComputerName,(lstrlen(szComputerName)+1) * sizeof(TCHAR));
RegCloseKey(hkeyState);
}
return TRUE;
} else return FALSE;
}
void GetRealUserName (HKEY hRemoteHLM, LPTSTR lpRemoteMachine,
LPTSTR lpInput, LPTSTR lpOutput)
{
TCHAR szName [MAX_PATH];
TCHAR szDomainName [MAX_PATH];
LONG lResult;
HKEY hKey;
DWORD dwSize, dwType, dwDomainSize;
PSID pSid;
SID_NAME_USE SidNameUse;
//
// Initialize the output with the default name
//
lstrcpy (lpOutput, lpInput);
//
// If the remote machine is NT, then this registry call will
// succeed and we can get the real sid to look up with.
// If the remote machine is Windows, then this call will fail
// and we'll go with the default input name (which is ok because
// Windows uses the user name in HKEY_USERS rather than a SID.
//
wsprintf (szName, TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\%s"),
lpInput);
lResult = RegOpenKeyEx (hRemoteHLM,
szName,
0,
KEY_READ,
&hKey);
if (lResult != ERROR_SUCCESS) {
return;
}
//
// Query the size of the SID (binary)
//
lResult = RegQueryValueEx (hKey,
TEXT("Sid"),
NULL,
&dwType,
NULL,
&dwSize);
if (lResult != ERROR_SUCCESS) {
RegCloseKey (hKey);
return;
}
//
// Allocate space for the SID
//
pSid = GlobalAlloc (GPTR, dwSize);
if (!pSid) {
return;
}
lResult = RegQueryValueEx (hKey,
TEXT("Sid"),
NULL,
&dwType,
pSid,
&dwSize);
if (lResult != ERROR_SUCCESS) {
GlobalFree (pSid);
RegCloseKey (hKey);
return;
}
//
// Lookup the account name
//
dwSize = MAX_PATH;
dwDomainSize = MAX_PATH;
if (LookupAccountSid (lpRemoteMachine,
pSid,
szName,
&dwSize,
szDomainName,
&dwDomainSize,
&SidNameUse) ) {
lstrcpy (lpOutput, szDomainName);
lstrcat (lpOutput, TEXT("\\"));
lstrcat (lpOutput, szName);
} else {
LoadSz(IDS_ACCOUNTUNKNOWN, lpOutput, MAX_KEY_NAME);
}
//
// Clean up
//
GlobalFree (pSid);
RegCloseKey (hKey);
}
LRESULT CALLBACK ChooseUserDlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) {
case WM_INITDIALOG:
{
LPUSERINFO lpInfo = (LPUSERINFO) lParam;
LPKEYNODE lpItem;
TCHAR szMsg[60+COMPUTERNAMELEN],szFmt[60];
TCHAR szUserName[MAX_KEY_NAME];
INT iResult;
//
// Store the LPUSERINFO pointer in the extra words
//
SetWindowLong (hDlg, GWL_USERDATA, (LONG) lpInfo);
//
// Fill in the title
//
LoadSz(IDS_CHOOSEUSER_TITLE, szFmt, ARRAYSIZE(szFmt));
wsprintf(szMsg,szFmt,lpInfo->lpComputerName);
SendDlgItemMessage (hDlg, IDD_USER, WM_SETTEXT, 0, (LPARAM) szMsg);
lpItem = lpInfo->lpList;
while (lpItem) {
//
// If the user name is not .Default, add it to
// the list.
//
if (lstrcmpi(lpItem->lpUserName, TEXT(".Default")) != 0) {
//
// Get the user name to display
//
GetRealUserName (lpInfo->hkeyRemoteHLM, lpInfo->lpComputerName,
lpItem->lpUserName, szUserName);
iResult = SendDlgItemMessage (hDlg, IDD_LIST, LB_INSERTSTRING,
(WPARAM) -1, (LPARAM) szUserName);
if (iResult != LB_ERR) {
SendDlgItemMessage (hDlg, IDD_LIST, LB_SETITEMDATA,
iResult, (LPARAM) lpItem);
}
}
lpItem = lpItem->pNext;
}
//
// Select the first item, or disable the Ok button if no
// one is logged on.
//
iResult = SendDlgItemMessage (hDlg, IDD_LIST, LB_GETCOUNT, 0, 0);
if (iResult == 0) {
EnableWindow (GetDlgItem (hDlg, IDOK), FALSE);
} else {
SendDlgItemMessage (hDlg, IDD_LIST, LB_SETCURSEL, 0, 0);
}
}
break;
case WM_COMMAND:
if ((LOWORD(wParam) == IDOK) ||
((LOWORD(wParam) == IDD_LIST) && (HIWORD(wParam) == LBN_DBLCLK))){
LPUSERINFO lpInfo = (LPUSERINFO) GetWindowLong(hDlg, GWL_USERDATA);
INT iResult;
if (!lpInfo) {
return TRUE;
}
//
// Find the selected item
//
iResult = SendDlgItemMessage (hDlg, IDD_LIST, LB_GETCURSEL, 0, 0);
if (iResult != LB_ERR) {
//
// Save the item pointer
//
iResult = SendDlgItemMessage (hDlg, IDD_LIST, LB_GETITEMDATA,
(WPARAM) iResult, 0);
if (iResult != LB_ERR) {
lpInfo->lpSelectedItem = (LPKEYNODE) iResult;
} else {
lpInfo->lpSelectedItem = NULL;
}
EndDialog(hDlg, TRUE);
}
return TRUE;
}
if (LOWORD(wParam) == IDCANCEL) {
EndDialog(hDlg, FALSE);
return TRUE;
}
break;
}
return FALSE;
}
BOOL RemoteConnectHCU (HWND hwndOwner, TCHAR * pszComputerName,
HKEY hkeyRemoteHLM, HKEY * hkeyRemoteHCU,
BOOL fDisplayError)
{
USERINFO UserInfo;
HKEY hkeyRemoteHU;
LONG lResult;
DWORD dwSubKeys;
TCHAR szUserName[MAX_KEY_NAME];
LPKEYNODE lpList = NULL, lpItem;
DWORD dwSize;
UINT Index = 0;
FILETIME ftWrite;
BOOL bRetVal = FALSE;
//
// Connect to HKEY_USERS on the remote machine to see
// how many people are logged on.
//
lResult = RegConnectRegistry(pszComputerName,HKEY_USERS,
&hkeyRemoteHU);
if (lResult != ERROR_SUCCESS) {
return FALSE;
}
//
// Enumerate the subkeys
//
dwSize = MAX_KEY_NAME;
lResult = RegEnumKeyEx(hkeyRemoteHU, Index, szUserName, &dwSize, NULL,
NULL, NULL, &ftWrite);
if (lResult == ERROR_SUCCESS) {
do {
//
// Add the node
//
if (!AddKeyNode (&lpList, szUserName)) {
break;
}
Index++;
dwSize = MAX_KEY_NAME;
lResult = RegEnumKeyEx(hkeyRemoteHU, Index, szUserName, &dwSize, NULL,
NULL, NULL, &ftWrite);
} while (lResult == ERROR_SUCCESS);
} else {
RegCloseKey(hkeyRemoteHU);
return FALSE;
}
UserInfo.lpComputerName = pszComputerName;
UserInfo.lpList = lpList;
UserInfo.hkeyRemoteHLM = hkeyRemoteHLM;
if (DialogBoxParam (ghInst, MAKEINTRESOURCE(DLG_CHOOSEUSER), hwndOwner,
ChooseUserDlgProc, (LPARAM)&UserInfo)) {
if (UserInfo.lpSelectedItem) {
lResult = RegOpenKeyEx (hkeyRemoteHU,
UserInfo.lpSelectedItem->lpUserName,
0,
KEY_ALL_ACCESS,
hkeyRemoteHCU);
if (lResult == ERROR_SUCCESS) {
bRetVal = TRUE;
} else {
SetFocus(GetDlgItem(hwndOwner,IDD_COMPUTERNAME));
SendDlgItemMessage(hwndOwner,IDD_COMPUTERNAME,EM_SETSEL,0,-1);
if (fDisplayError) {
TCHAR szMsg[MEDIUMBUF+COMPUTERNAMELEN+COMPUTERNAMELEN];
TCHAR szFmt[MEDIUMBUF];
LoadSz(IDS_CANTCONNECT,szFmt,ARRAYSIZE(szFmt));
wsprintf(szMsg,szFmt,pszComputerName,pszComputerName);
MsgBoxSz(hwndOwner,szMsg,MB_OK,MB_ICONINFORMATION);
}
}
}
}
//
// Free the link list
//
FreeKeyList (lpList);
//
// Close HKEY_USERS on the remote machine
//
RegCloseKey (hkeyRemoteHU);
return bRetVal;
}
BOOL RemoteConnect(HWND hwndOwner,TCHAR * pszComputerName,BOOL fDisplayError)
{
UINT uRet;
HCURSOR hOldCursor;
#ifdef DEBUG
wsprintf(szDebugOut,TEXT("ADMINCFG: connecting to %s\r\n"),pszComputerName);
OutputDebugString(szDebugOut);
#endif
hkeyRemoteHLM = hkeyRemoteHCU = NULL;
hOldCursor=SetCursor(LoadCursor(NULL,IDC_WAIT));
//
// try to connect to remote registry HKEY_LOCAL_MACHINE
//
uRet = RegConnectRegistry(pszComputerName,HKEY_LOCAL_MACHINE,
&hkeyRemoteHLM);
SetCursor(hOldCursor);
if (uRet != ERROR_SUCCESS) {
SetFocus(GetDlgItem(hwndOwner,IDD_COMPUTERNAME));
SendDlgItemMessage(hwndOwner,IDD_COMPUTERNAME,EM_SETSEL,0,-1);
if (fDisplayError) {
TCHAR szMsg[MEDIUMBUF+COMPUTERNAMELEN+COMPUTERNAMELEN];
TCHAR szFmt[MEDIUMBUF];
LoadSz(IDS_CANTCONNECT,szFmt,ARRAYSIZE(szFmt));
wsprintf(szMsg,szFmt,pszComputerName,pszComputerName);
MsgBoxSz(hwndOwner,szMsg,MB_OK,MB_ICONINFORMATION);
}
return FALSE;
}
//
// try to connect to remote registry HKEY_CURRENT_USER
//
uRet = RemoteConnectHCU (hwndOwner, pszComputerName, hkeyRemoteHLM,
&hkeyRemoteHCU, fDisplayError);
if (!uRet) {
RegCloseKey(hkeyRemoteHLM);
hkeyRemoteHLM = NULL;
return FALSE;
}
hkeyVirtHLM = hkeyRemoteHLM;
hkeyVirtHCU = hkeyRemoteHCU;
// change "connect..." menu item to "disconnect"
ReplaceMenuItem(hwndMain,IDM_CONNECT,IDM_DISCONNECT,IDS_DISCONNECT);
lstrcpy(szRemoteName,pszComputerName);
return TRUE;
}
BOOL OnDisconnect(HWND hwndOwner)
{
#ifdef DEBUG
OutputDebugString(TEXT("ADMINCFG: Disconnecting.\r\n"));
#endif
if (hkeyRemoteHLM) {
RegCloseKey(hkeyRemoteHLM);
hkeyRemoteHLM = NULL;
}
if (hkeyRemoteHCU) {
RegCloseKey(hkeyRemoteHCU);
hkeyRemoteHCU = NULL;
}
// point virtual HLM, HCU keys at local machine
hkeyVirtHLM = HKEY_LOCAL_MACHINE;
hkeyVirtHCU = HKEY_CURRENT_USER;
// change "disconnect" menu item to "connect..."
ReplaceMenuItem(hwndMain,IDM_DISCONNECT,IDM_CONNECT,IDS_CONNECT);
SetTitleBar(hwndMain,NULL);
return TRUE;
}
BOOL InitConnectDlg(HWND hDlg)
{
HKEY hkeyState;
TCHAR szComputerName[COMPUTERNAMELEN+1];
DWORD dwSize = ARRAYSIZE(szComputerName);
SetFocus(GetDlgItem(hDlg,IDD_COMPUTERNAME));
SendDlgItemMessage(hDlg,IDD_COMPUTERNAME,EM_SETLIMITTEXT,
COMPUTERNAMELEN,0L);
if (RegOpenKey(HKEY_CURRENT_USER,szAPPREGKEY,&hkeyState) ==
ERROR_SUCCESS) {
if (RegQueryValueEx(hkeyState,szLASTCONNECTION,NULL,NULL,szComputerName,&dwSize)
==ERROR_SUCCESS) {
SetDlgItemText(hDlg,IDD_COMPUTERNAME,szComputerName);
SendDlgItemMessage(hDlg,IDD_COMPUTERNAME,EM_SETSEL,0,-1);
}
RegCloseKey(hkeyState);
}
return TRUE;
}
//*************************************************************
//
// AddKEYNODE()
//
// Purpose: Adds a key node to the link listed
//
// Parameters: lpList - Link list of nodes
// lpUserName - User Name
//
//
// Return: TRUE if successful
// FALSE if an error occurs
//
// Comments:
//
// History: Date Author Comment
// 1/16/96 ericflo Created
//
//*************************************************************
BOOL AddKeyNode (LPKEYNODE *lpList, LPTSTR lpUserName)
{
LPKEYNODE lpNewItem;
if (!lpUserName || !*lpUserName) {
return TRUE;
}
//
// Setup the new node
//
lpNewItem = (LPKEYNODE) LocalAlloc(LPTR, sizeof(KEYNODE) +
((lstrlen(lpUserName) + 1) * sizeof(TCHAR)));
if (!lpNewItem) {
return FALSE;
}
lpNewItem->lpUserName = (LPTSTR)((LPBYTE)lpNewItem + sizeof(KEYNODE));
lstrcpy (lpNewItem->lpUserName, lpUserName);
lpNewItem->pNext = *lpList;
*lpList = lpNewItem;
return TRUE;
}
//*************************************************************
//
// FreeKeyList()
//
// Purpose: Free's a KEYNODE link list
//
// Parameters: lpList - List to be freed
//
// Return: TRUE if successful
// FALSE if an error occurs
//
// Comments:
//
// History: Date Author Comment
// 1/16/96 ericflo Created
//
//*************************************************************
BOOL FreeKeyList (LPKEYNODE lpList)
{
LPKEYNODE lpNext;
if (!lpList) {
return TRUE;
}
lpNext = lpList->pNext;
while (lpList) {
LocalFree (lpList);
lpList = lpNext;
if (lpList) {
lpNext = lpList->pNext;
}
}
return TRUE;
}
|