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
/* NFSv4.1 client for Windows
 * Copyright © 2012 The Regents of the University of Michigan
 *
 * Olga Kornievskaia <aglo@umich.edu>
 * Casey Bodley <cbodley@umich.edu>
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * without any warranty; without even the implied warranty of merchantability
 * or fitness for a particular purpose.  See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 */

#include <windows.h>
#include <npapi.h>
#include <devioctl.h>
#include <strsafe.h>

#include "nfs41_driver.h"
#include "nfs41_np.h"
#include "options.h"

#ifdef DBG
#define DbgP(_x_) NFS41DbgPrint _x_
#else
#define DbgP(_x_)
#endif
#define TRACE_TAG   L"[NFS41_NP]"
#define WNNC_DRIVER( major, minor ) ( major * 0x00010000 + minor )


ULONG _cdecl NFS41DbgPrint( __in LPTSTR Format, ... )
{
    ULONG rc = 0;
    TCHAR szbuffer[256];

    va_list marker;
    va_start( marker, Format );
    {

        //StringCchVPrintfW( szbuffer, 127, Format, marker );
        StringCchVPrintfW( szbuffer, 256, Format, marker );
        szbuffer[255] = (TCHAR)0;
        OutputDebugString( TRACE_TAG );
        OutputDebugString( szbuffer );
    }

    return rc;<--- va_list 'marker' was opened but not closed by va_end().
}

int filter(unsigned int code)
{
    DbgP((L"####Got exception %u\n", code));
    return EXCEPTION_CONTINUE_SEARCH;
}

DWORD
OpenSharedMemory(
    PHANDLE phMutex,
    PHANDLE phMemory,
    PVOID   *pMemory)
/*++

Routine Description:

    This routine opens the shared memory for exclusive manipulation

Arguments:

    phMutex - the mutex handle

    phMemory - the memory handle

    pMemory - a ptr. to the shared memory which is set if successful

Return Value:

    WN_SUCCESS -- if successful

--*/
{
    DWORD dwStatus;

    *phMutex = 0;
    *phMemory = 0;
    *pMemory = NULL;

    *phMutex = CreateMutex(NULL, FALSE, TEXT(NFS41NP_MUTEX_NAME));
    if (*phMutex == NULL)
    {
        dwStatus = GetLastError();
        DbgP((TEXT("OpenSharedMemory:  OpenMutex failed\n")));
        goto OpenSharedMemoryAbort1;
    }

    WaitForSingleObject(*phMutex, INFINITE);

    *phMemory = OpenFileMapping(FILE_MAP_WRITE,
                                FALSE,
                                TEXT(NFS41_USER_SHARED_MEMORY_NAME));
    if (*phMemory == NULL)
    {
        dwStatus = GetLastError();
        DbgP((TEXT("OpenSharedMemory:  OpenFileMapping failed\n")));
        goto OpenSharedMemoryAbort2;
    }

    *pMemory = MapViewOfFile(*phMemory, FILE_MAP_WRITE, 0, 0, 0);
    if (*pMemory == NULL)
    {
        dwStatus = GetLastError();
        DbgP((TEXT("OpenSharedMemory:  MapViewOfFile failed\n")));
        goto OpenSharedMemoryAbort3;
    }

    return ERROR_SUCCESS;

OpenSharedMemoryAbort3:
    CloseHandle(*phMemory);

OpenSharedMemoryAbort2:
    ReleaseMutex(*phMutex);
    CloseHandle(*phMutex);
    *phMutex = NULL;

OpenSharedMemoryAbort1:
    DbgP((TEXT("OpenSharedMemory: return dwStatus: %d\n"), dwStatus));

    return dwStatus;
}

VOID
CloseSharedMemory(
    PHANDLE hMutex,
    PHANDLE hMemory,
    PVOID   *pMemory)
/*++

Routine Description:

    This routine relinquishes control of the shared memory after exclusive
    manipulation

Arguments:

    hMutex - the mutex handle

    hMemory  - the memory handle

    pMemory - a ptr. to the shared memory which is set if successful

Return Value:

--*/
{
    if (*pMemory)
    {
        UnmapViewOfFile(*pMemory);
        *pMemory = NULL;
    }
    if (*hMemory)
    {
        CloseHandle(*hMemory);
        *hMemory = 0;
    }
    if (*hMutex)
    {
        if (ReleaseMutex(*hMutex) == FALSE)
        {
            DbgP((TEXT("CloseSharedMemory: ReleaseMutex error: %d\n"), GetLastError()));
        }
        CloseHandle(*hMutex);
        *hMutex = 0;
    }
}

static DWORD StoreConnectionInfo(
    IN LPCWSTR LocalName,
    IN LPCWSTR ConnectionName,
    IN USHORT ConnectionNameLength,
    IN LPNETRESOURCE lpNetResource)
{
    DWORD status;
    HANDLE hMutex, hMemory;
    PNFS41NP_SHARED_MEMORY pSharedMemory;
    PNFS41NP_NETRESOURCE pNfs41NetResource;
    INT Index;
    BOOLEAN FreeEntryFound = FALSE;

#ifdef __REACTOS__
    status = OpenSharedMemory(&hMutex, &hMemory, (PVOID *)&pSharedMemory);
#else
    status = OpenSharedMemory(&hMutex, &hMemory, &(PVOID)pSharedMemory);
#endif
    if (status)
        goto out;

    DbgP((TEXT("StoreConnectionInfo: NextIndex %d, NumResources %d\n"),
        pSharedMemory->NextAvailableIndex,
        pSharedMemory->NumberOfResourcesInUse));

    for (Index = 0; Index < pSharedMemory->NextAvailableIndex; Index++)
    {
        if (!pSharedMemory->NetResources[Index].InUse)
        {
            FreeEntryFound = TRUE;
            DbgP((TEXT("Reusing existing index %d\n"), Index));
            break;
        }
    }

    if (!FreeEntryFound)
    {
        if (pSharedMemory->NextAvailableIndex >= NFS41NP_MAX_DEVICES) {
            status = WN_NO_MORE_DEVICES;
            goto out_close;
        }
        Index = pSharedMemory->NextAvailableIndex++;
        DbgP((TEXT("Using new index %d\n"), Index));
    }

    pSharedMemory->NumberOfResourcesInUse += 1;

    pNfs41NetResource = &pSharedMemory->NetResources[Index];

    pNfs41NetResource->InUse                = TRUE;
    pNfs41NetResource->dwScope              = lpNetResource->dwScope;
    pNfs41NetResource->dwType               = lpNetResource->dwType;
    pNfs41NetResource->dwDisplayType        = lpNetResource->dwDisplayType;
    pNfs41NetResource->dwUsage              = RESOURCEUSAGE_CONNECTABLE;
    pNfs41NetResource->LocalNameLength      = (USHORT)(wcslen(LocalName) + 1) * sizeof(WCHAR);
    pNfs41NetResource->RemoteNameLength     = (USHORT)(wcslen(lpNetResource->lpRemoteName) + 1) * sizeof(WCHAR);
    pNfs41NetResource->ConnectionNameLength = ConnectionNameLength;

    StringCchCopy(pNfs41NetResource->LocalName,
        pNfs41NetResource->LocalNameLength,
        LocalName);
    StringCchCopy(pNfs41NetResource->RemoteName,
        pNfs41NetResource->RemoteNameLength,
        lpNetResource->lpRemoteName);
    StringCchCopy(pNfs41NetResource->ConnectionName,
        pNfs41NetResource->ConnectionNameLength,
        ConnectionName);

    // TODO: copy mount options -cbodley

out_close:
#ifdef __REACTOS__
    CloseSharedMemory(&hMutex, &hMemory, (PVOID *)&pSharedMemory);
#else
    CloseSharedMemory(&hMutex, &hMemory, &(PVOID)pSharedMemory);
#endif
out:
    return status;
}

ULONG
SendTo_NFS41Driver(
    IN ULONG            IoctlCode,
    IN PVOID            InputDataBuf,
    IN ULONG            InputDataLen,
    IN PVOID            OutputDataBuf,
    IN PULONG           pOutputDataLen)
{
    HANDLE  DeviceHandle;       // The mini rdr device handle
    BOOL    rc = FALSE;
    ULONG   Status;

    Status = WN_SUCCESS;
    DbgP((L"[aglo] calling CreateFile\n"));
    DeviceHandle = CreateFile(
        NFS41_USER_DEVICE_NAME,
        GENERIC_READ | GENERIC_WRITE,
        FILE_SHARE_READ | FILE_SHARE_WRITE,
        (LPSECURITY_ATTRIBUTES)NULL,
        OPEN_EXISTING,
        0,
        (HANDLE) NULL );

    DbgP((L"[aglo] after CreateFile Device Handle\n"));
    if ( INVALID_HANDLE_VALUE != DeviceHandle )
    {
        _SEH2_TRY {
        DbgP((L"[aglo] calling DeviceIoControl\n"));
        rc = DeviceIoControl(
            DeviceHandle,
            IoctlCode,
            InputDataBuf,
            InputDataLen,
            OutputDataBuf,
            *pOutputDataLen,
            pOutputDataLen,
            NULL );
        } _SEH2_EXCEPT(_SEH2_GetExceptionCode()) {
            DbgP((L"#### In except\n"));
        } _SEH2_END;
        DbgP((L"[aglo] returned from DeviceIoControl %08lx\n", rc));
            if ( !rc )
            {
                DbgP((L"[aglo] SendTo_NFS41Driver: returning error from DeviceIoctl\n"));
                Status = GetLastError( );
            }
            else
            {
                DbgP((L"[aglo] SendTo_NFS41Driver: The DeviceIoctl call succeded\n"));
            }
            CloseHandle(DeviceHandle);
    }
    else
    {
        Status = GetLastError( );
        DbgP((L"[aglo] SendTo_NFS41Driver: error %08lx opening device \n", Status));
    }
    DbgP((L"[aglo] returned from SendTo_NFS41Driver %08lx\n", Status));
    return Status;
}

DWORD APIENTRY
NPGetCaps(
    DWORD nIndex )
{
   DWORD rc = 0;

#ifndef __REACTOS__
    DbgP(( L"[aglo] GetNetCaps %d\n", nIndex ));
#endif
    switch ( nIndex )
    {
        case WNNC_SPEC_VERSION:
            rc = WNNC_SPEC_VERSION51;
            break;

        case WNNC_NET_TYPE:
            rc = WNNC_NET_RDR2SAMPLE;
            break;

        case WNNC_DRIVER_VERSION:
            rc = WNNC_DRIVER(1, 0);
            break;

        case WNNC_CONNECTION:
            rc = WNNC_CON_GETCONNECTIONS |
                 WNNC_CON_CANCELCONNECTION |
                 WNNC_CON_ADDCONNECTION |
                 WNNC_CON_ADDCONNECTION3;
            break;

        case WNNC_ENUMERATION:
            rc = WNNC_ENUM_LOCAL;
            break;

        case WNNC_START:
            rc = 1;
            break;

        case WNNC_USER:
        case WNNC_DIALOG:
        case WNNC_ADMIN:
        default:
            rc = 0;
            break;
    }

    return rc;
}

DWORD APIENTRY
NPLogonNotify(
    __in PLUID   lpLogonId,
    __in PCWSTR lpAuthentInfoType,
    __in PVOID  lpAuthentInfo,
    __in PCWSTR lpPreviousAuthentInfoType,
    __in PVOID  lpPreviousAuthentInfo,
    __in PWSTR  lpStationName,
    __in PVOID  StationHandle,
    __out PWSTR  *lpLogonScript)
{
    *lpLogonScript = NULL;
    DbgP(( L"[aglo] NPLogonNotify: returning WN_SUCCESS\n" ));
    return WN_SUCCESS;
}

DWORD APIENTRY
NPPasswordChangeNotify (
    __in LPCWSTR lpAuthentInfoType,
    __in LPVOID  lpAuthentInfo,
    __in LPCWSTR lpPreviousAuthentInfoType,
    __in LPVOID  lpPreviousAuthentInfo,
    __in LPWSTR  lpStationName,
    LPVOID  StationHandle,
    DWORD   dwChangeInfo )
{
    DbgP(( L"[aglo] NPPasswordChangeNotify: WN_NOT_SUPPORTED\n" ));
    SetLastError( WN_NOT_SUPPORTED );
    return WN_NOT_SUPPORTED;
}

#ifdef __REACTOS__
DWORD APIENTRY
NPAddConnection3(
    __in HWND           hwndOwner,
    __in LPNETRESOURCE  lpNetResource,
    __in_opt LPWSTR     lpPassword,
    __in_opt LPWSTR     lpUserName,
    __in DWORD          dwFlags);
#endif

DWORD APIENTRY
NPAddConnection(
    __in LPNETRESOURCE   lpNetResource,
    __in_opt LPWSTR      lpPassword,
    __in_opt LPWSTR      lpUserName )
{
    return NPAddConnection3( NULL, lpNetResource, lpPassword, lpUserName, 0 );
}

DWORD APIENTRY
NPAddConnection3(
    __in HWND           hwndOwner,
    __in LPNETRESOURCE  lpNetResource,
    __in_opt LPWSTR     lpPassword,
    __in_opt LPWSTR     lpUserName,
    __in DWORD          dwFlags)
{
    DWORD   Status;
    WCHAR   wszScratch[128];
    WCHAR   LocalName[3];
    DWORD   CopyBytes = 0;
    CONNECTION_INFO Connection;
    LPWSTR  ConnectionName;
    WCHAR ServerName[MAX_PATH];
    PWCHAR p;
    DWORD i;

    DbgP(( L"[aglo] NPAddConnection3('%s', '%s')\n",
        lpNetResource->lpLocalName, lpNetResource->lpRemoteName ));
    DbgP(( L"[aglo] username = '%s', passwd = '%s'\n", lpUserName, lpPassword));

    Status = InitializeConnectionInfo(&Connection,
        (PMOUNT_OPTION_BUFFER)lpNetResource->lpComment,
        &ConnectionName);
    if (Status)  {
        DbgP(( L"InitializeConnectionInfo failed with %d\n", Status ));
        goto out;
    }

    //  \device\miniredirector\;<DriveLetter>:\Server\Share

    // local name, must start with "X:"
    if (lstrlen(lpNetResource->lpLocalName) < 2 ||
        lpNetResource->lpLocalName[1] != L':') {
        Status = WN_BAD_LOCALNAME;
        goto out;
    }

    LocalName[0] = (WCHAR) toupper(lpNetResource->lpLocalName[0]);
    LocalName[1] = L':';
    LocalName[2] = L'\0';
    StringCchCopyW( ConnectionName, MAX_PATH, NFS41_DEVICE_NAME );
    StringCchCatW( ConnectionName, MAX_PATH, L"\\;" );
    StringCchCatW( ConnectionName, MAX_PATH, LocalName );

    // remote name, must start with "\\"
    if (lpNetResource->lpRemoteName[0] == L'\0' ||
        lpNetResource->lpRemoteName[0] != L'\\' ||
        lpNetResource->lpRemoteName[1] != L'\\') {
        Status = WN_BAD_NETNAME;
        goto out;
    }

    /* note: remotename comes as \\server but we need to add \server thus +1 pointer */
    p = lpNetResource->lpRemoteName + 1;
    ServerName[0] = L'\\';
    i = 1;
    for(;;) {
        /* convert servername ending unix slash to windows slash */
        if (p[i] == L'/')
            p[i] = L'\\';
        /* deal with servername ending with any slash */
        if (p[i] == L'\0')
            p[i] = L'\\';
        ServerName[i] = p[i];
        if (p[i] == L'\\') break;
        i++;
    }
    ServerName[i] = L'\0';
    StringCchCatW( ConnectionName, MAX_PATH, ServerName);
    /* insert the "nfs4" in between the server name and the path,
     * just to make sure all calls to our driver come thru this */
    StringCchCatW( ConnectionName, MAX_PATH, L"\\nfs4" );

#ifdef CONVERT_2_UNIX_SLASHES
    /* convert all windows slashes to unix slashes */
    {
        PWCHAR q = p;
        DWORD j = 0;
        for(;;) {
            if(q[j] == L'\0') break;
            if (q[j] == L'\\') q[j] = L'/';
            j++;
        }
    }
#else
    /* convert all unix slashes to windows slashes */
    {
        PWCHAR q = p;
        DWORD j = 0;
        for(;;) {
            if(q[j] == L'\0') break;
            if (q[j] == L'/') q[j] = L'\\';
            j++;
        }
    }
#endif
    StringCchCatW( ConnectionName, MAX_PATH, &p[i]);
    DbgP(( L"[aglo] Full Connect Name: %s\n", ConnectionName ));
    DbgP(( L"[aglo] Full Connect Name Length: %d %d\n",
        (wcslen(ConnectionName) + 1) * sizeof(WCHAR),
        (lstrlen(ConnectionName) + 1) * sizeof(WCHAR)));

    if ( QueryDosDevice( LocalName, wszScratch, 128 )
        || GetLastError() != ERROR_FILE_NOT_FOUND) {
        Status = WN_ALREADY_CONNECTED;
        goto out;
    }

    MarshalConnectionInfo(&Connection);

    Status = SendTo_NFS41Driver( IOCTL_NFS41_ADDCONN,
        Connection.Buffer, Connection.BufferSize,
        NULL, &CopyBytes );
    if (Status) {
        DbgP(( L"[aglo] SendTo_NFS41Driver failed with %d\n", Status));
        goto out;
    }

    DbgP(( L"[aglo] calling DefineDosDevice\n"));
    if ( !DefineDosDevice( DDD_RAW_TARGET_PATH |
                           DDD_NO_BROADCAST_SYSTEM,
                           lpNetResource->lpLocalName,
                           ConnectionName ) ) {
        Status = GetLastError();
        DbgP(( L"[aglo] DefineDosDevice failed with %d\n", Status));
        goto out_delconn;
    }

    // The connection was established and the local device mapping
    // added. Include this in the list of mapped devices.
    Status = StoreConnectionInfo(LocalName, ConnectionName,
        Connection.Buffer->NameLength, lpNetResource);
    if (Status) {
        DbgP(( L"[aglo] StoreConnectionInfo failed with %d\n", Status));
        goto out_undefine;
    }

out:
    FreeConnectionInfo(&Connection);
    DbgP(( L"[aglo] NPAddConnection3: status %08X\n", Status));
    return Status;
out_undefine:
    DefineDosDevice(DDD_REMOVE_DEFINITION | DDD_RAW_TARGET_PATH |
        DDD_EXACT_MATCH_ON_REMOVE, LocalName, ConnectionName);
out_delconn:
    SendTo_NFS41Driver(IOCTL_NFS41_DELCONN, ConnectionName,
        Connection.Buffer->NameLength, NULL, &CopyBytes);
    goto out;
}

DWORD APIENTRY
NPCancelConnection(
    __in LPWSTR  lpName,
    __in BOOL    fForce )
{
    DWORD   Status = 0;

    HANDLE  hMutex, hMemory;
    PNFS41NP_SHARED_MEMORY  pSharedMemory;

    DbgP((TEXT("NPCancelConnection\n")));
    DbgP((TEXT("NPCancelConnection: ConnectionName: %S\n"), lpName));

    Status = OpenSharedMemory( &hMutex,
                               &hMemory,
                               (PVOID)&pSharedMemory);

    if (Status == WN_SUCCESS)
    {
        INT  Index;
        PNFS41NP_NETRESOURCE pNetResource;
        Status = WN_NOT_CONNECTED;

        DbgP((TEXT("NPCancelConnection: NextIndex %d, NumResources %d\n"),
                    pSharedMemory->NextAvailableIndex,
                    pSharedMemory->NumberOfResourcesInUse));

        for (Index = 0; Index < pSharedMemory->NextAvailableIndex; Index++)
        {
            pNetResource = &pSharedMemory->NetResources[Index];

            if (pNetResource->InUse)
            {
                if ( ( (wcslen(lpName) + 1) * sizeof(WCHAR) ==
                        pNetResource->LocalNameLength)
                        && ( !wcscmp(lpName, pNetResource->LocalName) ))
                {
                    ULONG CopyBytes;

                    DbgP((TEXT("NPCancelConnection: Connection Found:\n")));

                    CopyBytes = 0;

                    Status = SendTo_NFS41Driver( IOCTL_NFS41_DELCONN,
                                pNetResource->ConnectionName,
                                pNetResource->ConnectionNameLength,
                                NULL,
                                &CopyBytes );

                    if (Status != WN_SUCCESS)
                    {
                        DbgP((TEXT("NPCancelConnection: SendToMiniRdr returned Status %lx\n"),Status));
                        break;
                    }

                    if (DefineDosDevice(DDD_REMOVE_DEFINITION | DDD_RAW_TARGET_PATH | DDD_EXACT_MATCH_ON_REMOVE,
                            lpName,
                            pNetResource->ConnectionName) == FALSE)
                    {
                        DbgP((TEXT("RemoveDosDevice:  DefineDosDevice error: %d\n"), GetLastError()));
                        Status = GetLastError();
                    }
                    else
                    {
                        pNetResource->InUse = FALSE;
                        pSharedMemory->NumberOfResourcesInUse--;

                        if (Index+1 == pSharedMemory->NextAvailableIndex)
                            pSharedMemory->NextAvailableIndex--;
                    }
                    break;
                }

                DbgP((TEXT("NPCancelConnection: Name %S EntryName %S\n"),
                            lpName,pNetResource->LocalName));
#ifndef __REACTOS__
                DbgP((TEXT("NPCancelConnection: Name Length %d Entry Name Length %d\n"),
                           pNetResource->LocalNameLength,pNetResource->LocalName));
#else
                DbgP((TEXT("NPCancelConnection: Name Length %d Entry Name Length %d\n"),
                           (wcslen(lpName) + 1) * sizeof(WCHAR), pNetResource->LocalNameLength));
#endif

            }
        }

        CloseSharedMemory( &hMutex,
                           &hMemory,
                          (PVOID)&pSharedMemory);
    }

    return Status;
}

DWORD APIENTRY
NPGetConnection(
    __in LPWSTR  lpLocalName,
    __out_bcount(*lpBufferSize) LPWSTR  lpRemoteName,
    __inout LPDWORD lpBufferSize )
{
    DWORD   Status = 0;

    HANDLE  hMutex, hMemory;
    PNFS41NP_SHARED_MEMORY  pSharedMemory;

    Status = OpenSharedMemory( &hMutex,
                               &hMemory,
                               (PVOID)&pSharedMemory);

    if (Status == WN_SUCCESS)
    {
        INT  Index;
        PNFS41NP_NETRESOURCE pNetResource;
        Status = WN_NOT_CONNECTED;

        for (Index = 0; Index < pSharedMemory->NextAvailableIndex; Index++)<--- Uninitialized variable: pSharedMemory
        {
            pNetResource = &pSharedMemory->NetResources[Index];

            if (pNetResource->InUse)
            {
                if ( ( (wcslen(lpLocalName) + 1) * sizeof(WCHAR) ==
                        pNetResource->LocalNameLength)
                        && ( !wcscmp(lpLocalName, pNetResource->LocalName) ))
                {
                    if (*lpBufferSize < pNetResource->RemoteNameLength)
                    {
                        *lpBufferSize = pNetResource->RemoteNameLength;
                        Status = WN_MORE_DATA;
                    }
                    else
                    {
                        *lpBufferSize = pNetResource->RemoteNameLength;
                        CopyMemory( lpRemoteName,
                                    pNetResource->RemoteName,
                                    pNetResource->RemoteNameLength);
                        Status = WN_SUCCESS;
                    }
                    break;
                }
            }
        }

        CloseSharedMemory( &hMutex, &hMemory, (PVOID)&pSharedMemory);
    }

    return Status;
}

DWORD APIENTRY
NPOpenEnum(
    DWORD          dwScope,
    DWORD          dwType,
    DWORD          dwUsage,
    LPNETRESOURCE  lpNetResource,
    LPHANDLE       lphEnum )
{
    DWORD   Status;

    DbgP((L"[aglo] NPOpenEnum\n"));

    *lphEnum = NULL;

    switch ( dwScope )
    {
        case RESOURCE_CONNECTED:
        {
            *lphEnum = HeapAlloc( GetProcessHeap( ), HEAP_ZERO_MEMORY, sizeof( ULONG ) );

            if (*lphEnum )
            {
                Status = WN_SUCCESS;
            }
            else
            {
                Status = WN_OUT_OF_MEMORY;
            }
            break;
        }
        break;

        case RESOURCE_CONTEXT:
        default:
            Status  = WN_NOT_SUPPORTED;
            break;
    }


    DbgP((L"[aglo] NPOpenEnum returning Status %lx\n",Status));

    return(Status);
}

DWORD APIENTRY
NPEnumResource(
    HANDLE  hEnum,
    LPDWORD lpcCount,
    LPVOID  lpBuffer,
    LPDWORD lpBufferSize)
{
    DWORD           Status = WN_SUCCESS;
    ULONG           EntriesCopied;
    LPNETRESOURCE   pNetResource;
    ULONG           SpaceNeeded = 0;
    ULONG           SpaceAvailable;
    PWCHAR          StringZone;
    HANDLE  hMutex, hMemory;
    PNFS41NP_SHARED_MEMORY  pSharedMemory;
    PNFS41NP_NETRESOURCE pNfsNetResource;
    INT  Index = *(PULONG)hEnum;


    DbgP((L"[aglo] NPEnumResource\n"));

    DbgP((L"[aglo] NPEnumResource Count Requested %d\n", *lpcCount));

    pNetResource = (LPNETRESOURCE) lpBuffer;
    SpaceAvailable = *lpBufferSize;
    EntriesCopied = 0;
    StringZone = (PWCHAR) ((PBYTE)lpBuffer + *lpBufferSize);

    Status = OpenSharedMemory( &hMutex,
                               &hMemory,
                               (PVOID)&pSharedMemory);

    if ( Status == WN_SUCCESS)
    {
        Status = WN_NO_MORE_ENTRIES;
        for (Index = *(PULONG)hEnum; EntriesCopied < *lpcCount &&
                Index < pSharedMemory->NextAvailableIndex; Index++)
        {
            pNfsNetResource = &pSharedMemory->NetResources[Index];

            if (pNfsNetResource->InUse)
            {
                SpaceNeeded  = sizeof( NETRESOURCE );
                SpaceNeeded += pNfsNetResource->LocalNameLength;
                SpaceNeeded += pNfsNetResource->RemoteNameLength;
                SpaceNeeded += 5 * sizeof(WCHAR);               // comment
                SpaceNeeded += sizeof(NFS41_PROVIDER_NAME_U);  // provider name
                if ( SpaceNeeded > SpaceAvailable )
                {
                    Status = WN_MORE_DATA;
                    DbgP((L"[aglo] NPEnumResource More Data Needed - %d\n", SpaceNeeded));
                    *lpBufferSize = SpaceNeeded;
                    break;
                }
                else
                {
                    SpaceAvailable -= SpaceNeeded;

                    pNetResource->dwScope       = pNfsNetResource->dwScope;
                    pNetResource->dwType        = pNfsNetResource->dwType;
                    pNetResource->dwDisplayType = pNfsNetResource->dwDisplayType;
                    pNetResource->dwUsage       = pNfsNetResource->dwUsage;

                    // setup string area at opposite end of buffer
                    SpaceNeeded -= sizeof( NETRESOURCE );
                    StringZone = (PWCHAR)( (PBYTE) StringZone - SpaceNeeded );
                    // copy local name
                    StringCchCopy( StringZone,
                        pNfsNetResource->LocalNameLength,
                        pNfsNetResource->LocalName );
                    pNetResource->lpLocalName = StringZone;
                    StringZone += pNfsNetResource->LocalNameLength/sizeof(WCHAR);
                    // copy remote name
                    StringCchCopy( StringZone,
                        pNfsNetResource->RemoteNameLength,
                        pNfsNetResource->RemoteName );
                    pNetResource->lpRemoteName = StringZone;
                    StringZone += pNfsNetResource->RemoteNameLength/sizeof(WCHAR);
                    // copy comment
                    pNetResource->lpComment = StringZone;
                    *StringZone++ = L'A';
                    *StringZone++ = L'_';
                    *StringZone++ = L'O';
                    *StringZone++ = L'K';
                    *StringZone++ = L'\0';
                    // copy provider name
                    pNetResource->lpProvider = StringZone;
                    StringCbCopyW( StringZone, sizeof(NFS41_PROVIDER_NAME_U), NFS41_PROVIDER_NAME_U );
                    StringZone += sizeof(NFS41_PROVIDER_NAME_U)/sizeof(WCHAR);
                    EntriesCopied++;
                    // set new bottom of string zone
                    StringZone = (PWCHAR)( (PBYTE) StringZone - SpaceNeeded );
                    Status = WN_SUCCESS;
                }
                pNetResource++;
            }
        }
        CloseSharedMemory( &hMutex, &hMemory, (PVOID*)&pSharedMemory);
    }

    *lpcCount = EntriesCopied;
    *(PULONG) hEnum = Index;

    DbgP((L"[aglo] NPEnumResource entries returned: %d\n", EntriesCopied));

    return Status;
}

DWORD APIENTRY
NPCloseEnum(
    HANDLE hEnum )
{
    DbgP((L"[aglo] NPCloseEnum\n"));
    HeapFree( GetProcessHeap( ), 0, (PVOID) hEnum );
    return WN_SUCCESS;
}

DWORD APIENTRY
NPGetResourceParent(
    LPNETRESOURCE   lpNetResource,
    LPVOID  lpBuffer,
    LPDWORD lpBufferSize )
{
    DbgP(( L"[aglo] NPGetResourceParent: WN_NOT_SUPPORTED\n" ));
    return WN_NOT_SUPPORTED;
}

DWORD APIENTRY
NPGetResourceInformation(
    __in LPNETRESOURCE   lpNetResource,
    __out_bcount(*lpBufferSize) LPVOID  lpBuffer,
    __inout LPDWORD lpBufferSize,
    __deref_out LPWSTR *lplpSystem )
{
    DbgP(( L"[aglo] NPGetResourceInformation: WN_NOT_SUPPORTED\n" ));
    return WN_NOT_SUPPORTED;
}

DWORD APIENTRY
NPGetUniversalName(
    LPCWSTR lpLocalPath,
    DWORD   dwInfoLevel,
    LPVOID  lpBuffer,
    LPDWORD lpBufferSize )
{
    DbgP(( L"[aglo] NPGetUniversalName: WN_NOT_SUPPORTED\n" ));
    return WN_NOT_SUPPORTED;
}