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.

476 lines
13 KiB

  1. /*++
  2. Copyright (c) 1992-1996 Microsoft Corporation
  3. Module Name:
  4. user_tbl.c
  5. Abstract:
  6. All routines to perform operations on the User Table.
  7. Environment:
  8. User Mode - Win32
  9. Revision History:
  10. 10-May-1996 DonRyan
  11. Removed banner from Technology Dynamics, Inc.
  12. --*/
  13. //--------------------------- WINDOWS DEPENDENCIES --------------------------
  14. //--------------------------- STANDARD DEPENDENCIES -- #include<xxxxx.h> ----
  15. #include <stdio.h>
  16. #include <memory.h>
  17. //--------------------------- MODULE DEPENDENCIES -- #include"xxxxx.h" ------
  18. #include <snmp.h>
  19. #include <snmputil.h>
  20. #include "mibfuncs.h"
  21. //--------------------------- SELF-DEPENDENCY -- ONE #include"module.h" -----
  22. #include "user_tbl.h"
  23. //--------------------------- PUBLIC VARIABLES --(same as in module.h file)--
  24. // Prefix to the Users table
  25. static UINT userSubids[] = { 2, 25, 1 };
  26. static AsnObjectIdentifier MIB_UserPrefix = { 3, userSubids };
  27. USER_TABLE MIB_UserTable = { 0, NULL };
  28. //--------------------------- PRIVATE CONSTANTS -----------------------------
  29. #define USER_FIELD_SUBID (MIB_UserPrefix.idLength+MIB_OidPrefix.idLength)
  30. #define USER_FIRST_FIELD USER_NAME_FIELD
  31. #define USER_LAST_FIELD USER_NAME_FIELD
  32. //--------------------------- PRIVATE STRUCTS -------------------------------
  33. //--------------------------- PRIVATE VARIABLES -----------------------------
  34. //--------------------------- PRIVATE PROTOTYPES ----------------------------
  35. UINT MIB_users_get(
  36. IN OUT RFC1157VarBind *VarBind
  37. );
  38. int MIB_users_match(
  39. IN AsnObjectIdentifier *Oid,
  40. OUT UINT *Pos
  41. );
  42. UINT MIB_users_copyfromtable(
  43. IN UINT Entry,
  44. IN UINT Field,
  45. OUT RFC1157VarBind *VarBind
  46. );
  47. //--------------------------- PRIVATE PROCEDURES ----------------------------
  48. //--------------------------- PUBLIC PROCEDURES -----------------------------
  49. //
  50. // MIB_users_func
  51. // High level routine for handling operations on the Users table
  52. //
  53. // Notes:
  54. //
  55. // Return Codes:
  56. // None.
  57. //
  58. // Error Codes:
  59. // None.
  60. //
  61. UINT MIB_users_func(
  62. IN UINT Action,
  63. IN MIB_ENTRY *MibPtr,
  64. IN OUT RFC1157VarBind *VarBind
  65. )
  66. {
  67. int Found;
  68. UINT Entry;
  69. UINT Field;
  70. UINT ErrStat;
  71. switch ( Action )
  72. {
  73. case MIB_ACTION_GETFIRST:
  74. // Fill the Users Table with the info from server
  75. if ( SNMPAPI_ERROR == MIB_users_lmget() )
  76. {
  77. ErrStat = SNMP_ERRORSTATUS_GENERR;
  78. goto Exit;
  79. }
  80. // If no elements in table, then return next MIB var, if one
  81. if ( MIB_UserTable.Len == 0 )
  82. {
  83. if ( MibPtr->MibNext == NULL )
  84. {
  85. ErrStat = SNMP_ERRORSTATUS_NOSUCHNAME;
  86. goto Exit;
  87. }
  88. // Do get first on the next MIB var
  89. ErrStat = (*MibPtr->MibNext->MibFunc)( Action, MibPtr->MibNext,
  90. VarBind );
  91. break;
  92. }
  93. //
  94. // Place correct OID in VarBind
  95. // Assuming the first field in the first record is the "start"
  96. {
  97. UINT temp_subs[] = { USER_FIRST_FIELD };
  98. AsnObjectIdentifier FieldOid = { 1, temp_subs };
  99. // prefix bug 445189
  100. AsnObjectIdentifier tmpOid;
  101. tmpOid = VarBind->name; // keep a copy (structure copy)
  102. if (SnmpUtilOidCpy( &VarBind->name, &MIB_OidPrefix ) == SNMPAPI_ERROR)
  103. {
  104. VarBind->name = tmpOid; // restore
  105. ErrStat = SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE;
  106. goto Exit;
  107. }
  108. if (SnmpUtilOidAppend( &VarBind->name, &MIB_UserPrefix ) == SNMPAPI_ERROR)
  109. {
  110. SnmpUtilOidFree(&VarBind->name);
  111. VarBind->name = tmpOid; // restore
  112. ErrStat = SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE;
  113. goto Exit;
  114. }
  115. if (SnmpUtilOidAppend( &VarBind->name, &FieldOid ) == SNMPAPI_ERROR)
  116. {
  117. SnmpUtilOidFree(&VarBind->name);
  118. VarBind->name = tmpOid; // restore
  119. ErrStat = SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE;
  120. goto Exit;
  121. }
  122. if (SnmpUtilOidAppend( &VarBind->name, &MIB_UserTable.Table[0].Oid ) == SNMPAPI_ERROR)
  123. {
  124. SnmpUtilOidFree(&VarBind->name);
  125. VarBind->name = tmpOid; // restore
  126. ErrStat = SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE;
  127. goto Exit;
  128. }
  129. // free the original VarBind->name
  130. SnmpUtilOidFree(&tmpOid);
  131. }
  132. //
  133. // Let fall through on purpose
  134. //
  135. case MIB_ACTION_GET:
  136. ErrStat = MIB_users_get( VarBind );
  137. break;
  138. case MIB_ACTION_GETNEXT:
  139. // Fill the Users table with the info from server
  140. if ( SNMPAPI_ERROR == MIB_users_lmget() )
  141. {
  142. ErrStat = SNMP_ERRORSTATUS_GENERR;
  143. goto Exit;
  144. }
  145. // Determine which field
  146. Field = VarBind->name.ids[USER_FIELD_SUBID];
  147. // Lookup OID in table
  148. if (Field < USER_FIRST_FIELD)
  149. {
  150. Entry = 0; // will take the first entry into the table
  151. Field = USER_FIRST_FIELD; // and the first column of the table
  152. Found = MIB_TBL_POS_BEFORE;
  153. }
  154. else if (Field > USER_LAST_FIELD)
  155. Found = MIB_TBL_POS_END;
  156. else
  157. Found = MIB_users_match( &VarBind->name, &Entry );
  158. // Index not found, but could be more fields to base GET on
  159. if ((Found == MIB_TBL_POS_BEFORE && MIB_UserTable.Len == 0) ||
  160. Found == MIB_TBL_POS_END )
  161. {
  162. // Index not found in table, get next from field
  163. // Field ++;
  164. // Make sure not past last field
  165. // if ( Field > USER_LAST_FIELD )
  166. // {
  167. // Get next VAR in MIB
  168. ErrStat = (*MibPtr->MibNext->MibFunc)( MIB_ACTION_GETFIRST,
  169. MibPtr->MibNext,
  170. VarBind );
  171. break;
  172. // }
  173. }
  174. // Get next TABLE entry
  175. if ( Found == MIB_TBL_POS_FOUND )
  176. {
  177. Entry ++;
  178. if ( Entry > MIB_UserTable.Len-1 )
  179. {
  180. Entry = 0;
  181. Field ++;
  182. if ( Field > USER_LAST_FIELD )
  183. {
  184. // Get next VAR in MIB
  185. ErrStat = (*MibPtr->MibNext->MibFunc)( MIB_ACTION_GETFIRST,
  186. MibPtr->MibNext,
  187. VarBind );
  188. break;
  189. }
  190. }
  191. }
  192. //
  193. // Place correct OID in VarBind
  194. // Assuming the first field in the first record is the "start"
  195. {
  196. UINT temp_subs[1];
  197. AsnObjectIdentifier FieldOid;
  198. AsnObjectIdentifier tmpOid; // Prefix bug 445189
  199. temp_subs[0] = Field;
  200. FieldOid.idLength = 1;
  201. FieldOid.ids = temp_subs;
  202. // Prefix bug 445189
  203. tmpOid = VarBind->name; // keep a copy (structure copy)
  204. if (SnmpUtilOidCpy( &VarBind->name, &MIB_OidPrefix ) == SNMPAPI_ERROR)
  205. {
  206. VarBind->name = tmpOid; // restore
  207. ErrStat = SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE;
  208. goto Exit;
  209. }
  210. if (SnmpUtilOidAppend( &VarBind->name, &MIB_UserPrefix ) == SNMPAPI_ERROR)
  211. {
  212. SnmpUtilOidFree(&VarBind->name);
  213. VarBind->name = tmpOid; // restore
  214. ErrStat = SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE;
  215. goto Exit;
  216. }
  217. if (SnmpUtilOidAppend( &VarBind->name, &FieldOid ) == SNMPAPI_ERROR)
  218. {
  219. SnmpUtilOidFree(&VarBind->name);
  220. VarBind->name = tmpOid; // restore
  221. ErrStat = SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE;
  222. goto Exit;
  223. }
  224. if (SnmpUtilOidAppend( &VarBind->name, &MIB_UserTable.Table[Entry].Oid ) == SNMPAPI_ERROR)
  225. {
  226. SnmpUtilOidFree(&VarBind->name);
  227. VarBind->name = tmpOid; // restore
  228. ErrStat = SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE;
  229. goto Exit;
  230. }
  231. // free the original VarBind->name
  232. SnmpUtilOidFree(&tmpOid);
  233. }
  234. ErrStat = MIB_users_copyfromtable( Entry, Field, VarBind );
  235. break;
  236. case MIB_ACTION_SET:
  237. ErrStat = SNMP_ERRORSTATUS_NOSUCHNAME;
  238. break;
  239. default:
  240. ErrStat = SNMP_ERRORSTATUS_GENERR;
  241. }
  242. Exit:
  243. return ErrStat;
  244. } // MIB_users_func
  245. //
  246. // MIB_users_get
  247. // Retrieve Users table information.
  248. //
  249. // Notes:
  250. //
  251. // Return Codes:
  252. // None.
  253. //
  254. // Error Codes:
  255. // None.
  256. //
  257. UINT MIB_users_get(
  258. IN OUT RFC1157VarBind *VarBind
  259. )
  260. {
  261. UINT Entry;
  262. int Found;
  263. UINT ErrStat;
  264. if (VarBind->name.ids[USER_FIELD_SUBID] < USER_FIRST_FIELD ||
  265. VarBind->name.ids[USER_FIELD_SUBID] > USER_LAST_FIELD)
  266. {
  267. ErrStat = SNMP_ERRORSTATUS_NOSUCHNAME;
  268. goto Exit;
  269. }
  270. // Fill the Users table with the info from server
  271. if ( SNMPAPI_ERROR == MIB_users_lmget() )
  272. {
  273. ErrStat = SNMP_ERRORSTATUS_GENERR;
  274. goto Exit;
  275. }
  276. Found = MIB_users_match( &VarBind->name, &Entry );
  277. // Look for a complete OID match
  278. if ( Found != MIB_TBL_POS_FOUND )
  279. {
  280. ErrStat = SNMP_ERRORSTATUS_NOSUCHNAME;
  281. goto Exit;
  282. }
  283. // Copy data from table
  284. ErrStat = MIB_users_copyfromtable( Entry,
  285. VarBind->name.ids[USER_FIELD_SUBID],
  286. VarBind );
  287. Exit:
  288. return ErrStat;
  289. } // MIB_users_get
  290. //
  291. // MIB_users_match
  292. // Match the target OID with a location in the Users table
  293. //
  294. // Notes:
  295. //
  296. // Return Codes:
  297. // None.
  298. //
  299. // Error Codes:
  300. // None
  301. //
  302. int MIB_users_match(
  303. IN AsnObjectIdentifier *Oid,
  304. OUT UINT *Pos
  305. )
  306. {
  307. AsnObjectIdentifier TempOid;
  308. int nResult;
  309. // Remove prefix including field reference
  310. TempOid.idLength = Oid->idLength - MIB_OidPrefix.idLength -
  311. MIB_UserPrefix.idLength - 1;
  312. TempOid.ids = &Oid->ids[MIB_OidPrefix.idLength+MIB_UserPrefix.idLength+1];
  313. *Pos = 0;
  314. while ( *Pos < MIB_UserTable.Len )
  315. {
  316. nResult = SnmpUtilOidCmp( &TempOid, &MIB_UserTable.Table[*Pos].Oid );
  317. if ( !nResult )
  318. {
  319. nResult = MIB_TBL_POS_FOUND;
  320. goto Exit;
  321. }
  322. if ( nResult < 0 )
  323. {
  324. nResult = MIB_TBL_POS_BEFORE;
  325. goto Exit;
  326. }
  327. (*Pos)++;
  328. }
  329. nResult = MIB_TBL_POS_END;
  330. Exit:
  331. return nResult;
  332. }
  333. //
  334. // MIB_users_copyfromtable
  335. // Copy requested data from table structure into Var Bind.
  336. //
  337. // Notes:
  338. //
  339. // Return Codes:
  340. // None.
  341. //
  342. // Error Codes:
  343. // None.
  344. //
  345. UINT MIB_users_copyfromtable(
  346. IN UINT Entry,
  347. IN UINT Field,
  348. OUT RFC1157VarBind *VarBind
  349. )
  350. {
  351. UINT ErrStat;
  352. // Get the requested field and save in var bind
  353. switch( Field )
  354. {
  355. case USER_NAME_FIELD:
  356. // Alloc space for string
  357. VarBind->value.asnValue.string.stream = SnmpUtilMemAlloc( sizeof(char)
  358. * MIB_UserTable.Table[Entry].svUserName.length );
  359. if ( VarBind->value.asnValue.string.stream == NULL )
  360. {
  361. ErrStat = SNMP_ERRORSTATUS_GENERR;
  362. goto Exit;
  363. }
  364. // Copy string into return position
  365. memcpy( VarBind->value.asnValue.string.stream,
  366. MIB_UserTable.Table[Entry].svUserName.stream,
  367. MIB_UserTable.Table[Entry].svUserName.length );
  368. // Set string length
  369. VarBind->value.asnValue.string.length =
  370. MIB_UserTable.Table[Entry].svUserName.length;
  371. VarBind->value.asnValue.string.dynamic = TRUE;
  372. // Set type of var bind
  373. VarBind->value.asnType = ASN_RFC1213_DISPSTRING;
  374. break;
  375. default:
  376. SNMPDBG(( SNMP_LOG_TRACE, "LMMIB2: Internal Error Users Table\n" ));
  377. ErrStat = SNMP_ERRORSTATUS_GENERR;
  378. goto Exit;
  379. }
  380. ErrStat = SNMP_ERRORSTATUS_NOERROR;
  381. Exit:
  382. return ErrStat;
  383. } // MIB_users_copyfromtable