summaryrefslogtreecommitdiffstats
path: root/private/net/svcdlls/lls/server/msvctbl.c
blob: 44e5a812420dc4e07c3db824268626705bcf7019 (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
712
/*++

Copyright (c) 1994  Microsoft Corporation

Module Name:

   msvctbl.c

Abstract:

   Master Service Table routines.  Handles all access to the master service
   table kept for per-seat information.

   =========================== DATA STRUCTURES =============================

   MasterServiceTable (PMASTER_SERVICE_RECORD *)
   MasterServiceList (PMASTER_SERVICE_RECORD *)
      Each of these points to an array of pointers to dynamically allocated
      MASTER_SERVICE_RECORDs.  There is exactly one MASTER_SERVICE_RECORD
      for each (product, version) pairing; e.g., (SQL 4.0, SNA 2.0,
      SNA 2.1).  The MasterServiceTable is never re-ordered, so a valid
      index into this table is guaranteed to always dereference to the same
      (product, version).  The MasterServiceList contains the same data
      sorted lexicographically by product name (and therefore the data
      pointed to by a specific index may change over time as new
      (product, version) pairs are added to the table).  Each table
      contains MasterServiceListSize entries.

   RootServiceList (PMASTER_SERVICE_ROOT *)
      This points to an array of pointers to dynamically allocated
      MASTER_SERVICE_ROOTs.  There is exactly one MASTER_SERVICE_ROOT
      for each product family.  Each MASTER_SERVICE_ROOT contains a
      pointer to an array of indices into the MasterServiceTable
      corresponding to all the products in the family, sorted by
      ascending version number.  The RootServiceList itself is
      sorted lexicographically by ascending family name.  It contains
      RootServiceListSize entries.

Author:

   Arthur Hanson (arth) 07-Dec-1994

Revision History:

   Jeff Parham (jeffparh)  05-Dec-1995
      o  Added a few comments.
      o  Added parameter to LicenseServiceListFind().
      o  Fixed benign bug in memory allocation:
         sizeof(PULONG) -> sizeof(ULONG).

--*/

#include <stdlib.h>
#include <nt.h>
#include <ntrtl.h>
#include <nturtl.h>
#include <windows.h>

#include "llsapi.h"
#include "debug.h"
#include "llssrv.h"
#include "registry.h"
#include "ntlsapi.h"
#include "mapping.h"
#include "msvctbl.h"
#include "svctbl.h"
#include "purchase.h"
#include "perseat.h"


/////////////////////////////////////////////////////////////////////////
//
// Master Service Table - Keeps list of products (SQL, SNA, etc.) with a 
// sub-list for each version of the product.
//

/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////

#define DEFAULT_SERVICE_TABLE_ENTRIES 10

ULONG RootServiceListSize = 0;
PMASTER_SERVICE_ROOT *RootServiceList = NULL;

ULONG MasterServiceListSize = 0;
PMASTER_SERVICE_RECORD *MasterServiceList = NULL;
PMASTER_SERVICE_RECORD *MasterServiceTable = NULL;

TCHAR BackOfficeStr[100];
PMASTER_SERVICE_RECORD BackOffice;


RTL_RESOURCE MasterServiceListLock;


/////////////////////////////////////////////////////////////////////////
VOID
MasterServiceListInit()

/*++

Routine Description:

   Creates the Master Service table, used for tracking the services and 
   session count.  This will pull the initial services from the registry.

   The table is linear so a binary search can be used on the table, so 
   some extra records are initialized so that each time we add a new 
   service we don't have to do a realloc.  We also assume that adding
   new services is a relatively rare occurance, since we need to sort
   it each time.

   The service table is guarded by a read and write semaphore.  Multiple
   reads can occur, but a write blocks everything.

   The service table has two default entries for FilePrint and REMOTE_ACCESS.

Arguments:

   None.

Return Value:

   None.

--*/

{
   int nLen;
   HANDLE LlsDLLHandle = NULL;

   RtlInitializeResource(&MasterServiceListLock);

   memset(BackOfficeStr, 0, sizeof(BackOffice));
   BackOffice = NULL;
   LlsDLLHandle = LoadLibrary(TEXT("LLSRPC.DLL"));
   ASSERT(LlsDLLHandle != NULL);

   if (LlsDLLHandle != NULL) {
      nLen = LoadString(LlsDLLHandle, IDS_BACKOFFICE, BackOfficeStr, sizeof(BackOfficeStr));

      if (nLen != 0) {
         BackOffice = MasterServiceListAdd( BackOfficeStr, BackOfficeStr, 0 );
      } else {
#if DBG
         dprintf(TEXT("LLS ERROR: Could not load BackOffice string\n"));
#endif
      }

   }

} // MasterServiceListInit


/////////////////////////////////////////////////////////////////////////
// used by qsort to sort MasterServiceList by product name
int __cdecl MasterServiceListCompare(const void *arg1, const void *arg2) {
   PMASTER_SERVICE_RECORD Svc1, Svc2;

   Svc1 = (PMASTER_SERVICE_RECORD) *((PMASTER_SERVICE_RECORD *) arg1);
   Svc2 = (PMASTER_SERVICE_RECORD) *((PMASTER_SERVICE_RECORD *) arg2);

   return lstrcmpi( Svc1->Name, Svc2->Name );

} // MasterServiceListCompare


/////////////////////////////////////////////////////////////////////////
// used by qsort to sort the Services array of indices pointed to by the
// MASTER_SERVICE_ROOT structure by product version number
int __cdecl MServiceRecordCompare(const void *arg1, const void *arg2) {
   PMASTER_SERVICE_RECORD Svc1, Svc2;

   Svc1 = (PMASTER_SERVICE_RECORD) MasterServiceTable[*((PULONG) arg1)];
   Svc2 = (PMASTER_SERVICE_RECORD) MasterServiceTable[*((PULONG) arg2)];

   return (int) Svc1->Version - Svc2->Version;

} // MServiceRecordCompare


/////////////////////////////////////////////////////////////////////////
// used by qsort to sort the RootServiceList array of product families
// by family name
int __cdecl MServiceRootCompare(const void *arg1, const void *arg2) {
   PMASTER_SERVICE_ROOT Svc1, Svc2;

   Svc1 = (PMASTER_SERVICE_ROOT) *((PMASTER_SERVICE_ROOT *) arg1);
   Svc2 = (PMASTER_SERVICE_ROOT) *((PMASTER_SERVICE_ROOT *) arg2);

   return lstrcmpi( Svc1->Name, Svc2->Name );

} // MServiceRootCompare


/////////////////////////////////////////////////////////////////////////
PMASTER_SERVICE_ROOT
MServiceRootFind(
   LPTSTR ServiceName
   )

/*++

Routine Description:

   Internal routine to actually do binary search on MasterServiceList, this
   does not do any locking as we expect the wrapper routine to do this.
   The search is a simple binary search.

Arguments:

   ServiceName -

Return Value:

   Pointer to found service table entry or NULL if not found.

--*/

{
   LONG begin = 0;
   LONG end = (LONG) RootServiceListSize - 1;
   LONG cur;
   int match;
   PMASTER_SERVICE_ROOT ServiceRoot;

#if DBG
   if (TraceFlags & TRACE_FUNCTION_TRACE)
      dprintf(TEXT("LLS TRACE: MServiceRootFind\n"));
#endif

   if ((RootServiceListSize == 0) || (ServiceName == NULL))
      return NULL;

   while (end >= begin) {
      // go halfway in-between
      cur = (begin + end) / 2;
      ServiceRoot = RootServiceList[cur];

      // compare the two result into match
      match = lstrcmpi(ServiceName, ServiceRoot->Name);

      if (match < 0)
         // move new begin
         end = cur - 1;
      else
         begin = cur + 1;

      if (match == 0)
         return ServiceRoot;
   }

   return NULL;

} // MServiceRootFind


/////////////////////////////////////////////////////////////////////////
PMASTER_SERVICE_RECORD
MasterServiceListFind(
   LPTSTR Name
   )

/*++

Routine Description:

   Internal routine to actually do binary search on MasterServiceList, this
   does not do any locking as we expect the wrapper routine to do this.
   The search is a simple binary search.

Arguments:

   ServiceName -

Return Value:

   Pointer to found service table entry or NULL if not found.

--*/

{
   LONG begin = 0;
   LONG end;
   LONG cur;
   int match;
   PMASTER_SERVICE_RECORD Service;

#if DBG
   if (TraceFlags & TRACE_FUNCTION_TRACE)
      dprintf(TEXT("LLS TRACE: MasterServiceListFind\n"));
#endif

   if ((Name == NULL) || (MasterServiceListSize == 0))
      return NULL;

   end = (LONG) MasterServiceListSize - 1;

   while (end >= begin) {
      // go halfway in-between
      cur = (begin + end) / 2;
      Service = MasterServiceList[cur];

      // compare the two result into match
      match = lstrcmpi(Name, Service->Name);

      if (match < 0)
         // move new begin
         end = cur - 1;
      else
         begin = cur + 1;

      if (match == 0)
         return Service;
   }

   return NULL;

} // MasterServiceListFind


/////////////////////////////////////////////////////////////////////////
PMASTER_SERVICE_RECORD
MasterServiceListAdd(
   LPTSTR FamilyName,
   LPTSTR Name,
   DWORD Version
   )

/*++

Routine Description:


Arguments:

   ServiceName -

Return Value:

   Pointer to added service table entry, or NULL if failed.

--*/

{
   ULONG i;
   ULONG SessionLimit = 0;
   BOOL PerSeatLicensing = FALSE;
   LPTSTR NewServiceName, pDisplayName;
   PMASTER_SERVICE_RECORD Service = NULL;
   PMASTER_SERVICE_ROOT ServiceRoot = NULL;
   PULONG ServiceList;
   PLICENSE_SERVICE_RECORD pLicense;

#if DBG
   if (TraceFlags & TRACE_FUNCTION_TRACE)
      dprintf(TEXT("LLS TRACE: MasterServiceListAdd\n"));
#endif

   //
   // Mask off low word of version - as it doesn't matter to licensing
   //
   Version &= 0xFFFF0000;

   Service = MasterServiceListFind(Name);
   if (Service != NULL)
      return Service;

   //
   // Try to find a root node for that family of products
   //
   ServiceRoot = MServiceRootFind(FamilyName);

   if (ServiceRoot == NULL) {
      //
      // No root record - so create a new one
      //
      if (RootServiceList == NULL)
         RootServiceList = (PMASTER_SERVICE_ROOT *) LocalAlloc(LPTR, sizeof(PMASTER_SERVICE_ROOT));
      else
         RootServiceList = (PMASTER_SERVICE_ROOT *) LocalReAlloc(RootServiceList, sizeof(PMASTER_SERVICE_ROOT) * (RootServiceListSize + 1), LHND);

      //
      // Make sure we could allocate service table
      //
      if (RootServiceList == NULL) {
         ASSERT(FALSE);
         RootServiceListSize = 0;
         return NULL;
      }

      //
      // Allocate space for Root.
      //
      ServiceRoot = (PMASTER_SERVICE_ROOT) LocalAlloc(LPTR, sizeof(MASTER_SERVICE_ROOT));
      if (ServiceRoot == NULL) {
         ASSERT(FALSE);
         return NULL;
      }

      RootServiceList[RootServiceListSize] = ServiceRoot;

      NewServiceName = (LPTSTR) LocalAlloc(LPTR, (lstrlen(FamilyName) + 1) * sizeof(TCHAR));
      if (NewServiceName == NULL) {
         ASSERT(FALSE);
         LocalFree(ServiceRoot);
         return NULL;
      }

      // now copy it over...
      ServiceRoot->Name = NewServiceName;
      lstrcpy(NewServiceName, FamilyName);

      //
      // Initialize stuff for list of various versions of this product
      //
      ServiceRoot->ServiceTableSize = 0;
      ServiceRoot->Services = NULL;
      ServiceRoot->Flags = 0;

      RtlInitializeResource(&ServiceRoot->ServiceLock);

      RootServiceListSize++;

      // Have added the entry - now need to sort it in order of the service names
      qsort((void *) RootServiceList, (size_t) RootServiceListSize, sizeof(PMASTER_SERVICE_ROOT), MServiceRootCompare);

   }

   ////////////////////////////////////////////////////////////////////////
   //
   // Whether added or found, ServiceRoot points to the Root Node entry.
   // Now double check to see if another thread just got done adding the
   // actual service before we got the write lock.
   //
   RtlAcquireResourceShared(&ServiceRoot->ServiceLock, TRUE);
   Service = MasterServiceListFind(Name);

   if (Service == NULL) {
      //
      // No Service Record - so create a new one
      //
      RtlConvertSharedToExclusive(&ServiceRoot->ServiceLock);
      ServiceList = ServiceRoot->Services;
      if (ServiceList == NULL)
         ServiceList = (PULONG) LocalAlloc(LPTR, sizeof(ULONG));
      else
         ServiceList = (PULONG) LocalReAlloc(ServiceList, sizeof(ULONG) * (ServiceRoot->ServiceTableSize + 1), LHND);

      if (MasterServiceList == NULL) {
         MasterServiceList = (PMASTER_SERVICE_RECORD *) LocalAlloc(LPTR, sizeof(PMASTER_SERVICE_RECORD));
         MasterServiceTable = (PMASTER_SERVICE_RECORD *) LocalAlloc(LPTR, sizeof(PMASTER_SERVICE_RECORD));
      } else {
         MasterServiceList = (PMASTER_SERVICE_RECORD *) LocalReAlloc(MasterServiceList, sizeof(PMASTER_SERVICE_RECORD) * (MasterServiceListSize + 1), LHND);
         MasterServiceTable = (PMASTER_SERVICE_RECORD *) LocalReAlloc(MasterServiceTable, sizeof(PMASTER_SERVICE_RECORD) * (MasterServiceListSize + 1), LHND);
      }

      ServiceRoot->Services = ServiceList;

      //
      // Make sure we could allocate service table
      //
      if ((ServiceList == NULL) || (MasterServiceList == NULL) || (MasterServiceTable == NULL)) {
         ASSERT(FALSE);
         ServiceRoot->ServiceTableSize = 0;
         MasterServiceListSize = 0;
         MasterServiceList = NULL;
         MasterServiceTable = NULL;
         RtlReleaseResource(&ServiceRoot->ServiceLock);
         return NULL;
      }

      //
      // Allocate space for saving off Service Record.
      //
      Service = (PMASTER_SERVICE_RECORD) LocalAlloc(LPTR, sizeof(MASTER_SERVICE_RECORD));
      if (Service == NULL) {
         ASSERT(FALSE);
         RtlReleaseResource(&ServiceRoot->ServiceLock);
         return NULL;
      }

      ServiceList[ServiceRoot->ServiceTableSize] = MasterServiceListSize;
      MasterServiceList[MasterServiceListSize] = Service;
      MasterServiceTable[MasterServiceListSize] = Service;

      //
      // ...DisplayName
      //
      NewServiceName = (LPTSTR) LocalAlloc(LPTR, (lstrlen(Name) + 1) * sizeof(TCHAR));
      if (NewServiceName == NULL) {
         ASSERT(FALSE);
         LocalFree(Service);
         RtlReleaseResource(&ServiceRoot->ServiceLock);
         return NULL;
      }

      // now copy it over...
      Service->Name = NewServiceName;
      lstrcpy(NewServiceName, Name);

      //
      // Init rest of values.
      //
      Service->Version= Version;
      Service->LicensesUsed = 0;
      Service->LicensesClaimed = 0;
      Service->next = 0;
      Service->Index = MasterServiceListSize;
      Service->Family = ServiceRoot;

      pLicense = LicenseServiceListFind(Service->Name, FALSE);
      if (pLicense == NULL)
         Service->Licenses = 0;
      else
         Service->Licenses = pLicense->NumberLicenses;

      //
      // Init next pointer
      //
      i = 0;
      while ((i < ServiceRoot->ServiceTableSize) && (MasterServiceTable[ServiceRoot->Services[i]]->Version < Version))
         i++;

      if (i > 0) {
         Service->next = MasterServiceTable[ServiceRoot->Services[i - 1]]->next;
         MasterServiceTable[ServiceRoot->Services[i - 1]]->next = Service->Index + 1;
      }

      ServiceRoot->ServiceTableSize++;
      MasterServiceListSize++;

      // Have added the entry - now need to sort it in order of the versions
      qsort((void *) ServiceList, (size_t) ServiceRoot->ServiceTableSize, sizeof(PMASTER_SERVICE_RECORD), MServiceRecordCompare);

      // And sort the list the UI uses (sorted by service name)
      qsort((void *) MasterServiceList, (size_t) MasterServiceListSize, sizeof(PMASTER_SERVICE_RECORD), MasterServiceListCompare);
   }

   RtlReleaseResource(&ServiceRoot->ServiceLock);
   return Service;

} // MasterServiceListAdd


#if DBG

/////////////////////////////////////////////////////////////////////////
//
//                   DEBUG INFORMATION DUMP ROUTINES
//
/////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////
VOID
MasterServiceRootDebugDump( )

/*++

Routine Description:


Arguments:


Return Value:


--*/

{
   ULONG i = 0;

   //
   // Need to scan list so get read access.
   //
   RtlAcquireResourceShared(&MasterServiceListLock, TRUE);

   dprintf(TEXT("Service Family Table, # Entries: %lu\n"), RootServiceListSize);
   if (RootServiceList == NULL)
      goto MasterServiceRootDebugDumpExit;

   for (i = 0; i < RootServiceListSize; i++) {
      dprintf(TEXT("%3lu) Services: %3lu Svc: %s [%s]\n"),
         i + 1, RootServiceList[i]->ServiceTableSize, RootServiceList[i]->Name, RootServiceList[i]->Name);
   }

MasterServiceRootDebugDumpExit:
   RtlReleaseResource(&MasterServiceListLock);

   return;
} // MasterServiceRootDebugDump


/////////////////////////////////////////////////////////////////////////
VOID
MasterServiceRootDebugInfoDump( PVOID Data )

/*++

Routine Description:


Arguments:


Return Value:


--*/

{
   ULONG i = 0;

   //
   // Need to scan list so get read access.
   //
   RtlAcquireResourceShared(&MasterServiceListLock, TRUE);

   dprintf(TEXT("Service Family Table, # Entries: %lu\n"), RootServiceListSize);
   if (RootServiceList == NULL)
      goto MasterServiceRootDebugDumpExit;

   for (i = 0; i < RootServiceListSize; i++) {
      dprintf(TEXT("%3lu) Services: %3lu Svc: %s [%s]\n"),
         i + 1, RootServiceList[i]->ServiceTableSize, RootServiceList[i]->Name, RootServiceList[i]->Name);
   }

MasterServiceRootDebugDumpExit:
   RtlReleaseResource(&MasterServiceListLock);

   return;
} // MasterServiceRootDebugInfoDump


/////////////////////////////////////////////////////////////////////////
VOID
MasterServiceListDebugDump( )

/*++

Routine Description:


Arguments:


Return Value:


--*/

{
   ULONG i = 0;

   //
   // Need to scan list so get read access.
   //
   RtlAcquireResourceShared(&MasterServiceListLock, TRUE);

   dprintf(TEXT("Master Service Table, # Entries: %lu\n"), MasterServiceListSize);
   if (MasterServiceList == NULL)
      goto MasterServiceListDebugDumpExit;

   for (i = 0; i < MasterServiceListSize; i++) {
      dprintf(TEXT("%3lu) [%3lu] LU: %4lu LP: %4lu LC: %4lu MS: %4lu HM: %4lu Next: %3lu Svc: %s %lX\n"),
         i + 1, MasterServiceList[i]->Index, 
         MasterServiceList[i]->LicensesUsed, MasterServiceList[i]->Licenses, MasterServiceList[i]->LicensesClaimed,
         MasterServiceList[i]->MaxSessionCount, MasterServiceList[i]->HighMark,
         MasterServiceList[i]->next, MasterServiceList[i]->Name, MasterServiceList[i]->Version);
   }

MasterServiceListDebugDumpExit:
   RtlReleaseResource(&MasterServiceListLock);

   return;
} // MasterServiceListDebugDump


/////////////////////////////////////////////////////////////////////////
VOID
MasterServiceListDebugInfoDump( PVOID Data )

/*++

Routine Description:


Arguments:


Return Value:


--*/

{
   PMASTER_SERVICE_RECORD CurrentRecord = NULL;

   dprintf(TEXT("Master Service Table, # Entries: %lu\n"), RootServiceListSize);

   if (lstrlen((LPWSTR) Data) > 0) {
//      CurrentRecord = MasterServiceListFind((LPWSTR) Data);
      if (CurrentRecord != NULL) {
      }
   }

} // MasterServiceListDebugInfoDump

#endif