Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

354 lines
10 KiB

  1. /*++
  2. Copyright (c) 1992-1996 Microsoft Corporation
  3. Module Name:
  4. srvr_lm.c
  5. Abstract:
  6. This file contains the routines which actually call Lan Manager and
  7. retrieve the contents of the domain server table, including cacheing.
  8. Environment:
  9. User Mode - Win32
  10. Revision History:
  11. 10-May-1996 DonRyan
  12. Removed banner from Technology Dynamics, Inc.
  13. --*/
  14. //--------------------------- WINDOWS DEPENDENCIES --------------------------
  15. //--------------------------- STANDARD DEPENDENCIES -- #include<xxxxx.h> ----
  16. #ifdef WIN32
  17. #include <windows.h>
  18. #include <lm.h>
  19. #endif
  20. #include <tchar.h>
  21. #include <string.h>
  22. #include <search.h>
  23. #include <stdlib.h>
  24. #include <time.h>
  25. //--------------------------- MODULE DEPENDENCIES -- #include"xxxxx.h" ------
  26. #include "mib.h"
  27. #include "mibfuncs.h"
  28. #include "srvr_tbl.h"
  29. #include "lmcache.h"
  30. //--------------------------- SELF-DEPENDENCY -- ONE #include"module.h" -----
  31. //--------------------------- PUBLIC VARIABLES --(same as in module.h file)--
  32. //--------------------------- PRIVATE CONSTANTS -----------------------------
  33. #define SafeBufferFree(x) if(NULL != x) NetApiBufferFree( x )
  34. #define SafeFree(x) if(NULL != x) SnmpUtilMemFree( x )
  35. //--------------------------- PRIVATE STRUCTS -------------------------------
  36. //--------------------------- PRIVATE VARIABLES -----------------------------
  37. //--------------------------- PRIVATE PROTOTYPES ----------------------------
  38. //--------------------------- PRIVATE PROCEDURES ----------------------------
  39. int __cdecl srvr_entry_cmp(
  40. IN const DOM_SERVER_ENTRY *A,
  41. IN const DOM_SERVER_ENTRY *B
  42. ) ;
  43. BOOL build_srvr_entry_oids( );
  44. void FreeDomServerTable();
  45. //--------------------------- PUBLIC PROCEDURES -----------------------------
  46. //
  47. // MIB_srvr_lmget
  48. // Retrieve domain server table information from Lan Manager.
  49. // If not cached, sort it and then
  50. // cache it.
  51. //
  52. // Notes:
  53. //
  54. // Return Codes:
  55. // SNMPAPI_NOERROR
  56. // SNMPAPI_ERROR
  57. //
  58. // Error Codes:
  59. // None.
  60. //
  61. SNMPAPI MIB_svsond_lmget()
  62. {
  63. DWORD entriesread = 0;
  64. DWORD totalentries = 0;
  65. LPBYTE bufptr = NULL;
  66. unsigned lmCode;
  67. unsigned i;
  68. SERVER_INFO_100 *DataTable;
  69. DOM_SERVER_ENTRY *MIB_DomServerTableElement ;
  70. int First_of_this_block;
  71. time_t curr_time ;
  72. SNMPAPI nResult = SNMPAPI_NOERROR;
  73. DWORD resumehandle=0;
  74. DWORD dwAllocatedEntries=0;
  75. time(&curr_time); // get the time
  76. //
  77. //
  78. // If cached, return piece of info.
  79. //
  80. //
  81. if((NULL != cache_table[C_SRVR_TABLE].bufptr) &&
  82. (curr_time < (cache_table[C_SRVR_TABLE].acquisition_time + cache_expire[C_SRVR_TABLE]))
  83. )
  84. { // it has NOT expired!
  85. goto Exit ; // the global table is valid
  86. }
  87. // free the old table LOOK OUT!!
  88. FreeDomServerTable();
  89. First_of_this_block = 0;
  90. do
  91. {
  92. // as long as there is more data to process
  93. //
  94. //
  95. // Do network call to gather information and put it in a nice array
  96. //
  97. //
  98. lmCode = NetServerEnum(
  99. NULL, // local server NT_PROBLEM
  100. 100, // level 100
  101. &bufptr, // data structure to return
  102. MAX_PREFERRED_LENGTH,
  103. &entriesread,
  104. &totalentries,
  105. SV_TYPE_SERVER,
  106. NULL,
  107. &resumehandle // resume handle
  108. );
  109. DataTable = (SERVER_INFO_100 *) bufptr ;
  110. if((NERR_Success == lmCode) || (ERROR_MORE_DATA == lmCode))
  111. {
  112. // valid so process it, otherwise error
  113. if(0 == MIB_DomServerTable.Len)
  114. {
  115. // 1st time, alloc the whole table
  116. // alloc the table space
  117. MIB_DomServerTable.Table = SnmpUtilMemAlloc(totalentries * sizeof(DOM_SERVER_ENTRY) );
  118. if (MIB_DomServerTable.Table == NULL)
  119. {
  120. // free all of the lan man data
  121. SafeBufferFree( bufptr ) ;
  122. // Signal error
  123. nResult = SNMPAPI_ERROR;
  124. goto Exit;
  125. }
  126. dwAllocatedEntries = totalentries;
  127. }
  128. MIB_DomServerTableElement = MIB_DomServerTable.Table + First_of_this_block ;
  129. for(i=0; (i<entriesread) && ((i+First_of_this_block) < dwAllocatedEntries); i++)
  130. {
  131. // once for each entry in the buffer
  132. // increment the entry number
  133. MIB_DomServerTable.Len ++;
  134. // Stuff the data into each item in the table
  135. MIB_DomServerTableElement->domServerName.dynamic = TRUE;
  136. #ifdef UNICODE
  137. if (SnmpUtilUnicodeToUTF8(
  138. &MIB_DomServerTableElement->domServerName.stream,
  139. DataTable->sv100_name,
  140. TRUE))
  141. {
  142. MIB_DomServerTableElement->domServerName.stream = NULL;
  143. MIB_DomServerTableElement->domServerName.length = 0;
  144. MIB_DomServerTableElement->domServerName.dynamic = FALSE;
  145. }
  146. else
  147. {
  148. MIB_DomServerTableElement->domServerName.length =
  149. strlen(MIB_DomServerTableElement->domServerName.stream);
  150. }
  151. #else
  152. MIB_DomServerTableElement->domServerName.stream = SnmpUtilMemAlloc(strlen( DataTable->sv100_name ) + 1 );
  153. MIB_DomServerTableElement->domServerName.length = strlen( DataTable->sv100_name ) ;
  154. // client name
  155. memcpy(
  156. MIB_DomServerTableElement->domServerName.stream,
  157. DataTable->sv100_name,
  158. strlen(DataTable->sv100_name)) ;
  159. #endif
  160. MIB_DomServerTableElement ++ ; // and table entry
  161. DataTable ++ ; // advance pointer to next sess entry in buffer
  162. } // for each entry in the data table
  163. // free all of the lan man data
  164. SafeBufferFree( bufptr ) ;
  165. // indicate where to start adding on next pass, if any
  166. First_of_this_block += i ;
  167. } // if data is valid to process
  168. else
  169. {
  170. // if ERROR_NO_BROWSER_SERVERS_FOUND we are not running in an NetBIOS environment
  171. // in this case we will have an empty table.
  172. nResult = (lmCode == ERROR_NO_BROWSER_SERVERS_FOUND) ? SNMPAPI_NOERROR : SNMPAPI_ERROR;
  173. goto Exit;
  174. }
  175. } while (ERROR_MORE_DATA == lmCode) ;
  176. // iterate over the table populating the Oid field
  177. if (! build_srvr_entry_oids())
  178. {
  179. SNMPDBG((
  180. SNMP_LOG_TRACE,
  181. "SNMP: LMMIB2: build_srvr_entry_oids failed\n."));
  182. FreeDomServerTable();
  183. cache_table[C_SRVR_TABLE].bufptr = NULL;
  184. nResult = SNMPAPI_ERROR;
  185. goto Exit;
  186. }
  187. // Sort the table information using MSC QuickSort routine
  188. qsort( &MIB_DomServerTable.Table[0], MIB_DomServerTable.Len,
  189. sizeof(DOM_SERVER_ENTRY), srvr_entry_cmp );
  190. //
  191. //
  192. // Cache table
  193. //
  194. //
  195. if(0 != MIB_DomServerTable.Len)
  196. {
  197. cache_table[C_SRVR_TABLE].acquisition_time = curr_time ;
  198. cache_table[C_SRVR_TABLE].bufptr = bufptr ;
  199. }
  200. //
  201. //
  202. // Return piece of information requested
  203. //
  204. //
  205. Exit:
  206. return nResult;
  207. } // MIB_srvr_get
  208. //
  209. // MIB_srvr_cmp
  210. // Routine for sorting the session table.
  211. //
  212. // Notes:
  213. //
  214. // Return Codes:
  215. // SNMPAPI_NOERROR
  216. // SNMPAPI_ERROR
  217. //
  218. // Error Codes:
  219. // None.
  220. //
  221. int __cdecl srvr_entry_cmp(
  222. IN const DOM_SERVER_ENTRY *A,
  223. IN const DOM_SERVER_ENTRY *B
  224. )
  225. {
  226. // Compare the OID's
  227. return SnmpUtilOidCmp( (AsnObjectIdentifier *)&A->Oid,
  228. (AsnObjectIdentifier *)&B->Oid );
  229. } // MIB_srvr_cmp
  230. //
  231. // None.
  232. //
  233. BOOL build_srvr_entry_oids(
  234. )
  235. {
  236. AsnOctetString OSA ;
  237. DOM_SERVER_ENTRY *DomServerEntry ;
  238. unsigned i;
  239. // start pointer at 1st guy in the table
  240. DomServerEntry = MIB_DomServerTable.Table ;
  241. // now iterate over the table, creating an oid for each entry
  242. for( i=0; i<MIB_DomServerTable.Len ; i++) {
  243. // for each entry in the session table
  244. OSA.stream = DomServerEntry->domServerName.stream ;
  245. OSA.length = DomServerEntry->domServerName.length ;
  246. OSA.dynamic = FALSE;
  247. // Make the entry's OID from string index
  248. if (! MakeOidFromStr( &OSA, &DomServerEntry->Oid ))
  249. {
  250. return FALSE;
  251. }
  252. DomServerEntry++; // point to the next guy in the table
  253. } // for
  254. return TRUE;
  255. } // build_srvr_entry_oids
  256. void FreeDomServerTable()
  257. {
  258. UINT i;
  259. DOM_SERVER_ENTRY *MIB_DomServerTableElement ;
  260. MIB_DomServerTableElement = MIB_DomServerTable.Table ;
  261. if (MIB_DomServerTableElement)
  262. {
  263. // iterate over the whole table
  264. for(i=0; i<MIB_DomServerTable.Len ;i++)
  265. {
  266. // free any alloc'ed elements of the structure
  267. SnmpUtilOidFree(&(MIB_DomServerTableElement->Oid));
  268. SnmpUtilMemFree(MIB_DomServerTableElement->domServerName.stream);
  269. MIB_DomServerTableElement ++ ; // increment table entry
  270. }
  271. SnmpUtilMemFree(MIB_DomServerTable.Table) ; // free the base Table
  272. }
  273. MIB_DomServerTable.Table = NULL ; // just for safety
  274. MIB_DomServerTable.Len = 0 ; // just for safety
  275. }
  276. //-------------------------------- END --------------------------------------