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.

596 lines
14 KiB

  1. #include "faxrtp.h"
  2. #pragma hdrstop
  3. HINSTANCE MyhInstance;
  4. BOOL ServiceDebug;
  5. WCHAR FaxReceiveDir[MAX_PATH];
  6. PSID ServiceSid;
  7. PFAXROUTEADDFILE FaxRouteAddFile;
  8. PFAXROUTEDELETEFILE FaxRouteDeleteFile;
  9. PFAXROUTEGETFILE FaxRouteGetFile;
  10. PFAXROUTEENUMFILES FaxRouteEnumFiles;
  11. BOOL
  12. MakeServiceSid(
  13. VOID
  14. )
  15. {
  16. SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
  17. BOOL Result;
  18. //
  19. // Output allocated via RtlAllocateHeap, free with FreeHeap?
  20. //
  21. Result = AllocateAndInitializeSid(
  22. &NtAuthority,
  23. 1,
  24. SECURITY_SERVICE_RID,
  25. 0, 0, 0, 0, 0, 0, 0,
  26. &ServiceSid
  27. );
  28. if (!Result) {
  29. ServiceSid = NULL;
  30. }
  31. return Result;
  32. }
  33. extern "C"
  34. DWORD
  35. FaxRouteDllInit(
  36. HINSTANCE hInstance,
  37. DWORD Reason,
  38. LPVOID Context
  39. )
  40. {
  41. if (Reason == DLL_PROCESS_ATTACH) {
  42. MyhInstance = hInstance;
  43. DisableThreadLibraryCalls( hInstance );
  44. MakeServiceSid();
  45. }
  46. return TRUE;
  47. }
  48. BOOL
  49. IsUserService(
  50. VOID
  51. )
  52. {
  53. BOOL Result,UserService = FALSE;
  54. Result = CheckTokenMembership( NULL, ServiceSid, &UserService );
  55. if (!Result) {
  56. DebugPrint(( TEXT("Couldn't CheckTokenMembership, ec = %d\n"), GetLastError() ));
  57. }
  58. return UserService;
  59. }
  60. BOOL WINAPI
  61. FaxRouteInitialize(
  62. IN HANDLE HeapHandle,
  63. IN PFAX_ROUTE_CALLBACKROUTINES FaxRouteCallbackRoutines
  64. )
  65. {
  66. HeapInitialize(HeapHandle,NULL,NULL,0);
  67. FaxRouteAddFile = FaxRouteCallbackRoutines->FaxRouteAddFile;
  68. FaxRouteDeleteFile = FaxRouteCallbackRoutines->FaxRouteDeleteFile;
  69. FaxRouteGetFile = FaxRouteCallbackRoutines->FaxRouteGetFile;
  70. FaxRouteEnumFiles = FaxRouteCallbackRoutines->FaxRouteEnumFiles;
  71. ServiceDebug = !IsUserService();
  72. if (!GetSpecialPath(CSIDL_COMMON_APPDATA, FaxReceiveDir)) {
  73. DebugPrint(( TEXT("Couldn't GetSpecialPath, ec = %d\n"), GetLastError() ));
  74. return FALSE;
  75. }
  76. ConcatenatePaths( FaxReceiveDir, FAX_RECEIVE_DIR );
  77. InitializeStringTable();
  78. InitializeRoutingTable();
  79. InitializeEmailRouting();
  80. return TRUE;
  81. }
  82. BOOL WINAPI
  83. FaxRouteGetRoutingInfo(
  84. IN LPCWSTR RoutingGuid,
  85. IN DWORD DeviceId,
  86. IN LPBYTE RoutingInfo,
  87. OUT LPDWORD RoutingInfoSize
  88. )
  89. {
  90. if (RoutingInfoSize == NULL) {
  91. return FALSE;
  92. }
  93. PROUTING_TABLE RoutingEntry = GetRoutingEntry( DeviceId );
  94. if (!RoutingEntry) {
  95. return FALSE;
  96. }
  97. DWORD Size;
  98. switch( GetMaskBit( RoutingGuid )) {
  99. case 0:
  100. return FALSE;
  101. case LR_PRINT:
  102. Size = sizeof(DWORD) + StringSize( RoutingEntry->PrinterName );
  103. if (RoutingInfo == NULL) {
  104. *RoutingInfoSize = Size;
  105. return TRUE;
  106. }
  107. if (Size > *RoutingInfoSize) {
  108. return FALSE;
  109. }
  110. *((LPDWORD)RoutingInfo) = (RoutingEntry->Mask & LR_PRINT) > 0;
  111. RoutingInfo += sizeof(DWORD);
  112. wcscpy( (LPWSTR)RoutingInfo, RoutingEntry->PrinterName );
  113. return TRUE;
  114. case LR_STORE:
  115. Size = sizeof(DWORD) + StringSize( RoutingEntry->StoreDir );
  116. if (RoutingInfo == NULL) {
  117. *RoutingInfoSize = Size;
  118. return TRUE;
  119. }
  120. if (Size > *RoutingInfoSize) {
  121. return FALSE;
  122. }
  123. *((LPDWORD)RoutingInfo) = (RoutingEntry->Mask & LR_STORE) > 0;
  124. RoutingInfo += sizeof(DWORD);
  125. wcscpy( (LPWSTR)RoutingInfo, RoutingEntry->StoreDir );
  126. return TRUE;
  127. case LR_INBOX:
  128. Size = sizeof(DWORD) + StringSize( RoutingEntry->ProfileName );
  129. if (RoutingInfo == NULL) {
  130. *RoutingInfoSize = Size;
  131. return TRUE;
  132. }
  133. if (Size > *RoutingInfoSize) {
  134. return FALSE;
  135. }
  136. *((LPDWORD)RoutingInfo) = (RoutingEntry->Mask & LR_INBOX) > 0;
  137. RoutingInfo += sizeof(DWORD);
  138. wcscpy( (LPWSTR)RoutingInfo, RoutingEntry->ProfileName );
  139. return TRUE;
  140. case LR_EMAIL:
  141. Size = sizeof(DWORD) + StringSize( RoutingEntry->ProfileName );
  142. if (RoutingInfo == NULL) {
  143. *RoutingInfoSize = Size;
  144. return TRUE;
  145. }
  146. if (Size > *RoutingInfoSize) {
  147. return FALSE;
  148. }
  149. *((LPDWORD)RoutingInfo) = (RoutingEntry->Mask & LR_EMAIL) > 0;
  150. RoutingInfo += sizeof(DWORD);
  151. wcscpy( (LPWSTR)RoutingInfo, RoutingEntry->ProfileName );
  152. return TRUE;
  153. }
  154. return FALSE;
  155. }
  156. BOOL WINAPI
  157. FaxRouteSetRoutingInfo(
  158. IN LPCWSTR RoutingGuid,
  159. IN DWORD DeviceId,
  160. IN const BYTE *RoutingInfo,
  161. IN DWORD RoutingInfoSize
  162. )
  163. {
  164. if (RoutingInfoSize == 0 || RoutingInfo == NULL) {
  165. return FALSE;
  166. }
  167. PROUTING_TABLE RoutingEntry = GetRoutingEntry( DeviceId );
  168. if (!RoutingEntry) {
  169. return FALSE;
  170. }
  171. switch( GetMaskBit( RoutingGuid ) ) {
  172. case 0:
  173. return FALSE;
  174. case LR_PRINT:
  175. if (*((LPDWORD)RoutingInfo)) {
  176. RoutingEntry->Mask |= LR_PRINT;
  177. MemFree( (LPBYTE) RoutingEntry->PrinterName );
  178. RoutingInfo += sizeof(DWORD);
  179. RoutingEntry->PrinterName = StringDup( (LPWSTR)RoutingInfo );
  180. } else {
  181. RoutingEntry->Mask &= ~LR_PRINT;
  182. }
  183. UpdateRoutingInfoRegistry( RoutingEntry );
  184. return TRUE;
  185. case LR_STORE:
  186. if (*((LPDWORD)RoutingInfo)) {
  187. RoutingEntry->Mask |= LR_STORE;
  188. MemFree( (LPBYTE) RoutingEntry->StoreDir );
  189. RoutingInfo += sizeof(DWORD);
  190. RoutingEntry->StoreDir = StringDup( (LPWSTR)RoutingInfo );
  191. } else {
  192. RoutingEntry->Mask &= ~LR_STORE;
  193. }
  194. UpdateRoutingInfoRegistry( RoutingEntry );
  195. return TRUE;
  196. case LR_INBOX:
  197. if (*((LPDWORD)RoutingInfo)) {
  198. RoutingEntry->Mask |= LR_INBOX;
  199. MemFree( (LPBYTE) RoutingEntry->ProfileName );
  200. RoutingInfo += sizeof(DWORD);
  201. RoutingEntry->ProfileName = StringDup( (LPWSTR)RoutingInfo );
  202. RoutingEntry->ProfileInfo = AddNewMapiProfile( RoutingEntry->ProfileName, FALSE, TRUE );
  203. } else {
  204. RoutingEntry->Mask &= ~LR_INBOX;
  205. }
  206. UpdateRoutingInfoRegistry( RoutingEntry );
  207. return TRUE;
  208. case LR_EMAIL:
  209. if (*((LPDWORD)RoutingInfo)) {
  210. RoutingEntry->Mask |= LR_EMAIL;
  211. MemFree( (LPBYTE) RoutingEntry->ProfileName );
  212. RoutingInfo += sizeof(DWORD);
  213. RoutingEntry->ProfileName = StringDup( (LPWSTR)RoutingInfo );
  214. } else {
  215. RoutingEntry->Mask &= ~LR_EMAIL;
  216. }
  217. UpdateRoutingInfoRegistry( RoutingEntry );
  218. return TRUE;
  219. }
  220. return FALSE;
  221. }
  222. BOOL WINAPI
  223. FaxRoutePrint(
  224. const FAX_ROUTE *FaxRoute,
  225. PVOID *FailureData,
  226. LPDWORD FailureDataSize
  227. )
  228. {
  229. PROUTING_TABLE RoutingEntry;
  230. WCHAR NameBuffer[MAX_PATH];
  231. LPCWSTR FBaseName;
  232. WCHAR TiffFileName[MAX_PATH];
  233. DWORD Size;
  234. RoutingEntry = GetRoutingEntry( FaxRoute->DeviceId );
  235. if (!RoutingEntry) {
  236. return FALSE;
  237. }
  238. if (!(RoutingEntry->Mask & LR_PRINT)) {
  239. return TRUE;
  240. }
  241. Size = sizeof(TiffFileName);
  242. if (!FaxRouteGetFile(
  243. FaxRoute->JobId,
  244. 1,
  245. TiffFileName,
  246. &Size))
  247. {
  248. return FALSE;
  249. }
  250. //
  251. // print the fax in requested to do so
  252. //
  253. if (!RoutingEntry->PrinterName[0]) {
  254. GetProfileString( L"windows",
  255. L"device",
  256. L",,,",
  257. (LPWSTR) &NameBuffer,
  258. MAX_PATH
  259. );
  260. FBaseName = NameBuffer;
  261. } else {
  262. FBaseName = RoutingEntry->PrinterName;
  263. }
  264. return TiffPrint( TiffFileName, (LPTSTR)FBaseName );
  265. }
  266. BOOL WINAPI
  267. FaxRouteStore(
  268. const FAX_ROUTE *FaxRoute,
  269. PVOID *FailureData,
  270. LPDWORD FailureDataSize
  271. )
  272. {
  273. PROUTING_TABLE RoutingEntry;
  274. WCHAR TiffFileName[MAX_PATH];
  275. DWORD Size;
  276. LPTSTR FullPath = NULL;
  277. DWORD StrCount;
  278. RoutingEntry = GetRoutingEntry( FaxRoute->DeviceId );
  279. if (!RoutingEntry) {
  280. return FALSE;
  281. }
  282. if (((RoutingEntry->Mask & LR_STORE) == 0) || (RoutingEntry->StoreDir[0] == 0)) {
  283. return TRUE;
  284. }
  285. Size = sizeof(TiffFileName);
  286. if (!FaxRouteGetFile(
  287. FaxRoute->JobId,
  288. 1,
  289. TiffFileName,
  290. &Size))
  291. {
  292. return FALSE;
  293. }
  294. StrCount = ExpandEnvironmentStrings( RoutingEntry->StoreDir, FullPath, 0 );
  295. FullPath = (LPWSTR) MemAlloc( StrCount * sizeof(WCHAR) );
  296. if (!FullPath) {
  297. return FALSE;
  298. }
  299. ExpandEnvironmentStrings( RoutingEntry->StoreDir, FullPath, StrCount );
  300. // if we are moving the fax to the directory that is was received into, do nothing to the file
  301. if (_wcsicmp( FullPath, FaxReceiveDir ) == 0) {
  302. FaxLog(
  303. FAXLOG_CATEGORY_INBOUND,
  304. FAXLOG_LEVEL_MAX,
  305. 2,
  306. MSG_FAX_SAVE_SUCCESS,
  307. TiffFileName,
  308. TiffFileName
  309. );
  310. } else if (!FaxMoveFile ( TiffFileName, FullPath )) {
  311. MemFree( FullPath );
  312. return FALSE;
  313. }
  314. MemFree( FullPath );
  315. return TRUE;
  316. }
  317. BOOL WINAPI
  318. FaxRouteInBox(
  319. const FAX_ROUTE *FaxRoute,
  320. PVOID *FailureData,
  321. LPDWORD FailureDataSize
  322. )
  323. {
  324. PROUTING_TABLE RoutingEntry;
  325. WCHAR TiffFileName[MAX_PATH];
  326. DWORD Size;
  327. LPTSTR FullPath = NULL;
  328. RoutingEntry = GetRoutingEntry( FaxRoute->DeviceId );
  329. if (!RoutingEntry) {
  330. return FALSE;
  331. }
  332. if (((RoutingEntry->Mask & LR_INBOX) == 0) || (RoutingEntry->ProfileName[0] == 0)) {
  333. return TRUE;
  334. }
  335. Size = sizeof(TiffFileName);
  336. if (!FaxRouteGetFile(
  337. FaxRoute->JobId,
  338. 1,
  339. TiffFileName,
  340. &Size))
  341. {
  342. return FALSE;
  343. }
  344. if (TiffMailDefault( FaxRoute, RoutingEntry, TiffFileName )) {
  345. FaxLog(
  346. FAXLOG_CATEGORY_INBOUND,
  347. FAXLOG_LEVEL_MAX,
  348. 2,
  349. MSG_FAX_INBOX_SUCCESS,
  350. TiffFileName,
  351. GetProfileName( RoutingEntry->ProfileInfo )
  352. );
  353. } else {
  354. FaxLog(
  355. FAXLOG_CATEGORY_INBOUND,
  356. FAXLOG_LEVEL_MIN,
  357. 2,
  358. MSG_FAX_INBOX_FAILED,
  359. TiffFileName,
  360. GetProfileName( RoutingEntry->ProfileInfo )
  361. );
  362. return FALSE;
  363. }
  364. return TRUE;
  365. }
  366. BOOL WINAPI
  367. FaxRouteEmail(
  368. const FAX_ROUTE *FaxRoute,
  369. PVOID *FailureData,
  370. LPDWORD FailureDataSize
  371. )
  372. {
  373. PROUTING_TABLE RoutingEntry;
  374. WCHAR TiffFileName[MAX_PATH];
  375. DWORD Size;
  376. LPTSTR FullPath = NULL;
  377. RoutingEntry = GetRoutingEntry( FaxRoute->DeviceId );
  378. if (!RoutingEntry) {
  379. return FALSE;
  380. }
  381. if (((RoutingEntry->Mask & LR_EMAIL) == 0) || (RoutingEntry->ProfileName[0] == 0)) {
  382. return TRUE;
  383. }
  384. Size = sizeof(TiffFileName);
  385. if (!FaxRouteGetFile(
  386. FaxRoute->JobId,
  387. 1,
  388. TiffFileName,
  389. &Size))
  390. {
  391. return FALSE;
  392. }
  393. if (TiffRouteEMail( FaxRoute, RoutingEntry, TiffFileName )) {
  394. FaxLog(
  395. FAXLOG_CATEGORY_INBOUND,
  396. FAXLOG_LEVEL_MAX,
  397. 2,
  398. MSG_FAX_INBOX_SUCCESS,
  399. TiffFileName,
  400. GetProfileName( InboundProfileInfo )
  401. );
  402. } else {
  403. FaxLog(
  404. FAXLOG_CATEGORY_INBOUND,
  405. FAXLOG_LEVEL_MIN,
  406. 2,
  407. MSG_FAX_INBOX_FAILED,
  408. TiffFileName,
  409. GetProfileName( InboundProfileInfo )
  410. );
  411. return FALSE;
  412. }
  413. return TRUE;
  414. }
  415. BOOL WINAPI
  416. FaxRouteConfigure(
  417. OUT HPROPSHEETPAGE *PropSheetPage
  418. )
  419. {
  420. return TRUE;
  421. }
  422. BOOL WINAPI
  423. FaxRouteDeviceEnable(
  424. IN LPCWSTR RoutingGuid,
  425. IN DWORD DeviceId,
  426. IN LONG Enabled
  427. )
  428. {
  429. PROUTING_TABLE RoutingEntry = GetRoutingEntry( DeviceId );
  430. if (!RoutingEntry) {
  431. return FALSE;
  432. }
  433. DWORD MaskBit = GetMaskBit( RoutingGuid );
  434. if (MaskBit == 0) {
  435. return FALSE;
  436. }
  437. BOOL DeviceEnabled = (RoutingEntry->Mask & MaskBit) > 0;
  438. if (Enabled == -1) {
  439. return DeviceEnabled;
  440. }
  441. RoutingEntry->Mask &= ~MaskBit;
  442. if (Enabled) {
  443. RoutingEntry->Mask |= MaskBit;
  444. }
  445. UpdateRoutingInfoRegistry( RoutingEntry );
  446. return TRUE;
  447. }
  448. BOOL WINAPI
  449. FaxRouteDeviceChangeNotification(
  450. IN DWORD DeviceId,
  451. IN BOOL NewDevice
  452. )
  453. {
  454. HKEY hKey;
  455. HKEY hKeyRouting;
  456. WCHAR SubKeyName[128];
  457. if (!NewDevice) {
  458. return TRUE;
  459. }
  460. PROUTING_TABLE RoutingEntry = GetRoutingEntry( DeviceId );
  461. if (RoutingEntry) {
  462. return TRUE;
  463. }
  464. wsprintf( SubKeyName, L"%s\\%08d", REGKEY_FAX_DEVICES, DeviceId );
  465. hKey = OpenRegistryKey( HKEY_LOCAL_MACHINE, SubKeyName, FALSE, KEY_READ );
  466. if (!hKey) {
  467. return FALSE;
  468. }
  469. hKeyRouting = OpenRegistryKey( hKey, REGKEY_ROUTING, FALSE, KEY_READ );
  470. if (!hKeyRouting) {
  471. RegCloseKey( hKey );
  472. return FALSE;
  473. }
  474. AddNewDeviceToRoutingTable(
  475. GetRegistryDword ( hKey, REGVAL_PERMANENT_LINEID ),
  476. GetRegistryString( hKey, REGVAL_DEVICE_NAME, EMPTY_STRING ),
  477. GetRegistryString( hKey, REGVAL_ROUTING_CSID, EMPTY_STRING ),
  478. GetRegistryString( hKey, REGVAL_ROUTING_TSID, EMPTY_STRING ),
  479. GetRegistryString( hKeyRouting, REGVAL_ROUTING_PRINTER, EMPTY_STRING ),
  480. GetRegistryString( hKeyRouting, REGVAL_ROUTING_DIR, EMPTY_STRING ),
  481. GetRegistryString( hKeyRouting, REGVAL_ROUTING_PROFILE, EMPTY_STRING ),
  482. GetRegistryDword ( hKeyRouting, REGVAL_ROUTING_MASK )
  483. );
  484. RegCloseKey( hKeyRouting );
  485. RegCloseKey( hKey );
  486. return TRUE;
  487. }