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.

180 lines
4.4 KiB

  1. /*
  2. File: LoadSave.c
  3. Handles routemon options to load and save router configuration.
  4. */
  5. #include "precomp.h"
  6. #define LOADSAVE_PATH_SIZE 512
  7. //
  8. // Defines a macro to perform string copies to
  9. // unicode strings regardless of the UNICODE setting.
  10. //
  11. #if defined( UNICODE ) || defined( _UNICODE )
  12. #define LoadSaveStrcpy(dst, src) wcscpy((dst), (src));
  13. #else
  14. #define LoadSaveStrcpy(dst, src) mbstowcs((dst), (src), strlen((src)));
  15. #endif
  16. //
  17. // Defines structure of parameters that can be sent to
  18. // a load/save config call.
  19. //
  20. typedef struct _LOADSAVE_PARAMS {
  21. WCHAR pszPath[LOADSAVE_PATH_SIZE];
  22. } LOADSAVE_PARAMS, * PLOADSAVE_PARAMS;
  23. //
  24. // Returns a static error message
  25. //
  26. PWCHAR LoadSaveError (DWORD dwErr) {
  27. static WCHAR pszRet[512];
  28. ZeroMemory(pszRet, sizeof(pszRet));
  29. FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM,
  30. NULL,
  31. dwErr,
  32. 0,
  33. pszRet,
  34. sizeof(pszRet) / sizeof(WCHAR),
  35. NULL);
  36. return pszRet;
  37. }
  38. //
  39. // Parse the load save config command line and fills
  40. // the parameters accordingly.
  41. //
  42. DWORD LoadSaveParse (
  43. IN int argc,
  44. IN TCHAR *argv[],
  45. IN PROUTEMON_PARAMS pRmParams,
  46. IN PROUTEMON_UTILS pUtils,
  47. IN BOOL bLoad,
  48. OUT LOADSAVE_PARAMS * pParams)
  49. {
  50. DWORD dwLen;
  51. // Initialize the return val
  52. ZeroMemory(pParams, sizeof(LOADSAVE_PARAMS));
  53. // Make sure a path has been provided
  54. if (argc == 0) {
  55. pUtils->put_msg (GetModuleHandle(NULL),
  56. MSG_LOADSAVE_HELP,
  57. pRmParams->pszProgramName);
  58. return ERROR_CAN_NOT_COMPLETE;
  59. }
  60. // Copy over the path
  61. LoadSaveStrcpy (pParams->pszPath, argv[0]);
  62. // Add a '\' to the end of the path if not provided
  63. // dwLen = wcslen (pParams->pszPath);
  64. // if (pParams->pszPath[dwLen - 1] != L'\\') {
  65. // pParams->pszPath[dwLen] = L'\\';
  66. // pParams->pszPath[dwLen + 1] = (WCHAR)0;
  67. // }
  68. return NO_ERROR;
  69. }
  70. //
  71. // The load/save engine
  72. //
  73. DWORD LoadSaveConfig (
  74. IN PROUTEMON_PARAMS pRmParams,
  75. IN PROUTEMON_UTILS pUtils,
  76. IN PLOADSAVE_PARAMS pLsParams,
  77. IN BOOL bLoad)
  78. {
  79. DWORD dwErr;
  80. if (bLoad)
  81. dwErr = MprConfigServerRestore (pRmParams->hRouterConfig,
  82. pLsParams->pszPath);
  83. else
  84. dwErr = MprConfigServerBackup (pRmParams->hRouterConfig,
  85. pLsParams->pszPath);
  86. return dwErr;
  87. }
  88. //
  89. // Handles request to load config
  90. //
  91. DWORD APIENTRY
  92. LoadMonitor (
  93. IN int argc,
  94. IN TCHAR *argv[],
  95. IN PROUTEMON_PARAMS params,
  96. IN PROUTEMON_UTILS utils
  97. )
  98. {
  99. DWORD dwErr;
  100. LOADSAVE_PARAMS LsParams;
  101. HINSTANCE hInst = GetModuleHandle(NULL);
  102. if ((dwErr = LoadSaveParse (argc, argv, params,
  103. utils, TRUE, &LsParams)) != NO_ERROR)
  104. return dwErr;
  105. dwErr = LoadSaveConfig (params, utils, &LsParams, TRUE);
  106. switch (dwErr) {
  107. case NO_ERROR:
  108. utils->put_msg(hInst, MSG_LOAD_SUCCESS, LsParams.pszPath);
  109. break;
  110. case ERROR_ROUTER_CONFIG_INCOMPATIBLE:
  111. utils->put_msg(hInst, MSG_LOAD_INCOMPATIBLE, LsParams.pszPath);
  112. break;
  113. case ERROR_ACCESS_DENIED:
  114. utils->put_msg(hInst, MSG_LOAD_FAIL_ACCESSDENIED);
  115. default:
  116. utils->put_msg(
  117. hInst,
  118. MSG_LOAD_FAIL,
  119. LsParams.pszPath,
  120. LoadSaveError(dwErr));
  121. break;
  122. }
  123. return dwErr;
  124. }
  125. //
  126. // Handles request to save config
  127. //
  128. DWORD APIENTRY
  129. SaveMonitor (
  130. IN int argc,
  131. IN TCHAR *argv[],
  132. IN PROUTEMON_PARAMS params,
  133. IN PROUTEMON_UTILS utils
  134. )
  135. {
  136. DWORD dwErr;
  137. LOADSAVE_PARAMS LsParams;
  138. if ((dwErr = LoadSaveParse (argc, argv, params,
  139. utils, FALSE, &LsParams)) != NO_ERROR)
  140. return dwErr;
  141. dwErr = LoadSaveConfig (params, utils, &LsParams, FALSE);
  142. if (dwErr == NO_ERROR)
  143. utils->put_msg(GetModuleHandle(NULL), MSG_SAVE_SUCCESS, LsParams.pszPath);
  144. else
  145. utils->put_msg(GetModuleHandle(NULL), MSG_SAVE_FAIL, LsParams.pszPath, LoadSaveError(dwErr));
  146. return dwErr;
  147. }