summaryrefslogtreecommitdiffstats
path: root/private/net/svcdlls/rpl/server/config.c
blob: 698cb962eec6f1ca3089b6a25d5e05e5e7b1a868 (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
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
/*++

Copyright (c) 1993  Microsoft Corporation

Module Name:

    config.c

Abstract:

    This module contains RPL config apis: ConfigEnum.

Author:

    Vladimir Z. Vulovic     (vladimv)       05 - November - 1993

Revision History:

    05-Nov-1993     vladimv
        Created

--*/

#include "local.h"
#include "rpldb.h"
#include "db.h"
#include "dblib.h"
#include "boot.h"
#include "profile.h"
#define RPLCONFIG_ALLOCATE
#include "config.h"
#undef RPLCONFIG_ALLOCATE
#include "setup.h"      //  IsConfigEnabled()



DWORD ConfigSetInfo(
    IN      PRPL_SESSION        pSession,
    IN      DWORD               Level,
    IN      LPVOID              Buffer,
    OUT     LPDWORD             pErrorParameter
    )
{
    LPRPL_CONFIG_INFO_2         Info = Buffer;
    switch( Level) {
    case 2:
        if ( Info->FitPersonal != NULL) {
            *pErrorParameter = CONFIG_FitPersonal;
            CallM( JetSetColumn( pSession->SesId, pSession->ConfigTableId,
                ConfigTable[ CONFIG_FitPersonal].ColumnId,
                Info->FitPersonal,
                ( wcslen( Info->FitPersonal) + 1) * sizeof(WCHAR),
                0, NULL));
        }
        if ( Info->FitShared != NULL) {
            *pErrorParameter = CONFIG_FitShared;
            CallM( JetSetColumn( pSession->SesId, pSession->ConfigTableId,
                ConfigTable[ CONFIG_FitShared].ColumnId,
                Info->FitShared,
                ( wcslen( Info->FitShared) + 1) * sizeof(WCHAR),
                0, NULL));
        }
        if ( Info->DirName4 != NULL) {
            *pErrorParameter = CONFIG_DirName4;
            CallM( JetSetColumn( pSession->SesId, pSession->ConfigTableId,
                ConfigTable[ CONFIG_DirName4].ColumnId,
                Info->DirName4,
                ( wcslen( Info->DirName4) + 1) * sizeof(WCHAR),
                0, NULL));
        }
        if ( Info->DirName3 != NULL) {
            *pErrorParameter = CONFIG_DirName3;
            CallM( JetSetColumn( pSession->SesId, pSession->ConfigTableId,
                ConfigTable[ CONFIG_DirName3].ColumnId,
                Info->DirName3,
                ( wcslen( Info->DirName3) + 1) * sizeof(WCHAR),
                0, NULL));
        }
        if ( Info->DirName2 != NULL) {
            *pErrorParameter = CONFIG_DirName2;
            CallM( JetSetColumn( pSession->SesId, pSession->ConfigTableId,
                ConfigTable[ CONFIG_DirName2].ColumnId,
                Info->DirName2,
                ( wcslen( Info->DirName2) + 1) * sizeof(WCHAR),
                0, NULL));
        }
        if ( Info->DirName != NULL) {
            *pErrorParameter = CONFIG_DirName;
            CallM( JetSetColumn( pSession->SesId, pSession->ConfigTableId,
                ConfigTable[ CONFIG_DirName].ColumnId,
                Info->DirName,
                ( wcslen( Info->DirName) + 1) * sizeof(WCHAR),
                0, NULL));
        }
        if ( Info->BootName != NULL) {
            *pErrorParameter = CONFIG_BootName;
            CallM( JetSetColumn( pSession->SesId, pSession->ConfigTableId,
                ConfigTable[ CONFIG_BootName].ColumnId,
                Info->BootName,
                ( wcslen( Info->BootName) + 1) * sizeof(WCHAR),
                0, NULL));
        }
        NOTHING;    //  fall through
    case 1:
        if ( Info->Flags != 0) {
            *pErrorParameter = CONFIG_Flags;
            CallM( JetSetColumn( pSession->SesId, pSession->ConfigTableId,
                ConfigTable[ CONFIG_Flags].ColumnId,
                &Info->Flags, sizeof( Info->Flags), 0, NULL));
        }
        NOTHING;    //  fall through
    case 0:
        if ( Info->ConfigComment != NULL) {
            *pErrorParameter = CONFIG_ConfigComment;
            CallM( JetSetColumn( pSession->SesId, pSession->ConfigTableId,
                ConfigTable[ CONFIG_ConfigComment].ColumnId,
                Info->ConfigComment,
                ( wcslen( Info->ConfigComment) + 1) * sizeof(WCHAR),
                0, NULL));
        }
        if ( Info->ConfigName != NULL) {
            *pErrorParameter = CONFIG_ConfigName;
            CallM( JetSetColumn( pSession->SesId, pSession->ConfigTableId,
                ConfigTable[ CONFIG_ConfigName].ColumnId,
                Info->ConfigName,
                ( wcslen( Info->ConfigName) + 1) * sizeof(WCHAR),
                0, NULL));
        }
        break;
    }
    return( NO_ERROR);
}


DWORD ConfigGetField(
    IN      PRPL_SESSION        pSession,
    IN      DWORD               FieldIndex,
    OUT     LPVOID *            pData,
    IN OUT  LPINT               pSpaceLeft
    )
{
    BYTE                LocalBuffer[ 300];
    PBYTE               Buffer;
    DWORD               DataSize;
    DWORD               BufferSize;
    JET_ERR             JetError;

    switch( FieldIndex) {
    case CONFIG_Flags:
        Buffer = (PBYTE)pData;
        BufferSize = sizeof( DWORD);
        break;
    default:
        Buffer = LocalBuffer;
        BufferSize = sizeof( LocalBuffer);
        break;
    }
    JetError = JetRetrieveColumn( pSession->SesId, pSession->ConfigTableId,
        ConfigTable[ FieldIndex].ColumnId, Buffer,
        BufferSize, &DataSize, 0, NULL);
    if ( JetError < 0) {
        RplDump( ++RG_Assert, ("JetError=%d", JetError));
        return( NERR_RplConfigInfoCorrupted);
    }
    if ( Buffer != LocalBuffer) {
        if ( BufferSize == DataSize) {
            return( NO_ERROR);
        } else {
            RplDump( ++RG_Assert, ("Bad DataSize=0x%x", DataSize));
            return( NERR_RplConfigInfoCorrupted);
        }
    }
    //
    //  We have done with fixed data.  From here on we deal with unicode
    //  strings only.
    //
    if ( DataSize > sizeof( LocalBuffer)) {
        RplDump( ++RG_Assert, ( "Too big DataSize=0x%x", DataSize));
        return( NERR_RplConfigInfoCorrupted);
    }
    if ( DataSize == 0) {
        if ( JetError != JET_wrnColumnNull) {
            RplDump( ++RG_Assert, ( "JetError=%d", JetError));
            return( NERR_RplConfigInfoCorrupted);
        } else {
            *pData = NULL; // so RPC rpcrt4!_tree_size_ndr() does not bomb here
            return( NO_ERROR);
        }
    }
    if ( DataSize & 1 != 0  ||  wcslen((PWCHAR)LocalBuffer) + 1 != DataSize/2) {
        RplDump( ++RG_Assert, ("LocalBuffer=0x%x, DataSize=0x%x", LocalBuffer, DataSize));
        return( NERR_RplConfigInfoCorrupted);
    }
    *pData = MIDL_user_allocate( DataSize);
    if ( *pData == NULL) {
        RplDump( ++RG_Assert, ( "Error=%d", GetLastError()));
        RPL_RETURN( ERROR_NOT_ENOUGH_MEMORY);
    }
    memcpy( *pData, LocalBuffer, DataSize);
    *pSpaceLeft -= DataSize; // BUGBUG might become negative?
    return( NO_ERROR);
}


DWORD ConfigGetInfo(
    IN      PRPL_SESSION        pSession,
    IN      LPWSTR              ConfigName,
    IN      DWORD               Level,
    OUT     LPVOID              Buffer,
    IN OUT  LPINT               pSpaceLeft
    )
{
    DWORD                   Error;
    LPRPL_CONFIG_INFO_2     Info = Buffer;

    switch( Level) {
    case 2:
        Error = ConfigGetField( pSession, CONFIG_FitPersonal, &Info->FitPersonal, pSpaceLeft);
        if ( Error != NO_ERROR) {
            return( Error);
        }
        Error = ConfigGetField( pSession, CONFIG_FitShared, &Info->FitShared, pSpaceLeft);
        if ( Error != NO_ERROR) {
            return( Error);
        }
        Error = ConfigGetField( pSession, CONFIG_DirName4, &Info->DirName4, pSpaceLeft);
        if ( Error != NO_ERROR) {
            return( Error);
        }
        Error = ConfigGetField( pSession, CONFIG_DirName3, &Info->DirName3, pSpaceLeft);
        if ( Error != NO_ERROR) {
            return( Error);
        }
        Error = ConfigGetField( pSession, CONFIG_DirName2, &Info->DirName2, pSpaceLeft);
        if ( Error != NO_ERROR) {
            return( Error);
        }
        Error = ConfigGetField( pSession, CONFIG_DirName, &Info->DirName, pSpaceLeft);
        if ( Error != NO_ERROR) {
            return( Error);
        }
        Error = ConfigGetField( pSession, CONFIG_BootName, &Info->BootName, pSpaceLeft);
        if ( Error != NO_ERROR) {
            return( Error);
        }
        NOTHING;    //  fall through
    case 1:
        Error = ConfigGetField( pSession, CONFIG_Flags, (LPVOID *)&Info->Flags, pSpaceLeft);
        if ( Error != NO_ERROR) {
            return( Error);
        }
    case 0:
        Error = ConfigGetField( pSession, CONFIG_ConfigComment, &Info->ConfigComment, pSpaceLeft);
        if ( Error != NO_ERROR) {
            return( Error);
        }
        if ( ConfigName == NULL) {
            Error = ConfigGetField( pSession, CONFIG_ConfigName, &Info->ConfigName, pSpaceLeft);
            if ( Error != NO_ERROR) {
                return( Error);
            }
        } else {
            DWORD   DataSize = (wcslen( ConfigName) + 1) * sizeof(WCHAR);
            Info->ConfigName = MIDL_user_allocate( DataSize);
            if ( Info->ConfigName == NULL) {
                return( ERROR_NOT_ENOUGH_MEMORY);
            }
            RplDump( RG_DebugLevel & RPL_DEBUG_CONFIG, ( "ConfigName=0x%x", Info->ConfigName));
            memcpy( Info->ConfigName, ConfigName, DataSize);
            *pSpaceLeft -= DataSize;
        }
        break;
    default:
        return( ERROR_INVALID_LEVEL);
        break;
    }
    return( NO_ERROR);
}



VOID ConfigGetInfoCleanup(
    IN      DWORD                       Level,
    IN OUT  LPVOID                      Buffer
    )
{
    LPRPL_CONFIG_INFO_2     Info = Buffer;

    switch( Level) {
    case 2:
        if ( Info->FitPersonal != NULL) {
            MIDL_user_free( Info->FitPersonal);
        }
        if ( Info->FitShared != NULL) {
            MIDL_user_free( Info->FitShared);
        }
        if ( Info->DirName4 != NULL) {
            MIDL_user_free( Info->DirName4);
        }
        if ( Info->DirName3 != NULL) {
            MIDL_user_free( Info->DirName3);
        }
        if ( Info->DirName2 != NULL) {
            MIDL_user_free( Info->DirName2);
        }
        if ( Info->DirName != NULL) {
            MIDL_user_free( Info->DirName);
        }
        if ( Info->BootName != NULL) {
            MIDL_user_free( Info->BootName);
        }
        NOTHING;    //  fall through
    case 1:
        NOTHING;    //  fall through
    case 0:
        if ( Info->ConfigComment != NULL) {
            MIDL_user_free( Info->ConfigComment);
        }
        if ( Info->ConfigName != NULL) {
            MIDL_user_free( Info->ConfigName);
        }
        break;
    }
}


NET_API_STATUS NET_API_FUNCTION
NetrRplConfigAdd(
    IN      RPL_HANDLE                  ServerHandle,
    IN      DWORD                       Level,
    OUT     LPRPL_CONFIG_INFO_STRUCT    ConfigInfoStruct,
    OUT     LPDWORD                     pErrorParameter      OPTIONAL
    )
{
    LPRPL_CONFIG_INFO_2     Info;
    LPVOID                  Buffer;
    DWORD                   Error;
    DWORD                   ErrorParameter;
    PRPL_SESSION            pSession = &RG_ApiSession;

    ErrorParameter = INVALID_ERROR_PARAMETER;
    Buffer = Info = ConfigInfoStruct->ConfigInfo2;

    switch( Level) {
    case 2:
        if ( !ValidName( Info->FitPersonal, RPL_MAX_STRING_LENGTH, TRUE)) {
            ErrorParameter = CONFIG_FitPersonal;
            break;
        }
        if ( !ValidName( Info->FitShared, RPL_MAX_STRING_LENGTH, TRUE)) {
            ErrorParameter = CONFIG_FitShared;
            break;
        }
        if ( !ValidName( Info->DirName4, RPL_MAX_STRING_LENGTH, FALSE)) {
            ErrorParameter = CONFIG_DirName4;
            break;
        }
        if ( !ValidName( Info->DirName3, RPL_MAX_STRING_LENGTH, FALSE)) {
            ErrorParameter = CONFIG_DirName3;
            break;
        }
        if ( !ValidName( Info->DirName2, RPL_MAX_STRING_LENGTH, TRUE)) {
            ErrorParameter = CONFIG_DirName2;
            break;
        }
        if ( !ValidName( Info->DirName, RPL_MAX_STRING_LENGTH, TRUE)) {
            ErrorParameter = CONFIG_DirName;
            break;
        }
        if ( !ValidName( Info->BootName, RPL_MAX_BOOT_NAME_LENGTH, TRUE)) {
            ErrorParameter = CONFIG_BootName;
            break;
        }
        _wcsupr( Info->BootName);
        if ( Info->Flags & ~CONFIG_FLAGS_MASK_ENABLED) {
            ErrorParameter = CONFIG_Flags;
            break;
        }
        switch ( Info->Flags) {
        case 0:
            Info->Flags = RplConfigEnabled( Info->DirName2) == TRUE ?
                CONFIG_FLAGS_ENABLED_TRUE : CONFIG_FLAGS_ENABLED_FALSE;
            break;
        case CONFIG_FLAGS_ENABLED_TRUE:
        case CONFIG_FLAGS_ENABLED_FALSE:
            break;
        default:
            ErrorParameter = CONFIG_Flags;
            break;
        }
        if ( RPL_STRING_TOO_LONG( Info->ConfigComment)) {
            ErrorParameter = CONFIG_ConfigComment;
            break;
        }
        if ( !ValidName( Info->ConfigName, RPL_MAX_CONFIG_NAME_LENGTH, TRUE)) {
            ErrorParameter = CONFIG_ConfigName;
            break;
        }
        _wcsupr( Info->ConfigName);
        break;
    default:
        return( ERROR_INVALID_LEVEL);
        break;
    }

    if ( ErrorParameter != INVALID_ERROR_PARAMETER) {
        if ( ARGUMENT_PRESENT( pErrorParameter)) {
            *pErrorParameter = ErrorParameter;
        }
        return( ERROR_INVALID_PARAMETER);
    }

    EnterCriticalSection( &RG_ProtectDatabase);
    Call( JetBeginTransaction( pSession->SesId));

    //
    //  Verify that ConfigName is available in the database.
    //
    if ( RplFind( pSession, CONFIG_TABLE_TAG, Info->ConfigName)) {
        Error = NERR_RplConfigNameUnavailable;
        goto cleanup;
    }

    CallJ( JetPrepareUpdate( pSession->SesId, pSession->ConfigTableId, JET_prepInsert));

    Error = ConfigSetInfo( pSession, Level, Buffer, &ErrorParameter);
    if ( Error == ERROR_SUCCESS) {
        ErrorParameter = 0;
        CallJ( JetUpdate( pSession->SesId, pSession->ConfigTableId, NULL, 0, NULL));
    }

cleanup:
    if ( Error == NO_ERROR) {
        Call( JetCommitTransaction( pSession->SesId, JET_bitCommitFlush));
    } else {
        Call( JetRollback( pSession->SesId, JET_bitRollbackAll));
    }
    LeaveCriticalSection( &RG_ProtectDatabase);

    if ( Error != ERROR_SUCCESS) {
         if ( ARGUMENT_PRESENT( pErrorParameter)) {
             *pErrorParameter = ErrorParameter;
         }
    }
    return( Error);
}


NET_API_STATUS NET_API_FUNCTION
NetrRplConfigDel(
    IN      RPL_HANDLE      ServerHandle,
    IN      LPWSTR          ConfigName
    )
/*++
--*/
{
    DWORD                   Error;
    PRPL_SESSION            pSession = &RG_ApiSession;

    _wcsupr( ConfigName);

    EnterCriticalSection( &RG_ProtectDatabase);
    Call( JetBeginTransaction( pSession->SesId));

    if ( RplFindByField( pSession, PROFILE_TABLE_TAG,
            PROFILE_INDEX_ConfigNameProfileName, ConfigName)) {
        //
        //  We found a PROFILE record which uses this CONFIG.
        //
        Error = NERR_RplConfigNotEmpty;
        goto cleanup;
    }
    if ( !RplFind( pSession, CONFIG_TABLE_TAG, ConfigName)) {
        Error = NERR_RplConfigNotFound;
        goto cleanup;
    }
    CallJ( JetDelete( pSession->SesId, pSession->ConfigTableId));
    Error = NO_ERROR;

cleanup:
    if ( Error == NO_ERROR) {
        Call( JetCommitTransaction( pSession->SesId, JET_bitCommitFlush));
    } else {
        Call( JetRollback( pSession->SesId, JET_bitRollbackAll));
    }
    LeaveCriticalSection( &RG_ProtectDatabase);
    return( Error);
}


NET_API_STATUS NET_API_FUNCTION
NetrRplConfigEnum(
    IN      RPL_RPC_HANDLE      ServerHandle,
    IN      PWCHAR              AdapterName,
    IN OUT  LPRPL_CONFIG_ENUM   ConfigEnum,
    IN      DWORD               PrefMaxLength,
    OUT     LPDWORD             TotalEntries,
    IN OUT  LPDWORD             pResumeHandle        OPTIONAL
    )
/*++

Routine Description:

    If AdapterName is null, enumerate all configurations.
    If AdapterName is not null, enumerate all configurations that can support
    this adapter id.

Arguments:

    pServerHandle - ptr to RPL_HANDLE

    InfoStruct - Pointer to a structure that contains the information that
        RPC needs about the returned data.  This structure contains the
        following information:
            Level - The desired information level - indicates how to
                interpret the structure of the returned buffer.
            EntriesRead - Indicates how many elements are returned in the
                 array of structures that are returned.
            BufferPointer - Location for the pointer to the array of
                 structures that are being returned.

    PrefMaxLen - Indicates a maximum size limit that the caller will allow
        for (the return buffer.

    TotalEntries - Pointer to a value that upon return indicates the total
        number of entries in the "active" database.

    pResumeHandle - Inidcates where to restart the enumeration.  This is an
        optional parameter and can be NULL.

Return Value:
    NO_ERROR if success.

--*/
{
    LPBYTE                  Buffer;
    DWORD                   TypicalSize;
    DWORD                   CoreSize;
    DWORD                   Error;
    INT                     SpaceLeft;
    DWORD                   ArrayLength;
    DWORD                   EntriesRead;
    BOOL                    InfoError;
    BOOL                    TableEnd;
    RPL_FILTER              Filter;
    PRPL_FILTER             pFilter;
    PRPL_SESSION            pSession = &RG_ApiSession;

    switch( ConfigEnum->Level) {
    case 2:
        TypicalSize = CoreSize = sizeof( RPL_CONFIG_INFO_2);
        TypicalSize += 20 * sizeof( WCHAR); //  typical size of FitPersonal
        TypicalSize += 20 * sizeof( WCHAR); //  typical size of FitShared
        TypicalSize += 12 * sizeof( WCHAR); //  typical size of DirName4
        TypicalSize += 12 * sizeof( WCHAR); //  typical size of DirName3
        TypicalSize += 12 * sizeof( WCHAR); //  typical size of DirName2
        TypicalSize += 12 * sizeof( WCHAR); //  typical size of DirName
        TypicalSize +=  8 * sizeof( WCHAR); //  typical size of BootName
        NOTHING;    //  fall through
    case 1:
        if ( ConfigEnum->Level == 1) {
            TypicalSize = CoreSize = sizeof( RPL_CONFIG_INFO_1);
        }
        NOTHING;    //  fall through
    case 0:
        if ( ConfigEnum->Level == 0) {
            TypicalSize = CoreSize = sizeof( RPL_CONFIG_INFO_0);
        }
        TypicalSize += 20 * sizeof( WCHAR); //  typical size of ConfigComment
        TypicalSize +=  8 * sizeof( WCHAR); //  typical size of ConfigName
        break;
    default:
        return( ERROR_INVALID_LEVEL);
        break;
    }

    if ( AdapterName != NULL) {
        pFilter = &Filter;
        if ( !ValidHexName( AdapterName, RPL_ADAPTER_NAME_LENGTH, TRUE)) {
            return( ERROR_INVALID_PARAMETER);
        }
        pFilter->VendorId = AdapterNameToVendorId( AdapterName);
        pFilter->FindFirst = TRUE;
    } else {
        pFilter = NULL;
    }

    if ( PrefMaxLength == -1) {
        //
        //  If the caller has not specified a size, calculate a size
        //  that will hold the entire enumeration.
        //
        SpaceLeft = DEFAULT_BUFFER_SIZE;
    } else {
        SpaceLeft = PrefMaxLength;
    }

    //
    //  Buffer space is shared by the array and by strings pointed at
    //  by the elements in this array.  We need to decide up front how much
    //  space is allocated for array and how much for the strings.
    //
    ArrayLength = SpaceLeft / TypicalSize;
    if ( ArrayLength == 0) {
        ArrayLength = 1;    //  try to return at least one element
    }

    //
    //  Note that MIDL_user_allocate() returns memory which is NOT initialized
    //  to zero.  Since we do NOT use allocate all nodes, this means that all
    //  fields, especially pointers, in array elements must be properly set.
    //
    Buffer = MIDL_user_allocate( ArrayLength * CoreSize);
    if ( Buffer == NULL) {
        return( ERROR_NOT_ENOUGH_MEMORY);
    }
    RplDump( RG_DebugLevel & RPL_DEBUG_CONFIG, (
        "ConfigEnum: Buffer=0x%x, ArrayLength=0x%x", Buffer, ArrayLength));
    ConfigEnum->ConfigInfo.Level0->Buffer = (LPRPL_CONFIG_INFO_0)Buffer;

    EntriesRead = 0;
    InfoError = FALSE;
    Error = NO_ERROR;

    EnterCriticalSection( &RG_ProtectDatabase);
    Call( JetBeginTransaction( pSession->SesId));

    if ( !RplFilterFirst( pSession, CONFIG_TABLE_TAG, pFilter, pResumeHandle, &TableEnd)) {
        Error = NERR_RplCannotEnum;
        goto cleanup;
    }
    if ( TableEnd == TRUE) {
        goto cleanup;
    }
    for ( ; ; ) {
        memset( Buffer, 0, CoreSize); // for cleanup to work properly
        Error = ConfigGetInfo( pSession, NULL, ConfigEnum->Level, Buffer, &SpaceLeft);
        if ( Error != NO_ERROR) {
            InfoError = TRUE;   //  clean things up without holding crit sec
            break;
        }
        EntriesRead++;
        Buffer += CoreSize;
        SpaceLeft -= CoreSize;
        if ( !RplFilterNext( pSession, pSession->ConfigTableId, pFilter, &TableEnd)) {
            Error = NERR_RplCannotEnum;
            goto cleanup;
        }
        if ( TableEnd == TRUE) {
            goto cleanup;
        }
        if ( SpaceLeft <= 0) {
            Error = ERROR_MORE_DATA;
            break;
        }
        if ( EntriesRead >= ArrayLength) {
            //
            //  We have space available but allocated array is not big enough.
            //  This should NOT happen often as our intent (see above) is to
            //  overestimate array length.  When it happens we can still try
            //  to reallocate array to a larger size here.  This is not done
            //  for now (too cumbersome) & we just stop the enumeration.
            //
            Error = ERROR_MORE_DATA;
            break;
        }
    }
cleanup:
    Call( JetCommitTransaction( pSession->SesId, 0));
    LeaveCriticalSection( &RG_ProtectDatabase);
    if ( InfoError == TRUE) {
        ConfigGetInfoCleanup( ConfigEnum->Level, Buffer);
    }
    if ( Error == NO_ERROR) {
        *TotalEntries = EntriesRead;
    } else if ( Error == ERROR_MORE_DATA) {
        *TotalEntries = EntriesRead * 2;    //  we cheat here
    } else {
        //
        //  Cleanup in case of "bad" errors.
        //
        while ( EntriesRead > 0) {
            EntriesRead--;
            Buffer -= CoreSize;
            ConfigGetInfoCleanup( ConfigEnum->Level, Buffer);
        }
        MIDL_user_free( Buffer);
    }

    RplDump( RG_DebugLevel & RPL_DEBUG_CONFIG, ("ConfigEnum: EntriesRead = 0x%x", EntriesRead));

    ConfigEnum->ConfigInfo.Level0->EntriesRead = EntriesRead;
    if ( EntriesRead == 0) {
        ConfigEnum->ConfigInfo.Level0->Buffer = NULL;
    }

    if ( ARGUMENT_PRESENT( pResumeHandle)) {
        if ( Error == ERROR_MORE_DATA  &&  EntriesRead > 0) {
            EnterCriticalSection( &RG_ProtectDatabase);
            Call( JetBeginTransaction( pSession->SesId));
            RplFilterSave( pSession, (DWORD)ServerHandle, pFilter,
                ((LPRPL_CONFIG_INFO_0)(Buffer-CoreSize))->ConfigName,
                pResumeHandle);
            Call( JetCommitTransaction( pSession->SesId, JET_bitCommitFlush));
            LeaveCriticalSection( &RG_ProtectDatabase);
        } else {
            *pResumeHandle = 0; // resume from beginning
        }
    }
    return( Error);
}