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.

404 lines
12 KiB

  1. //depot/private/wmi_branch2/admin/wmi/wbem/Winmgmt/mofcompiler/mofcomp.cpp#7 - edit change 38018 (text)
  2. /*++
  3. Copyright (C) 1997-2001 Microsoft Corporation
  4. Module Name:
  5. MOFCOMP.CPP
  6. Abstract:
  7. Entry points for the WBEM MOF compiler.
  8. History:
  9. a-davj 12-April-97 Added WMI support.
  10. --*/
  11. #include "precomp.h"
  12. #include <stdio.h>
  13. #include <locale.h>
  14. #include <initguid.h>
  15. #include <wbemidl.h>
  16. #include <winver.h>
  17. #include <cominit.h>
  18. #include <wbemutil.h>
  19. #include <wbemcli.h>
  20. #include <mofcomp.h>
  21. #include <cominit.h>
  22. #include <objidl.h>
  23. #include "mofutils.h"
  24. #include "strings.h"
  25. #include <strutils.h>
  26. char cFileName[MAX_PATH];
  27. WCHAR wFileName[MAX_PATH+1];
  28. WCHAR wTempFileName[MAX_PATH];
  29. WCHAR wBMOFFileName[MAX_PATH];
  30. WCHAR wResourceName[MAX_PATH];
  31. int __cdecl main(int argc, char** argv)
  32. {
  33. BOOL bExtractResource = FALSE;
  34. BOOL bUseLocal = FALSE;
  35. WORD wLocaleId = 0;
  36. cFileName[0] = 0;
  37. wFileName[0] = 0;
  38. wTempFileName[0] = 0;
  39. wBMOFFileName[0]= 0;
  40. wResourceName[0] = 0;
  41. bool bBmofSet = false;
  42. // Set locale so that strings are correctly processed.
  43. // ===================================================
  44. setlocale(LC_ALL, "");
  45. HRESULT hres;
  46. SCODE sc;
  47. // Parse command line arguments
  48. // ============================
  49. WCHAR UserName[MAX_PATH];
  50. WCHAR Password[MAX_PATH];
  51. WCHAR * pPassword = NULL;
  52. WCHAR Authority[MAX_PATH];
  53. WCHAR wszDefault[MAX_PATH];
  54. TCHAR pVersion[100];
  55. BOOL bRet = GetVerInfo(TEXT("ProductVersion"), pVersion, 100);
  56. if(bRet)
  57. Trace(false, LONGVERSION, pVersion);
  58. else
  59. Trace(false, SHORTVERSION);
  60. Trace(false, COPYRIGHT);
  61. if(argc < 2)
  62. {
  63. PrintUsage();
  64. return 1;
  65. }
  66. // Set locale so that strings are correctly processed.
  67. // ===================================================
  68. setlocale(LC_ALL, "");
  69. // Init buffers for command line args.
  70. // ===================================
  71. UserName[0] = 0;
  72. Password[0] = 0;
  73. Authority[0] = 0;
  74. wszDefault[0] = 0;
  75. long lLoginFlags = 0;
  76. // This scope is defined so that the local variables, such as the PARSE
  77. // object are destroyed before CoUninitialize is called.
  78. char cBMOFOutputName[MAX_PATH] = "";
  79. // Parse command line arguments
  80. // ============================
  81. bool bClassFlagsHardcoded = false;
  82. bool bInstanceFlagsHardcoded = false;
  83. long lClassFlags = 0;
  84. long lInstanceFlags = 0;
  85. long lOptionFlags = WBEM_FLAG_CONSOLE_PRINT;
  86. char * pTemp;
  87. for(int i = 1; i < argc-1; i++)
  88. {
  89. char *pcCurrArg = argv[i] + 1;
  90. if(argv[i][0] != '-' && argv[i][0] != '/')
  91. {
  92. PrintUsage();
  93. return 1;
  94. }
  95. if(!wbem_stricmp(pcCurrArg, "check"))
  96. {
  97. lOptionFlags |= WBEM_FLAG_CHECK_ONLY;
  98. }
  99. else if(!wbem_stricmp(pcCurrArg, "AUTORECOVER"))
  100. {
  101. lOptionFlags |= WBEM_FLAG_AUTORECOVER;
  102. }
  103. else if(!wbem_strnicmp(pcCurrArg, "er:", 3))
  104. {
  105. if(strlen(pcCurrArg) <=3)
  106. {
  107. PrintUsage();
  108. return 1;
  109. }
  110. bExtractResource = true;
  111. mbstowcs(wResourceName, pcCurrArg+3, MAX_PATH);
  112. }
  113. else if(toupper(pcCurrArg[0]) == 'L' && pcCurrArg[1] == ':')
  114. {
  115. bUseLocal = TRUE;
  116. wLocaleId = (WORD)atoi(&pcCurrArg[2]);
  117. }
  118. else if(!wbem_stricmp(pcCurrArg, "WMI"))
  119. {
  120. HINSTANCE hLib = LoadLibraryEx(TEXT("wmimofck.exe"), NULL, LOAD_LIBRARY_AS_DATAFILE);
  121. if(hLib == NULL)
  122. {
  123. Trace(true, WMI_NOT_SETUP);
  124. return 1;
  125. }
  126. else FreeLibrary(hLib);
  127. lOptionFlags |= WBEM_FLAG_WMI_CHECK;
  128. }
  129. else if(!wbem_stricmp(pcCurrArg, "class:updateonly"))
  130. {
  131. lClassFlags |= WBEM_FLAG_UPDATE_ONLY;
  132. }
  133. else if(!wbem_stricmp(pcCurrArg, "class:createonly"))
  134. {
  135. lClassFlags |= WBEM_FLAG_CREATE_ONLY;
  136. }
  137. else if(!wbem_stricmp(pcCurrArg, "class:safeupdate"))
  138. {
  139. lClassFlags |= WBEM_FLAG_UPDATE_SAFE_MODE;
  140. }
  141. else if(!wbem_stricmp(pcCurrArg, "class:forceupdate"))
  142. {
  143. lClassFlags |= WBEM_FLAG_UPDATE_FORCE_MODE;
  144. }
  145. else if(!wbem_stricmp(pcCurrArg, "instance:updateonly"))
  146. {
  147. if(lInstanceFlags != 0)
  148. {
  149. PrintUsage();
  150. return 1;
  151. }
  152. lInstanceFlags = WBEM_FLAG_UPDATE_ONLY;
  153. }
  154. else if(!wbem_stricmp(pcCurrArg, "instance:createonly"))
  155. {
  156. if(lInstanceFlags != 0)
  157. {
  158. PrintUsage();
  159. return 1;
  160. }
  161. lInstanceFlags = WBEM_FLAG_CREATE_ONLY;
  162. }
  163. else if(!wbem_strnicmp(pcCurrArg, "Amendment:", 10))
  164. {
  165. if(strlen(pcCurrArg) <=10)
  166. {
  167. PrintUsage();
  168. return 1;
  169. }
  170. strcat(cBMOFOutputName, ",a");
  171. strcat(cBMOFOutputName, pcCurrArg+10);
  172. mbstowcs(wBMOFFileName, cBMOFOutputName, MAX_PATH);
  173. lOptionFlags |= WBEM_FLAG_SPLIT_FILES;
  174. }
  175. else if(!wbem_strnicmp(pcCurrArg, "mof:", 4))
  176. {
  177. if(strlen(pcCurrArg) <=4)
  178. {
  179. PrintUsage();
  180. return 1;
  181. }
  182. strcat(cBMOFOutputName, ",n");
  183. strcat(cBMOFOutputName, pcCurrArg+4);
  184. mbstowcs(wBMOFFileName, cBMOFOutputName, MAX_PATH);
  185. lOptionFlags |= WBEM_FLAG_SPLIT_FILES;
  186. }
  187. else if(!wbem_strnicmp(pcCurrArg, "mfl:", 4))
  188. {
  189. if(strlen(pcCurrArg) <=4)
  190. {
  191. PrintUsage();
  192. return 1;
  193. }
  194. strcat(cBMOFOutputName, ",l");
  195. strcat(cBMOFOutputName, pcCurrArg+4);
  196. mbstowcs(wBMOFFileName, cBMOFOutputName, MAX_PATH);
  197. lOptionFlags |= WBEM_FLAG_SPLIT_FILES;
  198. }
  199. else if(toupper(pcCurrArg[0]) == 'C' && pcCurrArg[1] == ':')
  200. {
  201. if(lClassFlags != 0)
  202. {
  203. PrintUsage();
  204. return 1;
  205. }
  206. bClassFlagsHardcoded = true;
  207. lClassFlags = atol(&pcCurrArg[2]);
  208. }
  209. else if(toupper(pcCurrArg[0]) == 'I' && pcCurrArg[1] == ':')
  210. {
  211. if(lInstanceFlags != 0)
  212. {
  213. PrintUsage();
  214. return 1;
  215. }
  216. bInstanceFlagsHardcoded = true;
  217. lInstanceFlags = atol(&pcCurrArg[2]);
  218. }
  219. else if(toupper(*pcCurrArg) == 'N')
  220. {
  221. if(!bGetString(argv[i]+2, wszDefault))
  222. return 1;
  223. }
  224. else if(toupper(*pcCurrArg) == 'B')
  225. {
  226. if(strlen(argv[i]) <=3 || argv[i][2] != ':' || wcslen(wBMOFFileName))
  227. {
  228. PrintUsage();
  229. return 1;
  230. }
  231. strcpy(cBMOFOutputName, argv[i]+3);
  232. mbstowcs(wBMOFFileName, cBMOFOutputName, MAX_PATH);
  233. bBmofSet = true;
  234. }
  235. else if(toupper(*pcCurrArg) == 'U')
  236. {
  237. if(!bGetString(argv[i]+2, UserName))
  238. return 1;
  239. }
  240. else if(toupper(*pcCurrArg) == 'P')
  241. {
  242. // Allow for blank password
  243. char * pArg = argv[i];
  244. if(pArg[2] != ':' || pArg[3] != 0) // Dont use bGetString for empty password case
  245. if(!bGetString(argv[i]+2, Password))
  246. return 1;
  247. pPassword = Password;
  248. }
  249. else if(toupper(*pcCurrArg) == 'A')
  250. {
  251. if(!bGetString(argv[i]+2, Authority))
  252. return 1;
  253. }
  254. else
  255. {
  256. PrintUsage();
  257. return 1;
  258. }
  259. }
  260. // Do a sanity check of the flags chosen
  261. if((bClassFlagsHardcoded == false && !ValidFlags(true, lClassFlags)) ||
  262. (bInstanceFlagsHardcoded == false && !ValidFlags(false, lInstanceFlags)))
  263. {
  264. PrintUsage();
  265. return 1;
  266. }
  267. if((lOptionFlags & WBEM_FLAG_WMI_CHECK) && strlen(cBMOFOutputName) < 1)
  268. {
  269. Trace(true, WMI_ARG_ERROR);
  270. return 1;
  271. }
  272. pTemp = argv[argc-1];
  273. if((pTemp[0] == '-' || pTemp[0] == '/') && pTemp[1] == '?' && pTemp[2] == 0)
  274. {
  275. PrintUsage();
  276. return 1;
  277. }
  278. if((lOptionFlags & WBEM_FLAG_SPLIT_FILES) && bBmofSet)
  279. {
  280. PrintUsage();
  281. return 1;
  282. }
  283. // display the file name and make sure it is a valid file
  284. strcpy(cFileName, argv[argc-1]);
  285. mbstowcs(wTempFileName, argv[argc-1], MAX_PATH);
  286. DWORD nRes = ExpandEnvironmentStrings(wTempFileName,
  287. wFileName,
  288. FILENAME_MAX);
  289. if(nRes == 0)
  290. lstrcpy(wFileName, wTempFileName);
  291. if(GetFileAttributesW(wFileName) == 0xFFFFFFFF)
  292. {
  293. Trace(true, FILE_NOT_FOUND, wFileName);
  294. return 1;
  295. }
  296. hres = InitializeCom();
  297. if(FAILED(hres))
  298. {
  299. Trace(true, COMINIT_ERROR, hres);
  300. return 3;
  301. }
  302. WBEM_COMPILE_STATUS_INFO info;
  303. hres = InitializeSecurity(NULL, -1, NULL, NULL,
  304. RPC_C_AUTHN_LEVEL_DEFAULT,
  305. RPC_C_IMP_LEVEL_IDENTIFY,
  306. NULL, EOAC_NONE, 0);
  307. IMofCompiler * pCompiler = NULL;
  308. sc = CoCreateInstance(CLSID_MofCompiler, 0, CLSCTX_INPROC_SERVER,
  309. IID_IMofCompiler, (LPVOID *) &pCompiler);
  310. // special fix that will self register the mofd.dll if it isnt already registered
  311. if(sc != S_OK)
  312. {
  313. HRESULT (STDAPICALLTYPE *pFunc)(void);
  314. HINSTANCE hLib = LoadLibrary(TEXT("mofd.dll"));
  315. if(hLib)
  316. {
  317. pFunc = (HRESULT (STDAPICALLTYPE *)(void))GetProcAddress(hLib, "DllRegisterServer");
  318. if(pFunc)
  319. {
  320. pFunc();
  321. sc = CoCreateInstance(CLSID_MofCompiler, 0, CLSCTX_INPROC_SERVER,
  322. IID_IMofCompiler, (LPVOID *) &pCompiler);
  323. }
  324. FreeLibrary(hLib);
  325. }
  326. }
  327. if(sc == S_OK)
  328. {
  329. if(strlen(cBMOFOutputName) > 0)
  330. sc = pCompiler->CreateBMOF( wFileName, wBMOFFileName, wszDefault,
  331. lOptionFlags, lClassFlags,
  332. lInstanceFlags, &info);
  333. else if (bExtractResource)
  334. sc = ExtractFromResource(pCompiler, wResourceName, wFileName, wszDefault,
  335. UserName, Authority, pPassword, lOptionFlags, lClassFlags,
  336. lInstanceFlags, &info,
  337. bUseLocal,
  338. wLocaleId);
  339. else
  340. sc = pCompiler->CompileFile(wFileName, wszDefault, UserName,
  341. Authority, pPassword, lOptionFlags, lClassFlags,
  342. lInstanceFlags, &info);
  343. if(sc != S_OK)
  344. Trace(true, COMPILER_ERROR, info.hRes);
  345. pCompiler->Release();
  346. }
  347. else
  348. {
  349. Trace(true, CCI_ERROR, sc);
  350. return 3;
  351. }
  352. CoUninitialize();
  353. if(sc == S_OK)
  354. {
  355. Trace(true, DONE);
  356. return 0;
  357. }
  358. else
  359. return info.lPhaseError;
  360. }