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.

337 lines
9.2 KiB

  1. /*++
  2. Copyright (C) 1997-2001 Microsoft Corporation
  3. Module Name:
  4. MOFCOMP.CPP
  5. Abstract:
  6. Entry points for the WBEM MOF compiler.
  7. History:
  8. a-davj 12-April-97 Added WMI support.
  9. --*/
  10. #include "precomp.h"
  11. #include <stdio.h>
  12. #include <locale.h>
  13. #include <initguid.h>
  14. #include <wbemidl.h>
  15. #include <winver.h>
  16. #include <cominit.h>
  17. #include <wbemutil.h>
  18. #include <wbemcli.h>
  19. #include <mofcomp.h>
  20. #include <cominit.h>
  21. #include <objidl.h>
  22. #include <dllcalls.h>
  23. #include "mofutils.h"
  24. #include "strings.h"
  25. char cFileName[MAX_PATH];
  26. WCHAR wFileName[MAX_PATH];
  27. WCHAR wBMOFFileName[MAX_PATH];
  28. int __cdecl main(int argc, char** argv)
  29. {
  30. cFileName[0] = 0;
  31. wFileName[0] = 0;
  32. wBMOFFileName[0]= 0;
  33. bool bBmofSet = false;
  34. // Set locale so that strings are correctly processed.
  35. // ===================================================
  36. setlocale(LC_ALL, "");
  37. HRESULT hres;
  38. SCODE sc;
  39. // Parse command line arguments
  40. // ============================
  41. WCHAR UserName[MAX_PATH];
  42. WCHAR Password[MAX_PATH];
  43. WCHAR * pPassword = NULL;
  44. WCHAR Authority[MAX_PATH];
  45. WCHAR wszDefault[MAX_PATH];
  46. TCHAR pVersion[100];
  47. BOOL bRet = GetVerInfo(TEXT("ProductVersion"), pVersion, 100);
  48. if(bRet)
  49. Trace(false, LONGVERSION, pVersion);
  50. else
  51. Trace(false, SHORTVERSION);
  52. Trace(false, COPYRIGHT);
  53. if(argc < 2)
  54. {
  55. PrintUsage();
  56. return 1;
  57. }
  58. // Set locale so that strings are correctly processed.
  59. // ===================================================
  60. setlocale(LC_ALL, "");
  61. // Init buffers for command line args.
  62. // ===================================
  63. UserName[0] = 0;
  64. Password[0] = 0;
  65. Authority[0] = 0;
  66. wszDefault[0] = 0;
  67. long lLoginFlags = 0;
  68. // This scope is defined so that the local variables, such as the PARSE
  69. // object are destroyed before CoUninitialize is called.
  70. char cBMOFOutputName[MAX_PATH] = "";
  71. // Parse command line arguments
  72. // ============================
  73. bool bClassFlagsHardcoded = false;
  74. bool bInstanceFlagsHardcoded = false;
  75. long lClassFlags = 0;
  76. long lInstanceFlags = 0;
  77. long lOptionFlags = WBEM_FLAG_CONSOLE_PRINT;
  78. for(int i = 1; i < argc-1; i++)
  79. {
  80. char *pcCurrArg = argv[i] + 1;
  81. if(argv[i][0] != '-' && argv[i][0] != '/')
  82. {
  83. PrintUsage();
  84. return 1;
  85. }
  86. if(!wbem_stricmp(pcCurrArg, "check"))
  87. {
  88. lOptionFlags |= WBEM_FLAG_CHECK_ONLY;
  89. }
  90. else if(!wbem_stricmp(pcCurrArg, "AUTORECOVER"))
  91. {
  92. lOptionFlags |= WBEM_FLAG_AUTORECOVER;
  93. }
  94. else if(!wbem_stricmp(pcCurrArg, "WMI"))
  95. {
  96. HINSTANCE hLib = LoadLibraryEx(TEXT("wmimofck.exe"), NULL, LOAD_LIBRARY_AS_DATAFILE);
  97. if(hLib == NULL)
  98. {
  99. Trace(true, WMI_NOT_SETUP);
  100. return 1;
  101. }
  102. else FreeLibrary(hLib);
  103. lOptionFlags |= WBEM_FLAG_WMI_CHECK;
  104. }
  105. else if(!wbem_stricmp(pcCurrArg, "class:updateonly"))
  106. {
  107. lClassFlags |= WBEM_FLAG_UPDATE_ONLY;
  108. }
  109. else if(!wbem_stricmp(pcCurrArg, "class:createonly"))
  110. {
  111. lClassFlags |= WBEM_FLAG_CREATE_ONLY;
  112. }
  113. else if(!wbem_stricmp(pcCurrArg, "class:safeupdate"))
  114. {
  115. lClassFlags |= WBEM_FLAG_UPDATE_SAFE_MODE;
  116. }
  117. else if(!wbem_stricmp(pcCurrArg, "class:forceupdate"))
  118. {
  119. lClassFlags |= WBEM_FLAG_UPDATE_FORCE_MODE;
  120. }
  121. else if(!wbem_stricmp(pcCurrArg, "instance:updateonly"))
  122. {
  123. if(lInstanceFlags != 0)
  124. {
  125. PrintUsage();
  126. return 1;
  127. }
  128. lInstanceFlags = WBEM_FLAG_UPDATE_ONLY;
  129. }
  130. else if(!wbem_stricmp(pcCurrArg, "instance:createonly"))
  131. {
  132. if(lInstanceFlags != 0)
  133. {
  134. PrintUsage();
  135. return 1;
  136. }
  137. lInstanceFlags = WBEM_FLAG_CREATE_ONLY;
  138. }
  139. else if(!wbem_strnicmp(pcCurrArg, "Amendment:", 10))
  140. {
  141. if(strlen(pcCurrArg) <=10)
  142. {
  143. PrintUsage();
  144. return 1;
  145. }
  146. strcat(cBMOFOutputName, ",a");
  147. strcat(cBMOFOutputName, pcCurrArg+10);
  148. mbstowcs(wBMOFFileName, cBMOFOutputName, MAX_PATH);
  149. lOptionFlags |= WBEM_FLAG_SPLIT_FILES;
  150. }
  151. else if(!wbem_strnicmp(pcCurrArg, "mof:", 4))
  152. {
  153. if(strlen(pcCurrArg) <=4)
  154. {
  155. PrintUsage();
  156. return 1;
  157. }
  158. strcat(cBMOFOutputName, ",n");
  159. strcat(cBMOFOutputName, pcCurrArg+4);
  160. mbstowcs(wBMOFFileName, cBMOFOutputName, MAX_PATH);
  161. lOptionFlags |= WBEM_FLAG_SPLIT_FILES;
  162. }
  163. else if(!wbem_strnicmp(pcCurrArg, "mfl:", 4))
  164. {
  165. if(strlen(pcCurrArg) <=4)
  166. {
  167. PrintUsage();
  168. return 1;
  169. }
  170. strcat(cBMOFOutputName, ",l");
  171. strcat(cBMOFOutputName, pcCurrArg+4);
  172. mbstowcs(wBMOFFileName, cBMOFOutputName, MAX_PATH);
  173. lOptionFlags |= WBEM_FLAG_SPLIT_FILES;
  174. }
  175. else if(toupper(pcCurrArg[0]) == 'C' && pcCurrArg[1] == ':')
  176. {
  177. if(lClassFlags != 0)
  178. {
  179. PrintUsage();
  180. return 1;
  181. }
  182. bClassFlagsHardcoded = true;
  183. lClassFlags = atol(&pcCurrArg[2]);
  184. }
  185. else if(toupper(pcCurrArg[0]) == 'I' && pcCurrArg[1] == ':')
  186. {
  187. if(lInstanceFlags != 0)
  188. {
  189. PrintUsage();
  190. return 1;
  191. }
  192. bInstanceFlagsHardcoded = true;
  193. lInstanceFlags = atol(&pcCurrArg[2]);
  194. }
  195. else if(toupper(*pcCurrArg) == 'N')
  196. {
  197. if(!bGetString(argv[i]+2, wszDefault))
  198. return 1;
  199. }
  200. else if(toupper(*pcCurrArg) == 'B')
  201. {
  202. if(strlen(argv[i]) <=3 || argv[i][2] != ':' || wcslen(wBMOFFileName))
  203. {
  204. PrintUsage();
  205. return 1;
  206. }
  207. strcpy(cBMOFOutputName, argv[i]+3);
  208. mbstowcs(wBMOFFileName, cBMOFOutputName, MAX_PATH);
  209. bBmofSet = true;
  210. }
  211. else if(toupper(*pcCurrArg) == 'U')
  212. {
  213. if(!bGetString(argv[i]+2, UserName))
  214. return 1;
  215. }
  216. else if(toupper(*pcCurrArg) == 'P')
  217. {
  218. // Allow for blank password
  219. char * pArg = argv[i];
  220. if(pArg[2] != ':' || pArg[3] != 0) // Dont use bGetString for empty password case
  221. if(!bGetString(argv[i]+2, Password))
  222. return 1;
  223. pPassword = Password;
  224. }
  225. else if(toupper(*pcCurrArg) == 'A')
  226. {
  227. if(!bGetString(argv[i]+2, Authority))
  228. return 1;
  229. }
  230. else
  231. {
  232. PrintUsage();
  233. return 1;
  234. }
  235. }
  236. // Do a sanity check of the flags chosen
  237. if((bClassFlagsHardcoded == false && !ValidFlags(true, lClassFlags)) ||
  238. (bInstanceFlagsHardcoded == false && !ValidFlags(false, lInstanceFlags)))
  239. {
  240. PrintUsage();
  241. return 1;
  242. }
  243. if((lOptionFlags & WBEM_FLAG_WMI_CHECK) && strlen(cBMOFOutputName) < 1)
  244. {
  245. Trace(true, WMI_ARG_ERROR);
  246. return 1;
  247. }
  248. if(strcmp(argv[argc-1], "-?") == 0 || strcmp(argv[argc-1], "/?") == 0)
  249. {
  250. PrintUsage();
  251. return 1;
  252. }
  253. if((lOptionFlags & WBEM_FLAG_SPLIT_FILES) && bBmofSet)
  254. {
  255. PrintUsage();
  256. return 1;
  257. }
  258. // display the file name and make sure it is a valid file
  259. strcpy(cFileName, argv[argc-1]);
  260. mbstowcs(wFileName, argv[argc-1], MAX_PATH);
  261. #ifdef UNICODE
  262. if(GetFileAttributes(wFileName) == 0xFFFFFFFF)
  263. #else
  264. if(GetFileAttributes(cFileName) == 0xFFFFFFFF)
  265. #endif
  266. {
  267. Trace(true, FILE_NOT_FOUND, cFileName);
  268. return 1;
  269. }
  270. hres = InitializeCom();
  271. if(hres)
  272. {
  273. Trace(true, COMINIT_ERROR, hres);
  274. return 3;
  275. }
  276. WBEM_COMPILE_STATUS_INFO info;
  277. hres = InitializeSecurity(NULL, -1, NULL, NULL,
  278. RPC_C_AUTHN_LEVEL_CONNECT,
  279. RPC_C_IMP_LEVEL_IDENTIFY,
  280. NULL, EOAC_NONE, 0);
  281. if(strlen(cBMOFOutputName) > 0)
  282. sc = CreateBMOFViaDLL( wFileName, wBMOFFileName, wszDefault,
  283. lOptionFlags, lClassFlags,
  284. lInstanceFlags, &info);
  285. else
  286. sc = CompileFileViaDLL(wFileName, wszDefault, UserName,
  287. Authority, pPassword, lOptionFlags, lClassFlags,
  288. lInstanceFlags, &info);
  289. if(sc != S_OK)
  290. Trace(true, COMPILER_ERROR, info.hRes);
  291. CoUninitialize();
  292. if(sc == S_OK)
  293. {
  294. Trace(true, DONE);
  295. return 0;
  296. }
  297. else
  298. return info.lPhaseError;
  299. }