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.

356 lines
9.0 KiB

  1. /*++
  2. Copyright (c) 1992-1996 Microsoft Corporation
  3. Module Name:
  4. user_lm.c
  5. Abstract:
  6. This file contains the routines which actually call Lan Manager and
  7. retrieve the contents of the user 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 <string.h>
  21. #include <search.h>
  22. #include <stdlib.h>
  23. #include <time.h>
  24. //--------------------------- MODULE DEPENDENCIES -- #include"xxxxx.h" ------
  25. #include "mib.h"
  26. #include "mibfuncs.h"
  27. #include "user_tbl.h"
  28. #include "lmcache.h"
  29. //--------------------------- SELF-DEPENDENCY -- ONE #include"module.h" -----
  30. //--------------------------- PUBLIC VARIABLES --(same as in module.h file)--
  31. //--------------------------- PRIVATE CONSTANTS -----------------------------
  32. #define SafeBufferFree(x) if(NULL != x) NetApiBufferFree( x )
  33. #define SafeFree(x) if(NULL != x) SnmpUtilMemFree( x )
  34. //--------------------------- PRIVATE STRUCTS -------------------------------
  35. //--------------------------- PRIVATE VARIABLES -----------------------------
  36. //--------------------------- PRIVATE PROTOTYPES ----------------------------
  37. //--------------------------- PRIVATE PROCEDURES ----------------------------
  38. int __cdecl user_entry_cmp(
  39. IN const USER_ENTRY *A,
  40. IN const USER_ENTRY *B
  41. ) ;
  42. BOOL build_user_entry_oids( );
  43. void FreeUserTable();
  44. //--------------------------- PUBLIC PROCEDURES -----------------------------
  45. //
  46. // MIB_user_lmget
  47. // Retrieve print queue table information from Lan Manager.
  48. // If not cached, sort it and then
  49. // cache it.
  50. //
  51. // Notes:
  52. //
  53. // Return Codes:
  54. // SNMPAPI_NOERROR
  55. // SNMPAPI_ERROR
  56. //
  57. // Error Codes:
  58. // None.
  59. //
  60. SNMPAPI MIB_users_lmget(
  61. )
  62. {
  63. DWORD entriesread;
  64. DWORD totalentries;
  65. LPBYTE bufptr;
  66. unsigned lmCode;
  67. unsigned i;
  68. USER_INFO_0 *DataTable;
  69. USER_ENTRY *MIB_UserTableElement ;
  70. int First_of_this_block;
  71. LPSTR ansi_string;
  72. time_t curr_time ;
  73. SNMPAPI nResult = SNMPAPI_NOERROR;
  74. DWORD resumehandle=0;
  75. DWORD dwAllocatedEntries=0;
  76. time(&curr_time); // get the time
  77. //
  78. //
  79. // If cached, return piece of info.
  80. //
  81. //
  82. if((NULL != cache_table[C_USER_TABLE].bufptr) &&
  83. (curr_time <
  84. (cache_table[C_USER_TABLE].acquisition_time
  85. + cache_expire[C_USER_TABLE] ) ) )
  86. { // it has NOT expired!
  87. goto Exit ; // the global table is valid
  88. }
  89. //
  90. //
  91. // Do network call to gather information and put it in a nice array
  92. //
  93. //
  94. //
  95. // remember to free the existing data
  96. //
  97. FreeUserTable();
  98. First_of_this_block = 0;
  99. do { // as long as there is more data to process
  100. lmCode =
  101. NetUserEnum( NULL, // local server
  102. 0, // level 0, no admin priv.
  103. FILTER_NORMAL_ACCOUNT,
  104. &bufptr, // data structure to return
  105. MAX_PREFERRED_LENGTH,
  106. &entriesread,
  107. &totalentries,
  108. &resumehandle // resume handle
  109. );
  110. DataTable = (USER_INFO_0 *) bufptr ;
  111. if((NERR_Success == lmCode) || (ERROR_MORE_DATA == lmCode))
  112. { // valid so process it, otherwise error
  113. if(0 == MIB_UserTable.Len) { // 1st time, alloc the whole table
  114. // alloc the table space
  115. MIB_UserTable.Table = SnmpUtilMemAlloc(totalentries *
  116. sizeof(USER_ENTRY) );
  117. // prefix bugs 445183 and 445184
  118. if (MIB_UserTable.Table == NULL) {
  119. // free all of the lan man data
  120. SafeBufferFree( bufptr ) ;
  121. // Signal error
  122. nResult = SNMPAPI_ERROR;
  123. goto Exit;
  124. }
  125. dwAllocatedEntries = totalentries;
  126. }
  127. MIB_UserTableElement = MIB_UserTable.Table + First_of_this_block ;
  128. for(i=0; (i<entriesread) && ((i+First_of_this_block) < dwAllocatedEntries); i++) { // once for each entry in the buffer
  129. // increment the entry number
  130. MIB_UserTable.Len ++;
  131. // Stuff the data into each item in the table
  132. // convert the undocumented unicode to something readable
  133. if (SnmpUtilUnicodeToUTF8(
  134. &ansi_string,
  135. DataTable->usri0_name,
  136. TRUE )) // auto alloc the space for ansi
  137. {
  138. MIB_UserTableElement->svUserName.stream = NULL;
  139. MIB_UserTableElement->svUserName.length = 0;
  140. MIB_UserTableElement->svUserName.dynamic = FALSE;
  141. }
  142. else
  143. {
  144. // client name
  145. MIB_UserTableElement->svUserName.stream = ansi_string;
  146. MIB_UserTableElement->svUserName.length = strlen( ansi_string ) ;
  147. MIB_UserTableElement->svUserName.dynamic = TRUE;
  148. }
  149. ansi_string = NULL;
  150. MIB_UserTableElement ++ ; // and table entry
  151. DataTable ++ ; // advance pointer to next sess entry in buffer
  152. } // for each entry in the data table
  153. // free all of the lan man data
  154. SafeBufferFree( bufptr ) ;
  155. // indicate where to start adding on next pass, if any
  156. First_of_this_block += i ;
  157. } // if data is valid to process
  158. else
  159. {
  160. // Signal error
  161. nResult = SNMPAPI_ERROR;
  162. goto Exit;
  163. }
  164. } while (ERROR_MORE_DATA == lmCode) ;
  165. // iterate over the table populating the Oid field
  166. if (! build_user_entry_oids())
  167. {
  168. SNMPDBG((
  169. SNMP_LOG_TRACE,
  170. "SNMP: LMMIB2: build_user_entry_oids failed\n."));
  171. FreeUserTable();
  172. cache_table[C_USER_TABLE].bufptr = NULL;
  173. nResult = SNMPAPI_ERROR;
  174. goto Exit;
  175. }
  176. // Sort the table information using MSC QuickSort routine
  177. qsort( &MIB_UserTable.Table[0], MIB_UserTable.Len,
  178. sizeof(USER_ENTRY), user_entry_cmp );
  179. //
  180. //
  181. // Cache table
  182. //
  183. //
  184. if(0 != MIB_UserTable.Len) {
  185. cache_table[C_USER_TABLE].acquisition_time = curr_time ;
  186. cache_table[C_USER_TABLE].bufptr = bufptr ;
  187. }
  188. //
  189. //
  190. // Return piece of information requested
  191. //
  192. //
  193. Exit:
  194. return nResult;
  195. } // MIB_user_get
  196. //
  197. // MIB_user_cmp
  198. // Routine for sorting the session table.
  199. //
  200. // Notes:
  201. //
  202. // Return Codes:
  203. // SNMPAPI_NOERROR
  204. // SNMPAPI_ERROR
  205. //
  206. // Error Codes:
  207. // None.
  208. //
  209. int __cdecl user_entry_cmp(
  210. IN const USER_ENTRY *A,
  211. IN const USER_ENTRY *B
  212. )
  213. {
  214. // Compare the OID's
  215. return SnmpUtilOidCmp( (AsnObjectIdentifier *)&A->Oid,
  216. (AsnObjectIdentifier *)&B->Oid );
  217. } // MIB_user_cmp
  218. //
  219. // None.
  220. //
  221. BOOL build_user_entry_oids(
  222. )
  223. {
  224. AsnOctetString OSA ;
  225. USER_ENTRY *UserEntry ;
  226. unsigned i;
  227. // start pointer at 1st guy in the table
  228. UserEntry = MIB_UserTable.Table ;
  229. // now iterate over the table, creating an oid for each entry
  230. for( i=0; i<MIB_UserTable.Len ; i++) {
  231. // for each entry in the session table
  232. OSA.stream = UserEntry->svUserName.stream ;
  233. OSA.length = UserEntry->svUserName.length ;
  234. OSA.dynamic = FALSE;
  235. // Make the entry's OID from string index
  236. if (! MakeOidFromStr( &OSA, &UserEntry->Oid ))
  237. {
  238. return FALSE;
  239. }
  240. UserEntry++; // point to the next guy in the table
  241. } // for
  242. return TRUE;
  243. } // build_user_entry_oids
  244. void FreeUserTable()
  245. {
  246. UINT i;
  247. USER_ENTRY *MIB_UserTableElement ;
  248. MIB_UserTableElement = MIB_UserTable.Table ;
  249. if (MIB_UserTableElement)
  250. {
  251. // iterate over the whole table
  252. for(i=0; i<MIB_UserTable.Len ;i++)
  253. {
  254. // free any alloc'ed elements of the structure
  255. SnmpUtilOidFree(&(MIB_UserTableElement->Oid));
  256. SnmpUtilMemFree(MIB_UserTableElement->svUserName.stream);
  257. MIB_UserTableElement ++ ; // increment table entry
  258. }
  259. SnmpUtilMemFree(MIB_UserTable.Table) ; // free the base Table
  260. }
  261. MIB_UserTable.Table = NULL ; // just for safety
  262. MIB_UserTable.Len = 0 ; // just for safety
  263. }
  264. //-------------------------------- END --------------------------------------