Source code of Windows XP (NT5)
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.

516 lines
14 KiB

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