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.

556 lines
9.5 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. sfmutil.c
  5. Author:
  6. Krishg
  7. Revision History:
  8. --*/
  9. #if DBG==1 && DEVL==1
  10. #define DEBUG
  11. #endif
  12. #include <nt.h>
  13. #include <ntrtl.h>
  14. #include <nturtl.h>
  15. #include <windows.h>
  16. #include <winspool.h>
  17. #include <ntsam.h>
  18. #include <ntlsa.h>
  19. #include <stdlib.h>
  20. #include "sfmutil.h"
  21. #ifdef DBCS
  22. #include "locale.h"
  23. #endif // DBCS
  24. HANDLE ThisDLLHandle;
  25. CHAR ReturnTextBuffer[512];
  26. BOOL
  27. UtilDLLInit(
  28. IN HANDLE DLLHandle,
  29. IN DWORD Reason,
  30. IN LPVOID ReservedAndUnused
  31. )
  32. {
  33. ReservedAndUnused;
  34. switch(Reason) {
  35. case DLL_PROCESS_ATTACH:
  36. ThisDLLHandle = DLLHandle;
  37. #ifdef DBCS // UtilDLLInit()
  38. // we want to have the Unicode <-> Ansi conversion based on System locale.
  39. setlocale(LC_ALL,"");
  40. #endif // DBCS
  41. break;
  42. case DLL_PROCESS_DETACH:
  43. case DLL_THREAD_ATTACH:
  44. case DLL_THREAD_DETACH:
  45. break;
  46. }
  47. return(TRUE);
  48. }
  49. /* Delete the file. If Unable to delete the file return failed */
  50. BOOL
  51. DelFile (
  52. DWORD cArgs,
  53. LPTSTR Args[],
  54. LPTSTR *TextOut
  55. )
  56. {
  57. BOOL Status = FALSE;
  58. LPTSTR pSrcFile = NULL;
  59. INT cbAscii;
  60. *TextOut = (LPTSTR)ReturnTextBuffer;
  61. do
  62. {
  63. if(cArgs!=1)
  64. break;
  65. cbAscii = strlen((LPSTR)Args[0]) +1;
  66. pSrcFile = (LPTSTR)LocalAlloc(LPTR, sizeof(WCHAR) * cbAscii);
  67. if(pSrcFile == NULL)
  68. break;
  69. if(mbstowcs(pSrcFile, (LPSTR)Args[0],cbAscii) == -1)
  70. break;
  71. Status = DeleteFile(pSrcFile);
  72. } while (FALSE);
  73. if(pSrcFile != NULL)
  74. LocalFree(pSrcFile);
  75. strcpy(ReturnTextBuffer, Status ? (LPSTR)"SUCCESS" : (LPSTR)"FAILED");
  76. return Status;
  77. }
  78. /*
  79. * Copy Uam Files :
  80. * Copies the UAM Files to the NTFS Volume
  81. */
  82. BOOL
  83. CopyUamFiles (
  84. DWORD cArgs,
  85. LPTSTR Args[],
  86. LPTSTR *TextOut
  87. )
  88. {
  89. BOOL Status = FALSE;
  90. INT cbAscii;
  91. LPTSTR pSrcFile = NULL;
  92. LPTSTR pDestFile = NULL;
  93. *TextOut = (LPTSTR)ReturnTextBuffer;
  94. do
  95. {
  96. if(cArgs!=2)
  97. break;
  98. cbAscii = strlen((LPSTR)Args[0]) +1;
  99. pSrcFile = (LPTSTR)LocalAlloc(LPTR, sizeof(WCHAR) * cbAscii);
  100. if(pSrcFile == NULL)
  101. break;
  102. if(mbstowcs(pSrcFile, (LPSTR)Args[0],cbAscii) == -1)
  103. break;
  104. cbAscii = strlen((LPSTR)Args[1]) +1;
  105. pDestFile = (LPTSTR)LocalAlloc(LPTR, sizeof(WCHAR) * cbAscii);
  106. if(pDestFile == NULL)
  107. break;
  108. if(mbstowcs(pDestFile, (LPSTR)Args[1],cbAscii) == -1)
  109. break;
  110. #ifdef DEBUG
  111. DbgPrint("Source File %ws\n",pSrcFile);
  112. DbgPrint("Destination File %ws\n",pDestFile);
  113. #endif
  114. Status = CopyFile(pSrcFile,pDestFile,FALSE);
  115. } while (FALSE);
  116. if(pSrcFile != NULL)
  117. LocalFree(pSrcFile);
  118. if(pDestFile != NULL)
  119. LocalFree(pDestFile);
  120. strcpy(ReturnTextBuffer, Status ? (LPSTR)"SUCCESS" : (LPSTR)"FAILED");
  121. return Status;
  122. }
  123. #define AFPMGRKEY TEXT("Afp Manager")
  124. BOOL
  125. WriteAfpMgrIniStrings (
  126. DWORD cArgs,
  127. LPTSTR Args[],
  128. LPTSTR *TextOut
  129. )
  130. {
  131. BOOL Status = FALSE;
  132. BOOL KeyDelete = FALSE;
  133. INT cbAscii;
  134. LPTSTR pSectionName = NULL;
  135. LPTSTR pString = NULL;
  136. LPTSTR pIniFile = NULL;
  137. *TextOut = (LPTSTR)ReturnTextBuffer;
  138. do
  139. {
  140. if(cArgs != 3)
  141. break;
  142. cbAscii = strlen((LPSTR)Args[0]) +1;
  143. pSectionName = (LPTSTR)LocalAlloc(LPTR, sizeof(WCHAR) * cbAscii);
  144. if(pSectionName == NULL)
  145. break;
  146. if(mbstowcs(pSectionName, (LPSTR)Args[0],cbAscii) == -1)
  147. break;
  148. //
  149. // If the String is NULL, set String to NULL, otherwise
  150. // convert the string to unicode
  151. //
  152. if(!strcmp((LPSTR)Args[1],(LPSTR)"NULL")) {
  153. KeyDelete = TRUE;
  154. }
  155. else
  156. {
  157. cbAscii = strlen((LPSTR)Args[1]) +1;
  158. pString = (LPTSTR)LocalAlloc(LPTR, sizeof(WCHAR) * cbAscii);
  159. if(pString == NULL)
  160. break;
  161. if(mbstowcs(pString, (LPSTR)Args[1],cbAscii) == -1)
  162. break;
  163. }
  164. cbAscii = strlen((LPSTR)Args[2]) +1;
  165. pIniFile = (LPTSTR)LocalAlloc(LPTR, sizeof(WCHAR) * cbAscii);
  166. if(pIniFile == NULL)
  167. break;
  168. if(mbstowcs(pIniFile, (LPSTR)Args[2],cbAscii) == -1)
  169. break;
  170. #ifdef DEBUG
  171. DbgPrint("File:%ws\n",pIniFile);
  172. DbgPrint("String: %ws\n",pString);
  173. DbgPrint("Section: %ws\n",pSectionName);
  174. #endif
  175. Status = WritePrivateProfileString (pSectionName,
  176. AFPMGRKEY,
  177. (KeyDelete == TRUE) ? (LPCTSTR)NULL : pString,
  178. pIniFile);
  179. } while (FALSE);
  180. if(pIniFile != NULL)
  181. LocalFree(pIniFile);
  182. if(pSectionName != NULL)
  183. LocalFree(pSectionName);
  184. if(pString != NULL)
  185. LocalFree(pString);
  186. strcpy(ReturnTextBuffer ,Status ? (LPSTR)"SUCCESS": (LPSTR)"FAILED");
  187. return Status;
  188. }
  189. BOOL
  190. GetPrintProcDir (
  191. DWORD cArgs,
  192. LPTSTR Args[],
  193. LPTSTR *TextOut
  194. )
  195. {
  196. BOOL Status = FALSE;
  197. DWORD BytesCopied;
  198. LPTSTR wpProcFile = NULL;
  199. *TextOut = (LPTSTR)ReturnTextBuffer;
  200. do
  201. {
  202. wpProcFile = (LPTSTR)LocalAlloc(LPTR, 512);
  203. if(wpProcFile == NULL)
  204. break;
  205. Status = GetPrintProcessorDirectory(NULL,
  206. NULL,
  207. 1,
  208. (LPBYTE)wpProcFile,
  209. 512,
  210. &BytesCopied
  211. );
  212. if(Status) {
  213. if(wcstombs(ReturnTextBuffer,wpProcFile,512) ==-1) {
  214. Status = FALSE;
  215. break;
  216. }
  217. }
  218. } while (FALSE);
  219. if(wpProcFile != NULL)
  220. LocalFree(wpProcFile);
  221. if(!Status)
  222. strcpy(ReturnTextBuffer ,(LPSTR)"FAILED");
  223. return Status;
  224. }
  225. BOOL
  226. AddPrintProc (
  227. DWORD cArgs,
  228. LPTSTR Args[],
  229. LPTSTR *TextOut
  230. )
  231. {
  232. BOOL Status = FALSE;
  233. LPTSTR pPrintProcessorFile = NULL;
  234. LPTSTR pPrintProcessor = NULL;
  235. INT cbAscii;
  236. *TextOut = (LPTSTR)ReturnTextBuffer;
  237. do
  238. {
  239. if(cArgs != 2)
  240. break;
  241. cbAscii = strlen((LPSTR)Args[0]) +1;
  242. pPrintProcessorFile = (LPTSTR)LocalAlloc(LPTR, sizeof(WCHAR) * cbAscii);
  243. if(pPrintProcessorFile == NULL)
  244. break;
  245. if(mbstowcs(pPrintProcessorFile, (LPSTR)Args[0],cbAscii) == -1)
  246. break;
  247. cbAscii = strlen((LPSTR)Args[1]) +1;
  248. pPrintProcessor = (LPTSTR)LocalAlloc(LPTR, sizeof(WCHAR) * cbAscii);
  249. if(pPrintProcessor == NULL)
  250. break;
  251. if(mbstowcs(pPrintProcessor, (LPSTR)Args[1],cbAscii) == -1)
  252. break;
  253. Status = AddPrintProcessor(NULL, // Do local stuff only
  254. NULL, // Use Current Environment
  255. pPrintProcessorFile, // PSPRINT.DLL
  256. pPrintProcessor
  257. );
  258. } while (FALSE);
  259. if(pPrintProcessorFile != NULL)
  260. LocalFree(pPrintProcessorFile);
  261. if(pPrintProcessor != NULL)
  262. LocalFree(pPrintProcessor);
  263. strcpy(ReturnTextBuffer ,Status ? (LPSTR)"SUCCESS": (LPSTR)"FAILED");
  264. return Status;
  265. }
  266. /* Delete the PSTODIB PSPRINT PrintProcessor
  267. *
  268. *
  269. */
  270. BOOL
  271. DeletePrintProc (
  272. DWORD cArgs,
  273. LPTSTR Args[],
  274. LPTSTR *TextOut
  275. )
  276. {
  277. BOOL Status = FALSE;
  278. LPTSTR pPrintProcessorFile = NULL;
  279. LPTSTR pPrintProcessor = NULL;
  280. INT cbAscii;
  281. *TextOut = (LPTSTR)ReturnTextBuffer;
  282. do
  283. {
  284. if(cArgs != 2)
  285. break;
  286. cbAscii = strlen((LPSTR)Args[0]) +1;
  287. pPrintProcessorFile = (LPTSTR)LocalAlloc(LPTR, sizeof(WCHAR) * cbAscii);
  288. if(pPrintProcessorFile == NULL)
  289. break;
  290. if(mbstowcs(pPrintProcessorFile, (LPSTR)Args[0],cbAscii) == -1)
  291. break;
  292. cbAscii = strlen((LPSTR)Args[1]) +1;
  293. pPrintProcessor = (LPTSTR)LocalAlloc(LPTR, sizeof(WCHAR) * cbAscii);
  294. if(pPrintProcessor == NULL)
  295. break;
  296. if(mbstowcs(pPrintProcessor, (LPSTR)Args[1],cbAscii) == -1)
  297. break;
  298. Status = DeletePrintProcessor( pPrintProcessorFile,
  299. NULL, // Use Current Environment
  300. pPrintProcessor
  301. );
  302. } while (FALSE);
  303. if(pPrintProcessorFile != NULL)
  304. LocalFree(pPrintProcessorFile);
  305. if(pPrintProcessor != NULL)
  306. LocalFree(pPrintProcessor);
  307. strcpy(ReturnTextBuffer ,Status ? (LPSTR)"SUCCESS": (LPSTR)"FAILED");
  308. return Status;
  309. }
  310. BOOL
  311. SfmAddPrintMonitor (
  312. DWORD cArgs,
  313. LPTSTR Args[],
  314. LPTSTR *TextOut
  315. )
  316. {
  317. BOOL Status = FALSE;
  318. LPTSTR wpMonName = NULL;
  319. INT cbAscii = 0;
  320. LPSTR pMonInfoBuffer = NULL;
  321. TCHAR MonDllName[] = L"sfmmon.dll";
  322. PMONITOR_INFO_2 pmoninfo;
  323. *TextOut = (LPTSTR)ReturnTextBuffer;
  324. do {
  325. if(cArgs != 1)
  326. break;
  327. cbAscii = strlen((LPSTR)Args[0]) + 1;
  328. wpMonName = (LPTSTR)LocalAlloc(LPTR,sizeof(WCHAR) * cbAscii);
  329. if(wpMonName == NULL)
  330. break;
  331. if(mbstowcs(wpMonName, (LPSTR)Args[0], cbAscii) == -1)
  332. break;
  333. pMonInfoBuffer = (LPSTR) LocalAlloc(LPTR, 512);
  334. if(pMonInfoBuffer == NULL)
  335. break;
  336. pmoninfo = (PMONITOR_INFO_2)pMonInfoBuffer;
  337. pmoninfo->pName = wpMonName;
  338. pmoninfo->pEnvironment = NULL;
  339. pmoninfo->pDLLName = MonDllName;
  340. #ifdef DEBUG
  341. DbgPrint("ADDPRINTMONITOR Name : %ws\n", pmoninfo->pName);
  342. DbgPrint("ADDPRINTMONITOR Dll Name : %ws\n", pmoninfo->pDLLName);
  343. #endif
  344. Status = AddMonitor( NULL,
  345. 2,
  346. (LPSTR)pMonInfoBuffer
  347. );
  348. }while(FALSE);
  349. if(wpMonName != NULL)
  350. LocalFree(wpMonName);
  351. if(pMonInfoBuffer != NULL)
  352. LocalFree(pMonInfoBuffer);
  353. strcpy(ReturnTextBuffer ,Status ? (LPSTR)"SUCCESS": (LPSTR)"FAILED");
  354. return Status;
  355. }
  356. BOOL
  357. SfmDeletePrintMonitor (
  358. DWORD cArgs,
  359. LPTSTR Args[],
  360. LPTSTR *TextOut
  361. )
  362. {
  363. BOOL Status = FALSE;
  364. LPTSTR wpMonName = NULL;
  365. INT cbAscii;
  366. *TextOut = (LPTSTR)ReturnTextBuffer;
  367. do
  368. {
  369. if(cArgs !=1)
  370. break;
  371. cbAscii = strlen((LPSTR)Args[0]) + 1;
  372. wpMonName = (LPTSTR)LocalAlloc(LPTR,sizeof(WCHAR) * cbAscii);
  373. if(wpMonName == NULL)
  374. break;
  375. if(mbstowcs(wpMonName, (LPSTR)Args[0], cbAscii) == -1)
  376. break;
  377. Status = DeleteMonitor( NULL,
  378. NULL,
  379. wpMonName
  380. );
  381. }while(FALSE);
  382. if(wpMonName != NULL)
  383. LocalFree(wpMonName);
  384. strcpy(ReturnTextBuffer ,Status ? (LPSTR)"SUCCESS": (LPSTR)"FAILED");
  385. return Status;
  386. }