summaryrefslogtreecommitdiffstats
path: root/private/nw/ndsutils/treebn.c
blob: 77a735e3797eca0fd27b792815af61427bd61d95 (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
/***

Copyright (c) 1996  Microsoft Corporation

Module Name:

    Treebn.c

Abstract:

    Command line test for getting the connection information
    for a particular NT user name.

Author:

    Cory West       [corywest] 1-March-1996

***/

#include "ndsapi32.h"
#include "ntddnwfs.h"

int
_cdecl main(
    int argc,
    char **argv
) {

    NTSTATUS ntstatus;
    IO_STATUS_BLOCK IoStatusBlock;
    OBJECT_ATTRIBUTES ObjectAttributes;
    ACCESS_MASK DesiredAccess = SYNCHRONIZE | FILE_LIST_DIRECTORY;
    HANDLE hRdr;

    WCHAR OpenString[] = L"\\Device\\Nwrdr\\*";
    UNICODE_STRING OpenName;

    OEM_STRING OemArg;
    UNICODE_STRING NtUserName;
    WCHAR NtUserBuffer[512];

    ULONG BufferSize = 4096;
    BYTE *Reply;

    PNWR_NDS_REQUEST_PACKET Request;
    BYTE RequestBuffer[2048];
    ULONG RequestSize;

    PCONN_INFORMATION pConnInfo;
    UNICODE_STRING Name;
    DWORD dwTrees, dwSize;

    //
    // Check the arguments.
    //

    if ( argc != 2 ) {
        printf( "Usage: treebn [nt user name]\n" );
        return -1;
    }

    //
    // Allocate buffer space.
    //

    Reply = LocalAlloc( LMEM_ZEROINIT, BufferSize );

    if ( !Reply ) {
       printf( "Insufficient memory to complete the request.\n" );
       return -1;
    }

    //
    // Convert the user name to unicode.
    //

    NtUserName.Length = 0;
    NtUserName.MaximumLength = sizeof( NtUserBuffer );
    NtUserName.Buffer = NtUserBuffer;

    OemArg.Length = strlen( argv[1] );
    OemArg.MaximumLength = OemArg.Length;
    OemArg.Buffer = argv[1];

    RtlOemStringToUnicodeString( &NtUserName, &OemArg, FALSE );

    //
    // Set up the object attributes.
    //

    RtlInitUnicodeString( &OpenName, OpenString );

    InitializeObjectAttributes( &ObjectAttributes,
                                &OpenName,
                                OBJ_CASE_INSENSITIVE,
                                NULL,
                                NULL );

    ntstatus = NtOpenFile( &hRdr,
                           DesiredAccess,
                           &ObjectAttributes,
                           &IoStatusBlock,
                           FILE_SHARE_VALID_FLAGS,
                           FILE_SYNCHRONOUS_IO_NONALERT );

    if ( !NT_SUCCESS(ntstatus) )
        return ntstatus;

    //
    // Fill out the request packet for FSCTL_NWR_NDS_LIST_TREES;
    //

    Request = ( PNWR_NDS_REQUEST_PACKET ) RequestBuffer;

    Request->Parameters.ListTrees.NtUserNameLength = NtUserName.Length;

    RtlCopyMemory( &(Request->Parameters.ListTrees.NtUserName[0]),
                   NtUserBuffer,
                   NtUserName.Length );

    RequestSize = sizeof( Request->Parameters.ListTrees ) +
                  NtUserName.Length +
                  sizeof( DWORD );

    ntstatus = NtFsControlFile( hRdr,
                                NULL,
                                NULL,
                                NULL,
                                &IoStatusBlock,
                                FSCTL_NWR_NDS_LIST_TREES,
                                (PVOID) Request,
                                RequestSize,
                                (PVOID) Reply,
                                BufferSize );

    if ( !NT_SUCCESS( ntstatus ) ) {
        goto ExitWithClose;
    }

    //
    // Print out the CONN_INFO array that is in the reply buffer.
    //

    dwTrees = Request->Parameters.ListTrees.TreesReturned;
    printf( "Returned %d trees.\n", dwTrees );
    printf( "Luid was %08lx\n", Request->Parameters.ListTrees.UserLuid.LowPart );

    printf( "------------------------\n" );

    pConnInfo = (PCONN_INFORMATION) Reply;

    while ( dwTrees > 0 ) {

        dwSize = sizeof( CONN_INFORMATION );

        Name.Length = Name.MaximumLength = (USHORT) pConnInfo->HostServerLength;
        Name.Buffer = pConnInfo->HostServer;
        printf( "Tree: %wZ\n", &Name );

        dwSize += Name.Length;

        Name.Length = Name.MaximumLength = (USHORT) pConnInfo->UserNameLength;
        Name.Buffer = pConnInfo->UserName;
        printf( "User Name: %wZ\n", &Name );

        dwSize += Name.Length;

        pConnInfo = (PCONN_INFORMATION) ( ((BYTE *)pConnInfo) + dwSize );
        dwTrees--;

        printf( "------------------------\n" );
    }

    ntstatus = STATUS_SUCCESS;

ExitWithClose:

   LocalFree( Request );
   NtClose( hRdr );
   return ntstatus;

}