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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
|
/*++
Copyright (c) 1990 Microsoft Corporation
Module Name:
server.c
Abstract:
This file contains services related to the SAM "server" object.
Author:
Jim Kelly (JimK) 4-July-1991
Environment:
User Mode - Win32
Revision History:
--*/
///////////////////////////////////////////////////////////////////////////////
// //
// Includes //
// //
///////////////////////////////////////////////////////////////////////////////
#include <samsrvp.h>
///////////////////////////////////////////////////////////////////////////////
// //
// private service prototypes //
// //
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// //
// Routines //
// //
///////////////////////////////////////////////////////////////////////////////
NTSTATUS
SamrConnect2(
IN PSAMPR_SERVER_NAME ServerName,
OUT SAMPR_HANDLE * ServerHandle,
IN ACCESS_MASK DesiredAccess
)
/*++
Routine Description:
This service is the dispatch routine for SamConnect. It performs
an access validation to determine whether the caller may connect
to SAM for the access specified. If so, a context block is established.
This is different from the SamConnect call in that the entire server
name is passed instead of just the first character.
Arguments:
ServerName - Name of the node this SAM reside on. Ignored by this
routine.
ServerHandle - If the connection is successful, the value returned
via this parameter serves as a context handle to the openned
SERVER object.
DesiredAccess - Specifies the accesses desired to the SERVER object.
Return Value:
Status values returned by SamIConnect().
--*/
{
BOOLEAN TrustedClient;
//
// If we ever want to support trusted remote clients, then the test
// for whether or not the client is trusted can be made here and
// TrustedClient set appropriately. For now, all remote clients are
// considered untrusted.
TrustedClient = FALSE;
return SamIConnect(ServerName, ServerHandle, DesiredAccess, TrustedClient );
}
NTSTATUS
SamrConnect(
IN PSAMPR_SERVER_NAME ServerName,
OUT SAMPR_HANDLE * ServerHandle,
IN ACCESS_MASK DesiredAccess
)
/*++
Routine Description:
This service is the dispatch routine for SamConnect. It performs
an access validation to determine whether the caller may connect
to SAM for the access specified. If so, a context block is established
Arguments:
ServerName - Name of the node this SAM reside on. Ignored by this
routine. The name contains only a single character.
ServerHandle - If the connection is successful, the value returned
via this parameter serves as a context handle to the openned
SERVER object.
DesiredAccess - Specifies the accesses desired to the SERVER object.
Return Value:
Status values returned by SamIConnect().
--*/
{
BOOLEAN TrustedClient;
//
// If we ever want to support trusted remote clients, then the test
// for whether or not the client is trusted can be made here and
// TrustedClient set appropriately. For now, all remote clients are
// considered untrusted.
TrustedClient = FALSE;
return SamIConnect(NULL, ServerHandle, DesiredAccess, TrustedClient );
}
NTSTATUS
SamIConnect(
IN PSAMPR_SERVER_NAME ServerName,
OUT SAMPR_HANDLE * ServerHandle,
IN ACCESS_MASK DesiredAccess,
IN BOOLEAN TrustedClient
)
/*++
Routine Description:
This service is the dispatch routine for SamConnect. It performs
an access validation to determine whether the caller may connect
to SAM for the access specified. If so, a context block is established
NOTE: If the caller is trusted, then the DesiredAccess parameter may
NOT contain any Generic access types or MaximumAllowed. All
mapping must be done by the caller.
Arguments:
ServerName - Name of the node this SAM reside on. Ignored by this
routine.
ServerHandle - If the connection is successful, the value returned
via this parameter serves as a context handle to the openned
SERVER object.
DesiredAccess - Specifies the accesses desired to the SERVER object.
TrustedClient - Indicates whether the client is known to be part of
the trusted computer base (TCB). If so (TRUE), no access validation
is performed and all requested accesses are granted. If not
(FALSE), then the client is impersonated and access validation
performed against the SecurityDescriptor on the SERVER object.
Return Value:
STATUS_SUCCESS - The SERVER object has been successfully openned.
STATUS_INSUFFICIENT_RESOURCES - The SAM server processes doesn't
have sufficient resources to process or accept another connection
at this time.
Other values as may be returned from:
NtAccessCheckAndAuditAlarm()
--*/
{
NTSTATUS NtStatus;
PSAMP_OBJECT Context;
UNREFERENCED_PARAMETER( ServerName ); //Ignored by this routine
//
// If the SAM server is not initialized, reject the connection.
//
if (SampServiceState != SampServiceEnabled) {
return(STATUS_INVALID_SERVER_STATE);
}
SampAcquireReadLock();
Context = SampCreateContext( SampServerObjectType, TrustedClient );
if (Context != NULL) {
//
// The RootKey for a SERVER object is the root of the SAM database.
// This key should not be closed when the context is deleted.
//
Context->RootKey = SampKey;
//
// The rootkeyname has been initialized to NULL inside CreateContext.
//
//
// Perform access validation ...
//
NtStatus = SampValidateObjectAccess(
Context, //Context
DesiredAccess, //DesiredAccess
FALSE //ObjectCreation
);
//
// if we didn't pass the access test, then free up the context block
// and return the error status returned from the access validation
// routine. Otherwise, return the context handle value.
//
if (!NT_SUCCESS(NtStatus)) {
SampDeleteContext( Context );
} else {
(*ServerHandle) = Context;
}
} else {
NtStatus = STATUS_INSUFFICIENT_RESOURCES;
}
//
// Free the read lock
//
SampReleaseReadLock();
return(NtStatus);
}
NTSTATUS
SamrShutdownSamServer(
IN SAMPR_HANDLE ServerHandle
)
/*++
Routine Description:
This service shuts down the SAM server.
In the long run, this routine will perform an orderly shutdown.
In the short term, it is useful for debug purposes to shutdown
in a brute force un-orderly fashion.
Arguments:
ServerHandle - Received from a previous call to SamIConnect().
Return Value:
STATUS_SUCCESS - The services completed successfully.
STATUS_ACCESS_DENIED - The caller doesn't have the appropriate access
to perform the requested operation.
--*/
{
NTSTATUS NtStatus, IgnoreStatus;
PSAMP_OBJECT ServerContext;
SAMP_OBJECT_TYPE FoundType;
NtStatus = SampAcquireWriteLock();
if (!NT_SUCCESS(NtStatus)) {
return(NtStatus);
}
//
// Validate type of, and access to server object.
//
ServerContext = (PSAMP_OBJECT)ServerHandle;
NtStatus = SampLookupContext(
ServerContext,
SAM_SERVER_SHUTDOWN, // DesiredAccess
SampServerObjectType, // ExpectedType
&FoundType
);
if (NT_SUCCESS(NtStatus)) {
//
// Signal the event that will cut loose the main thread.
// The main thread will then exit - causing the walls to
// come tumbling down.
//
IgnoreStatus = RpcMgmtStopServerListening(0);
ASSERT(NT_SUCCESS(IgnoreStatus));
//
// De-reference the server object
//
IgnoreStatus = SampDeReferenceContext( ServerContext, FALSE );
ASSERT(NT_SUCCESS(IgnoreStatus));
}
//
// Free the write lock and roll-back the transaction
//
IgnoreStatus = SampReleaseWriteLock( FALSE );
ASSERT(NT_SUCCESS(IgnoreStatus));
return(NtStatus);
}
NTSTATUS
SamrLookupDomainInSamServer(
IN SAMPR_HANDLE ServerHandle,
IN PRPC_UNICODE_STRING Name,
OUT PRPC_SID *DomainId
)
/*++
Routine Description:
This service
Arguments:
ServerHandle - A context handle returned by a previous call
to SamConnect().
Name - contains the name of the domain to look up.
DomainSid - Receives a pointer to a buffer containing the SID of
the domain. The buffer pointed to must be deallocated by the
caller using MIDL_user_free() when no longer needed.
Return Value:
STATUS_SUCCESS - The services completed successfully.
STATUS_ACCESS_DENIED - The caller doesn't have the appropriate access
to perform the requested operation.
STATUS_NO_SUCH_DOMAIN - The specified domain does not exist at this
server.
STATUS_INVALID_SERVER_STATE - Indicates the SAM server is currently
disabled.
--*/
{
NTSTATUS NtStatus, IgnoreStatus;
PSAMP_OBJECT ServerContext;
SAMP_OBJECT_TYPE FoundType;
ULONG i, SidLength;
BOOLEAN DomainFound;
PSID FoundSid;
//
// Make sure we understand what RPC is doing for (to) us.
//
ASSERT (DomainId != NULL);
ASSERT ((*DomainId) == NULL);
ASSERT( Name != NULL );
if (Name->Buffer == NULL) {
return(STATUS_INVALID_PARAMETER);
}
SampAcquireReadLock();
//
// Validate type of, and access to object.
//
ServerContext = (PSAMP_OBJECT)ServerHandle;
NtStatus = SampLookupContext(
ServerContext,
SAM_SERVER_LOOKUP_DOMAIN,
SampServerObjectType, // ExpectedType
&FoundType
);
if (NT_SUCCESS(NtStatus)) {
//
// Set our default completion status
//
NtStatus = STATUS_NO_SUCH_DOMAIN;
//
// Search the list of defined domains for a match.
//
DomainFound = FALSE;
for (i = 0;
(i<SampDefinedDomainsCount && (!DomainFound));
i++ ) {
if (RtlEqualDomainName(&SampDefinedDomains[i].ExternalName, (PUNICODE_STRING)Name) ) {
DomainFound = TRUE;
//
// Allocate and fill in the return buffer
//
SidLength = RtlLengthSid( SampDefinedDomains[i].Sid );
FoundSid = MIDL_user_allocate( SidLength );
if (FoundSid != NULL) {
NtStatus =
RtlCopySid( SidLength, FoundSid, SampDefinedDomains[i].Sid );
if (!NT_SUCCESS(NtStatus) ) {
MIDL_user_free( FoundSid );
NtStatus = STATUS_INTERNAL_ERROR;
}
(*DomainId) = FoundSid;
}
NtStatus = STATUS_SUCCESS;
}
}
//
// De-reference the object
//
if ( NT_SUCCESS( NtStatus ) ) {
NtStatus = SampDeReferenceContext( ServerContext, FALSE );
} else {
IgnoreStatus = SampDeReferenceContext( ServerContext, FALSE );
}
}
//
// Free the read lock
//
SampReleaseReadLock();
return(NtStatus);
}
NTSTATUS
SamrEnumerateDomainsInSamServer(
IN SAMPR_HANDLE ServerHandle,
IN OUT PSAM_ENUMERATE_HANDLE EnumerationContext,
OUT PSAMPR_ENUMERATION_BUFFER *Buffer,
IN ULONG PreferedMaximumLength,
OUT PULONG CountReturned
)
/*++
Routine Description:
This API lists all the domains defined in the account database.
Since there may be more domains than can fit into a buffer, the
caller is provided with a handle that can be used across calls to
the API. On the initial call, EnumerationContext should point to a
SAM_ENUMERATE_HANDLE variable that is set to 0.
If the API returns STATUS_MORE_ENTRIES, then the API should be
called again with EnumerationContext. When the API returns
STATUS_SUCCESS or any error return, the handle becomes invalid for
future use.
This API requires SAM_SERVER_ENUMERATE_DOMAINS access to the
SamServer object.
Arguments:
ConnectHandle - Handle obtained from a previous SamConnect call.
EnumerationContext - API specific handle to allow multiple calls
(see below). This is a zero based index.
Buffer - Receives a pointer to the buffer where the information
is placed. The information returned is contiguous
SAM_RID_ENUMERATION data structures. However, the
RelativeId field of each of these structures is not valid.
This buffer must be freed when no longer needed using
SamFreeMemory().
PreferedMaximumLength - Prefered maximum length of returned data
(in 8-bit bytes). This is not a hard upper limit, but serves
as a guide to the server. Due to data conversion between
systems with different natural data sizes, the actual amount
of data returned may be greater than this value.
CountReturned - Number of entries returned.
Return Value:
STATUS_SUCCESS - The Service completed successfully, and there
are no addition entries.
STATUS_MORE_ENTRIES - There are more entries, so call again.
This is a successful return.
STATUS_ACCESS_DENIED - Caller does not have the access required
to enumerate the domains.
STATUS_INVALID_HANDLE - The handle passed is invalid.
STATUS_INVALID_SERVER_STATE - Indicates the SAM server is
currently disabled.
--*/
{
NTSTATUS NtStatus, IgnoreStatus;
ULONG i;
PSAMP_OBJECT Context;
SAMP_OBJECT_TYPE FoundType;
ULONG TotalLength = 0;
ULONG NewTotalLength;
PSAMP_ENUMERATION_ELEMENT SampHead, NextEntry, NewEntry;
BOOLEAN LengthLimitReached = FALSE;
PSAMPR_RID_ENUMERATION ArrayBuffer;
ULONG ArrayBufferLength;
//
// Make sure we understand what RPC is doing for (to) us.
//
ASSERT (ServerHandle != NULL);
ASSERT (EnumerationContext != NULL);
ASSERT ( Buffer != NULL);
ASSERT ((*Buffer) == NULL);
ASSERT (CountReturned != NULL);
//
// Initialize the list of names being returned.
// This is a singly linked list.
//
SampHead = NULL;
//
// Initialize the count returned
//
(*CountReturned) = 0;
SampAcquireReadLock();
//
// Validate type of, and access to object.
//
Context = (PSAMP_OBJECT)ServerHandle;
NtStatus = SampLookupContext(
Context,
SAM_SERVER_ENUMERATE_DOMAINS,
SampServerObjectType, // ExpectedType
&FoundType
);
if (NT_SUCCESS(NtStatus)) {
//
// Enumerating domains is easy. We keep a list in memory.
// All we have to do is use the enumeration context as an
// index into the defined domains array.
//
//
// Set our default completion status
// Note that this is a SUCCESS status code.
// That is NT_SUCCESS(STATUS_MORE_ENTRIES) will return TRUE.
//
NtStatus = STATUS_MORE_ENTRIES;
//
// Search the list of defined domains for a match.
//
for ( i = (ULONG)(*EnumerationContext);
( (i < SampDefinedDomainsCount) &&
(NT_SUCCESS(NtStatus)) &&
(!LengthLimitReached) );
i++ ) {
//
// See if there is room for the next name. If TotalLength
// is still zero then we haven't yet even gotten one name.
// We have to return at least one name even if it exceeds
// the length request.
//
NewTotalLength = TotalLength +
sizeof(UNICODE_STRING) +
(ULONG)SampDefinedDomains[i].ExternalName.Length +
sizeof(UNICODE_NULL);
if ( (NewTotalLength < PreferedMaximumLength) ||
(TotalLength == 0) ) {
if (NewTotalLength > SAMP_MAXIMUM_MEMORY_TO_USE) {
NtStatus = STATUS_INSUFFICIENT_RESOURCES;
} else {
TotalLength = NewTotalLength;
(*CountReturned) += 1;
//
// Room for this name as well.
// Allocate a new return list entry, and a buffer for the
// name.
//
NewEntry = MIDL_user_allocate(sizeof(SAMP_ENUMERATION_ELEMENT));
if (NewEntry == NULL) {
NtStatus = STATUS_INSUFFICIENT_RESOURCES;
} else {
NewEntry->Entry.Name.Buffer =
MIDL_user_allocate(
(ULONG)SampDefinedDomains[i].ExternalName.Length +
sizeof(UNICODE_NULL)
);
if (NewEntry->Entry.Name.Buffer == NULL) {
MIDL_user_free(NewEntry);
NtStatus = STATUS_INSUFFICIENT_RESOURCES;
} else {
//
// Copy the name into the return buffer
//
RtlCopyMemory( NewEntry->Entry.Name.Buffer,
SampDefinedDomains[i].ExternalName.Buffer,
SampDefinedDomains[i].ExternalName.Length
);
NewEntry->Entry.Name.Length = SampDefinedDomains[i].ExternalName.Length;
NewEntry->Entry.Name.MaximumLength = NewEntry->Entry.Name.Length + (USHORT)sizeof(UNICODE_NULL);
UnicodeTerminate((PUNICODE_STRING)(&NewEntry->Entry.Name));
//
// The Rid field of the ENUMERATION_INFORMATION is not
// filled in for domains.
// Just for good measure, set it to zero.
//
NewEntry->Entry.RelativeId = 0;
//
// Now add this to the list of names to be returned.
//
NewEntry->Next = (PSAMP_ENUMERATION_ELEMENT)SampHead;
SampHead = NewEntry;
}
}
}
} else {
LengthLimitReached = TRUE;
}
}
if ( NT_SUCCESS(NtStatus) ) {
//
// Set the enumeration context
//
(*EnumerationContext) = (*EnumerationContext) + (*CountReturned);
//
// If we are returning the last of the names, then change our
// status code to indicate this condition.
//
if ( ((*EnumerationContext) >= SampDefinedDomainsCount) ) {
NtStatus = STATUS_SUCCESS;
}
//
// Build a return buffer containing an array of the
// SAM_RID_ENUMERATIONs pointed to by another
// buffer containing the number of elements in that
// array.
//
(*Buffer) = MIDL_user_allocate( sizeof(SAMPR_ENUMERATION_BUFFER) );
if ( (*Buffer) == NULL) {
NtStatus = STATUS_INSUFFICIENT_RESOURCES;
} else {
(*Buffer)->EntriesRead = (*CountReturned);
ArrayBufferLength = sizeof( SAM_RID_ENUMERATION ) *
(*CountReturned);
ArrayBuffer = MIDL_user_allocate( ArrayBufferLength );
(*Buffer)->Buffer = ArrayBuffer;
if ( ArrayBuffer == NULL) {
NtStatus = STATUS_INSUFFICIENT_RESOURCES;
MIDL_user_free( (*Buffer) );
} else {
//
// Walk the list of return entries, copying
// them into the return buffer
//
NextEntry = SampHead;
i = 0;
while (NextEntry != NULL) {
NewEntry = NextEntry;
NextEntry = NewEntry->Next;
ArrayBuffer[i] = NewEntry->Entry;
i += 1;
MIDL_user_free( NewEntry );
}
}
}
}
if ( !NT_SUCCESS(NtStatus) ) {
//
// Free the memory we've allocated
//
NextEntry = SampHead;
while (NextEntry != NULL) {
NewEntry = NextEntry;
NextEntry = NewEntry->Next;
MIDL_user_free( NewEntry->Entry.Name.Buffer );
MIDL_user_free( NewEntry );
}
(*EnumerationContext) = 0;
(*CountReturned) = 0;
(*Buffer) = NULL;
}
//
// De-reference the object
// Note that NtStatus could be STATUS_MORE_ENTRIES, which is a
// successful return code - we want to make sure we return that,
// without wiping it out here.
//
if ( NtStatus == STATUS_SUCCESS ) {
NtStatus = SampDeReferenceContext( Context, FALSE );
} else {
IgnoreStatus = SampDeReferenceContext( Context, FALSE );
}
}
//
// Free the read lock
//
SampReleaseReadLock();
return(NtStatus);
}
|