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.

695 lines
24 KiB

  1. /************************************************************************************************
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name: POP3RegKeys.c
  4. Notes:
  5. History:
  6. ************************************************************************************************/
  7. #include "rpc.h"
  8. #include "rpcndr.h"
  9. #include <assert.h>
  10. #include <rpcdce.h>
  11. #include <windows.h>
  12. #include <tchar.h>
  13. #include "Pop3RegKeys.h"
  14. #include <DSRole.h>
  15. #include <sddl.h>
  16. #include <Aclapi.h>
  17. long RegHKLMOpenKey( LPCTSTR psSubKey, REGSAM samDesired, PHKEY phKey, LPTSTR psMachineName )
  18. {
  19. long lRC;
  20. if ( NULL == psMachineName )
  21. lRC = RegOpenKeyEx( HKEY_LOCAL_MACHINE, psSubKey, 0, samDesired, phKey );
  22. else
  23. {
  24. HKEY hKey;
  25. WCHAR sBuffer[MAX_PATH];
  26. if ( 0 < _snwprintf( sBuffer, sizeof( sBuffer )/sizeof(WCHAR), L"\\\\%s", psMachineName ))
  27. {
  28. lRC = RegConnectRegistry( sBuffer, HKEY_LOCAL_MACHINE, &hKey );
  29. if ( ERROR_SUCCESS == lRC )
  30. {
  31. lRC = RegOpenKeyEx( hKey, psSubKey, 0, samDesired, phKey );
  32. RegCloseKey( hKey );
  33. }
  34. }
  35. else
  36. lRC = ERROR_INSUFFICIENT_BUFFER;
  37. }
  38. return lRC;
  39. }
  40. long RegQueryDWORD( LPCTSTR lpSubKey, LPCTSTR lpValueName, DWORD *pdwValue, LPTSTR psMachineName /*= NULL*/, bool bDefault /*=false*/, DWORD dwDefault /*=0*/ )
  41. {
  42. HKEY hKey;
  43. DWORD dwType = REG_DWORD;
  44. DWORD dwSize = sizeof(DWORD);
  45. long lRC;
  46. lRC = RegHKLMOpenKey( lpSubKey, KEY_QUERY_VALUE, &hKey, psMachineName );
  47. if ( ERROR_SUCCESS == lRC )
  48. {
  49. lRC = RegQueryValueEx( hKey, lpValueName, NULL, &dwType, reinterpret_cast<LPBYTE>( pdwValue ), &dwSize );
  50. if ( ERROR_FILE_NOT_FOUND == lRC && bDefault )
  51. {
  52. *pdwValue = dwDefault;
  53. lRC = ERROR_SUCCESS;
  54. }
  55. RegCloseKey( hKey );
  56. }
  57. return lRC;
  58. }
  59. long RegQueryString( LPCTSTR lpSubKey, LPCTSTR lpValueName, LPTSTR psStrBuf, DWORD *pdwSize, LPTSTR psMachineName /*= NULL*/ )
  60. {
  61. HKEY hKey;
  62. DWORD dwType = REG_SZ;
  63. long lRC;
  64. if( (NULL == psStrBuf) ||
  65. (NULL == pdwSize ) )
  66. {
  67. return ERROR_INVALID_PARAMETER;
  68. }
  69. lRC = RegHKLMOpenKey( lpSubKey, KEY_QUERY_VALUE, &hKey, psMachineName );
  70. if(ERROR_SUCCESS == lRC )
  71. {
  72. lRC = RegQueryValueEx( hKey, lpValueName, 0, &dwType, reinterpret_cast<LPBYTE>( psStrBuf ), pdwSize );
  73. RegCloseKey( hKey );
  74. }
  75. return lRC;
  76. }
  77. long RegSetString( LPCTSTR lpSubKey, LPCTSTR lpValueName, LPTSTR psStrBuf, LPTSTR psMachineName /*= NULL*/ )
  78. {
  79. HKEY hKey;
  80. DWORD dwType = REG_SZ;
  81. long lRC;
  82. if( NULL == psStrBuf )
  83. {
  84. return ERROR_INVALID_PARAMETER;
  85. }
  86. lRC = RegHKLMOpenKey( lpSubKey, KEY_QUERY_VALUE, &hKey, psMachineName );
  87. if(ERROR_SUCCESS == lRC )
  88. {
  89. lRC = RegSetValueEx( hKey, lpValueName, 0, dwType, reinterpret_cast<LPBYTE>( psStrBuf ), sizeof(WCHAR)*(wcslen(psStrBuf)+1) );
  90. RegCloseKey( hKey );
  91. }
  92. return lRC;
  93. }
  94. long RegQueryMailRoot( LPTSTR psMailRoot, DWORD dwSize, LPTSTR psMachineName /*= NULL*/ )
  95. {
  96. assert(!( NULL == psMailRoot ));
  97. if ( NULL == psMailRoot )
  98. return ERROR_INVALID_PARAMETER;
  99. HKEY hKey;
  100. DWORD dwType = REG_SZ;
  101. long lRC;
  102. dwSize *= sizeof( TCHAR ); // dwSize in characters we need bytes for RegQueryValueEx
  103. lRC = RegHKLMOpenKey( POP3SERVER_SOFTWARE_SUBKEY, KEY_QUERY_VALUE, &hKey, psMachineName );
  104. if ( ERROR_SUCCESS == lRC )
  105. {
  106. lRC = RegQueryValueEx( hKey, VALUENAME_MAILROOT, 0, &dwType, reinterpret_cast<LPBYTE>( psMailRoot ), &dwSize );
  107. if ( ERROR_SUCCESS == lRC )
  108. {
  109. dwSize = _tcslen( psMailRoot );
  110. if ( 0 < dwSize && _T('\\') == *( psMailRoot + dwSize - 1 ))
  111. *( psMailRoot + dwSize - 1 ) = 0x0;
  112. }
  113. RegCloseKey( hKey );
  114. }
  115. return lRC;
  116. }
  117. long RegQueryGreeting( LPTSTR psGreeting, DWORD dwSize, LPTSTR psMachineName /*= NULL*/ )
  118. {
  119. assert(!( NULL == psGreeting ));
  120. if ( NULL == psGreeting )
  121. return ERROR_INVALID_PARAMETER;
  122. HKEY hKey;
  123. DWORD dwType = REG_SZ;
  124. long lRC;
  125. lRC = RegHKLMOpenKey( POP3SERVER_SOFTWARE_SUBKEY, KEY_QUERY_VALUE, &hKey, psMachineName );
  126. if ( ERROR_SUCCESS == lRC )
  127. {
  128. lRC = RegQueryValueEx( hKey, VALUENAME_GREETING, 0, &dwType, reinterpret_cast<LPBYTE>( psGreeting ), &dwSize );
  129. RegCloseKey( hKey );
  130. }
  131. return lRC;
  132. }
  133. long RegQueryAuthGuid( LPTSTR psAuthGuid, DWORD *pdwSize, LPTSTR psMachineName /*= NULL*/ )
  134. {
  135. assert(!( NULL == psAuthGuid ));
  136. assert(!( NULL == pdwSize ));
  137. if ( NULL == psAuthGuid )
  138. return ERROR_INVALID_PARAMETER;
  139. HKEY hKey;
  140. DWORD dwType = REG_SZ;
  141. long lRC;
  142. lRC = RegHKLMOpenKey( POP3SERVER_AUTH_SUBKEY, KEY_QUERY_VALUE, &hKey, psMachineName );
  143. if ( ERROR_SUCCESS == lRC )
  144. {
  145. lRC = RegQueryValueEx( hKey, VALUENAME_AUTHGUID, 0, &dwType, reinterpret_cast<LPBYTE>( psAuthGuid ), pdwSize );
  146. RegCloseKey( hKey );
  147. }
  148. return lRC;
  149. }
  150. long RegQueryAuthMethod( DWORD& dwValue, LPTSTR psMachineName /*= NULL*/ )
  151. {
  152. return RegQueryDWORD( POP3SERVER_AUTH_SUBKEY, VALUENAME_DEFAULTAUTH, &dwValue, psMachineName );
  153. }
  154. long RegQueryConfirmAddUser( DWORD& dwValue, LPTSTR psMachineName /*= NULL*/ )
  155. {
  156. return RegQueryDWORD( POP3SERVER_SOFTWARE_SUBKEY, VALUENAME_CONFIRM_ADDUSER, &dwValue, psMachineName, true, 1 );
  157. }
  158. long RegQuerySPARequired( DWORD& dwValue, LPTSTR psMachineName /*= NULL*/ )
  159. {
  160. return RegQueryDWORD( POP3SERVER_SOFTWARE_SUBKEY, VALUENAME_SPA_REQUIRED, &dwValue, psMachineName, true, 0 );
  161. }
  162. long RegQueryLoggingLevel( DWORD& dwLoggingLevel, LPTSTR psMachineName /*= NULL*/ )
  163. {
  164. return RegQueryDWORD( POP3SERVER_SOFTWARE_SUBKEY, VALUENAME_LOGGINGLEVEL, &dwLoggingLevel, psMachineName );
  165. }
  166. long RegQueryPort( DWORD& dwPort, LPTSTR psMachineName /*= NULL*/ )
  167. {
  168. return RegQueryDWORD( POP3SERVICE_SERVICES_SUBKEY, VALUENAME_PORT, &dwPort, psMachineName );
  169. }
  170. long RegQuerySocketBacklog( DWORD& dwBacklog, LPTSTR psMachineName /*= NULL*/ )
  171. {
  172. return RegQueryDWORD( POP3SERVICE_SERVICES_SUBKEY, VALUENAME_BACKLOG, &dwBacklog, psMachineName );
  173. }
  174. long RegQuerySocketMax( DWORD& dwMax, LPTSTR psMachineName /*= NULL*/ )
  175. {
  176. return RegQueryDWORD( POP3SERVICE_SERVICES_SUBKEY, VALUENAME_MAX, &dwMax, psMachineName );
  177. }
  178. long RegQuerySocketMin( DWORD& dwMin, LPTSTR psMachineName /*= NULL*/ )
  179. {
  180. return RegQueryDWORD( POP3SERVICE_SERVICES_SUBKEY, VALUENAME_MIN, &dwMin, psMachineName );
  181. }
  182. long RegQuerySocketThreshold( DWORD& dwThreshold, LPTSTR psMachineName /*= NULL*/ )
  183. {
  184. return RegQueryDWORD( POP3SERVICE_SERVICES_SUBKEY, VALUENAME_THRESHOLD, &dwThreshold, psMachineName );
  185. }
  186. long RegQueryThreadCountPerCPU( DWORD& dwCount, LPTSTR psMachineName /*= NULL*/ )
  187. {
  188. return RegQueryDWORD( POP3SERVICE_SERVICES_SUBKEY, VALUENAME_THREADCOUNT, &dwCount, psMachineName );
  189. }
  190. long RegQueryCreateUser( DWORD& dwCreateUser, LPTSTR psMachineName /*= NULL*/ )
  191. {
  192. return RegQueryDWORD( POP3SERVER_SOFTWARE_SUBKEY, VALUENAME_CREATE_USER, &dwCreateUser, psMachineName );
  193. }
  194. long RegQueryVersion( DWORD& dwVersion, LPTSTR psMachineName /*= NULL*/ )
  195. {
  196. return RegQueryDWORD( POP3SERVER_SOFTWARE_SUBKEY, VALUENAME_VERSION, &dwVersion, psMachineName );
  197. }
  198. long RegSetDWORD( LPCTSTR lpSubKey, LPCTSTR lpValueName, DWORD dwValue, LPTSTR psMachineName /*= NULL*/ )
  199. {
  200. HKEY hKey;
  201. DWORD dwType = REG_DWORD;
  202. DWORD dwSize = sizeof(DWORD);
  203. long lRC;
  204. lRC = RegHKLMOpenKey( lpSubKey, KEY_SET_VALUE, &hKey, psMachineName );
  205. if ( ERROR_SUCCESS == lRC )
  206. {
  207. lRC = RegSetValueEx( hKey, lpValueName, 0, dwType, reinterpret_cast<LPBYTE>( &dwValue ), dwSize );
  208. RegCloseKey( hKey );
  209. }
  210. return lRC;
  211. }
  212. long RegSetAuthGuid( LPTSTR psAuthGuid, LPTSTR psMachineName /*= NULL*/ )
  213. {
  214. assert(!( NULL == psAuthGuid ));
  215. if ( NULL == psAuthGuid )
  216. return ERROR_INVALID_PARAMETER;
  217. HKEY hKey;
  218. DWORD dwType = REG_SZ;
  219. DWORD dwSize = (wcslen( psAuthGuid ) +1) * sizeof( WCHAR );
  220. long lRC;
  221. lRC = RegHKLMOpenKey( POP3SERVER_AUTH_SUBKEY, KEY_SET_VALUE, &hKey, psMachineName );
  222. if ( ERROR_SUCCESS == lRC )
  223. {
  224. lRC = RegSetValueEx( hKey, VALUENAME_AUTHGUID, 0, dwType, reinterpret_cast<LPBYTE>( psAuthGuid ), dwSize );
  225. RegCloseKey( hKey );
  226. }
  227. return lRC;
  228. }
  229. long RegSetMailRoot( LPTSTR psMailRoot, LPTSTR psMachineName /*= NULL*/ )
  230. {
  231. assert(!( NULL == psMailRoot ));
  232. if ( NULL == psMailRoot )
  233. return ERROR_INVALID_PARAMETER;
  234. HKEY hKey;
  235. DWORD dwType = REG_SZ;
  236. DWORD dwSize = (wcslen( psMailRoot ) +1) * sizeof( WCHAR );
  237. long lRC;
  238. lRC = RegHKLMOpenKey( POP3SERVER_SOFTWARE_SUBKEY, KEY_SET_VALUE, &hKey, psMachineName );
  239. if ( ERROR_SUCCESS == lRC )
  240. {
  241. lRC = RegSetValueEx( hKey, VALUENAME_MAILROOT, 0, dwType, reinterpret_cast<LPBYTE>( psMailRoot ), dwSize );
  242. RegCloseKey( hKey );
  243. }
  244. return lRC;
  245. }
  246. long RegSetGreeting( LPTSTR psGreeting, LPTSTR psMachineName /*= NULL*/ )
  247. {
  248. assert(!( NULL == psGreeting ));
  249. if ( NULL == psGreeting )
  250. return ERROR_INVALID_PARAMETER;
  251. HKEY hKey;
  252. DWORD dwType = REG_SZ;
  253. DWORD dwSize = ( wcslen( psGreeting ) +1 ) * sizeof( WCHAR );
  254. long lRC;
  255. lRC = RegHKLMOpenKey( POP3SERVER_SOFTWARE_SUBKEY, KEY_SET_VALUE, &hKey, psMachineName );
  256. if ( ERROR_SUCCESS == lRC )
  257. {
  258. lRC = RegSetValueEx( hKey, VALUENAME_GREETING, 0, dwType, reinterpret_cast<LPBYTE>( psGreeting ), dwSize );
  259. RegCloseKey( hKey );
  260. }
  261. return lRC;
  262. }
  263. long RegSetAuthMethod( DWORD dwValue, LPTSTR psMachineName /*= NULL*/ )
  264. {
  265. return RegSetDWORD( POP3SERVER_AUTH_SUBKEY, VALUENAME_DEFAULTAUTH, dwValue, psMachineName );
  266. }
  267. long RegSetConfirmAddUser( DWORD dwValue, LPTSTR psMachineName /*= NULL*/ )
  268. {
  269. return RegSetDWORD( POP3SERVER_SOFTWARE_SUBKEY, VALUENAME_CONFIRM_ADDUSER, dwValue, psMachineName );
  270. }
  271. long RegSetSPARequired( DWORD dwValue, LPTSTR psMachineName /*= NULL*/ )
  272. {
  273. return RegSetDWORD( POP3SERVER_SOFTWARE_SUBKEY, VALUENAME_SPA_REQUIRED , dwValue, psMachineName );
  274. }
  275. long RegSetLoggingLevel( DWORD dwValue, LPTSTR psMachineName /*= NULL*/ )
  276. {
  277. return RegSetDWORD( POP3SERVER_SOFTWARE_SUBKEY, VALUENAME_LOGGINGLEVEL, dwValue, psMachineName );
  278. }
  279. long RegSetPort( DWORD dwValue, LPTSTR psMachineName /*= NULL*/ )
  280. {
  281. return RegSetDWORD( POP3SERVICE_SERVICES_SUBKEY, VALUENAME_PORT, dwValue, psMachineName );
  282. }
  283. long RegSetSocketBacklog( DWORD dwValue, LPTSTR psMachineName /*= NULL*/ )
  284. {
  285. return RegSetDWORD( POP3SERVICE_SERVICES_SUBKEY, VALUENAME_BACKLOG, dwValue, psMachineName );
  286. }
  287. long RegSetSocketMax( DWORD dwValue, LPTSTR psMachineName /*= NULL*/ )
  288. {
  289. return RegSetDWORD( POP3SERVICE_SERVICES_SUBKEY, VALUENAME_MAX, dwValue, psMachineName );
  290. }
  291. long RegSetSocketMin( DWORD dwValue, LPTSTR psMachineName /*= NULL*/ )
  292. {
  293. return RegSetDWORD( POP3SERVICE_SERVICES_SUBKEY, VALUENAME_MIN, dwValue, psMachineName );
  294. }
  295. long RegSetSocketThreshold( DWORD dwValue, LPTSTR psMachineName /*= NULL*/ )
  296. {
  297. return RegSetDWORD( POP3SERVICE_SERVICES_SUBKEY, VALUENAME_THRESHOLD, dwValue, psMachineName );
  298. }
  299. long RegSetThreadCount( DWORD dwValue, LPTSTR psMachineName /*= NULL*/ )
  300. {
  301. return RegSetDWORD( POP3SERVICE_SERVICES_SUBKEY, VALUENAME_THREADCOUNT, dwValue, psMachineName );
  302. }
  303. long RegSetCreateUser( DWORD dwCreateUser, LPTSTR psMachineName /*= NULL*/ )
  304. {
  305. return RegSetDWORD( POP3SERVER_SOFTWARE_SUBKEY, VALUENAME_CREATE_USER, dwCreateUser, psMachineName );
  306. }
  307. long RegSetAuthValues()
  308. {
  309. HKEY hKey, hKeyAuth;
  310. long lRC;
  311. DWORD dwDefaultAuth=0;
  312. WCHAR wBuffer[]=_T("14f1665c-e3d3-46aa-884f-ed4cf19d7ad5\0")
  313. _T("ef9d811e-36c5-497f-ade7-2b36df172824\0")
  314. _T("c395e20c-2236-4af7-b736-54fad07dc526\0")
  315. _T("7c295e55-aab1-466d-b589-526fa0ebc397\0");
  316. DWORD cbBufSize=sizeof(wBuffer);
  317. lRC = RegOpenKeyEx( HKEY_LOCAL_MACHINE, POP3SERVER_SOFTWARE_SUBKEY, 0, KEY_WRITE, &hKey );
  318. if( ERROR_SUCCESS == lRC )
  319. {
  320. lRC = RegCreateKeyEx( hKey, POP3AUTH_SUBKEY, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKeyAuth, NULL );
  321. if( ERROR_SUCCESS == lRC )
  322. {
  323. lRC = RegSetValueEx( hKeyAuth,
  324. VALUENAME_AUTHMETHODS,
  325. NULL,
  326. REG_MULTI_SZ,
  327. (LPBYTE)wBuffer,
  328. cbBufSize );
  329. if(ERROR_SUCCESS == lRC)
  330. {
  331. lRC = RegSetValueEx( hKeyAuth,
  332. VALUENAME_DEFAULTAUTH,
  333. NULL,
  334. REG_DWORD,
  335. (LPBYTE)&dwDefaultAuth,
  336. sizeof(DWORD) );
  337. }
  338. if(ERROR_SUCCESS == lRC )
  339. {
  340. UUID uuid;
  341. WCHAR *wszUuid=NULL;
  342. lRC=UuidCreate(&uuid);
  343. if(RPC_S_OK == lRC )
  344. {
  345. lRC=UuidToStringW(&uuid, &wszUuid);
  346. if(RPC_S_OK== lRC )
  347. {
  348. lRC = RegSetValueEx(hKeyAuth,
  349. VALUENAME_AUTHGUID,
  350. NULL,
  351. REG_SZ,
  352. (LPBYTE)wszUuid,
  353. sizeof(WCHAR)*(wcslen(wszUuid)+1) );
  354. RpcStringFreeW(&wszUuid);
  355. }
  356. }
  357. }
  358. RegCloseKey( hKeyAuth);
  359. }
  360. RegCloseKey( hKey );
  361. }
  362. return lRC;
  363. }
  364. long RegSetEventLogKeys()
  365. {
  366. long lRC;
  367. WCHAR wszPath[MAX_PATH]=L"";
  368. HKEY hKey, hKey2;
  369. DWORD dwTypes=7;
  370. DWORD cbPathSize=0;
  371. if(GetCurrentDirectory(MAX_PATH, wszPath))
  372. {
  373. wcscat(wszPath, WSZ_EVENTLOG_FILE_NAME);
  374. cbPathSize= (wcslen(wszPath)+1)*sizeof(WCHAR);
  375. lRC=ERROR_SUCCESS;
  376. }
  377. else
  378. {
  379. lRC=GetLastError();
  380. }
  381. if( ERROR_SUCCESS == lRC )
  382. {
  383. lRC = RegOpenKeyEx( HKEY_LOCAL_MACHINE, EVENTLOG_KEY, 0, KEY_WRITE, &hKey );
  384. if( ERROR_SUCCESS == lRC )
  385. {
  386. lRC = RegCreateKeyEx( hKey, POP3SERVICE_SUBKEY, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey2, NULL );
  387. if( ERROR_SUCCESS == lRC )
  388. {
  389. lRC = RegSetValueEx( hKey2,
  390. VALUENAME_EVENTMSGFILE,
  391. NULL,
  392. REG_SZ,
  393. (LPBYTE)wszPath,
  394. cbPathSize );
  395. if(ERROR_SUCCESS == lRC)
  396. {
  397. lRC = RegSetValueEx(hKey2,
  398. VALUENAME_TYPESSUPPORTED,
  399. NULL,
  400. REG_DWORD,
  401. (LPBYTE)&dwTypes,
  402. sizeof(DWORD) );
  403. }
  404. RegCloseKey( hKey2);
  405. }
  406. RegCloseKey( hKey );
  407. }
  408. }
  409. return lRC;
  410. }
  411. long RegSetup()
  412. {
  413. HKEY hKey, hKeyPOP3;
  414. long lRC;
  415. // Create POP3SERVER_SOFTWARE_SUBKEY
  416. lRC = RegOpenKeyEx( HKEY_LOCAL_MACHINE, _T("Software\\Microsoft"), 0, KEY_WRITE, &hKey );
  417. if( ERROR_SUCCESS == lRC )
  418. {
  419. lRC = RegCreateKeyEx( hKey, POP3SERVER_SUBKEY, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKeyPOP3, NULL );
  420. if( ERROR_SUCCESS == lRC )
  421. RegCloseKey( hKeyPOP3 );
  422. RegCloseKey( hKey );
  423. }
  424. // Create POP3SERVICE_SERVICES_SUBKEY
  425. if( ERROR_SUCCESS == lRC )
  426. {
  427. lRC = RegOpenKeyEx( HKEY_LOCAL_MACHINE, _T("System\\CurrentControlSet\\Services"), 0, KEY_WRITE, &hKey );
  428. if( ERROR_SUCCESS == lRC )
  429. {
  430. lRC = RegCreateKeyEx( hKey, POP3SERVICE_SUBKEY, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKeyPOP3, NULL );
  431. if( ERROR_SUCCESS == lRC )
  432. RegCloseKey( hKeyPOP3 );
  433. RegCloseKey( hKey );
  434. }
  435. }
  436. // Create the Auth key and values
  437. if( ERROR_SUCCESS == lRC )
  438. {
  439. lRC = RegSetAuthValues();
  440. }
  441. if( ERROR_SUCCESS == lRC )
  442. {
  443. lRC = RegSetEventLogKeys();
  444. }
  445. return lRC;
  446. }
  447. long RegSetupOCM()
  448. {
  449. HKEY hKey;
  450. long lRC;
  451. TCHAR sBuffer[MAX_PATH];
  452. DWORD dwPathSize;
  453. WCHAR wszSDL[MAX_PATH]=L"O:BAG:BAD:PAI(A;OICI;GA;;;BA)(A;OICIIO;GA;;;CO)(A;OICI;KR;;;NS)(A;OICI;KR;;;SY)";
  454. PSECURITY_DESCRIPTOR pSD=NULL;
  455. ULONG lSize=0;
  456. if(!ConvertStringSecurityDescriptorToSecurityDescriptorW(
  457. wszSDL,
  458. SDDL_REVISION_1,
  459. &pSD,
  460. &lSize))
  461. {
  462. lRC = GetLastError();
  463. }
  464. // EventLogKeys
  465. ZeroMemory(sBuffer, sizeof(sBuffer));
  466. lRC = GetModuleFileName( GetModuleHandle( P3ADMIN_MODULENAME ), sBuffer, sizeof(sBuffer)/sizeof(TCHAR) -1 );
  467. if ( 0 < lRC )
  468. {
  469. // Strip off the module file name and replace with pop3evt.dll
  470. LPTSTR ps = _tcsrchr( sBuffer, _T( '\\' ));
  471. if ( NULL != ps )
  472. {
  473. _tcscpy( ps, WSZ_EVENTLOG_FILE_NAME );
  474. dwPathSize = ( _tcslen( sBuffer ) + 1 ) * sizeof(TCHAR);
  475. lRC = ERROR_SUCCESS;
  476. }
  477. else
  478. lRC = ERROR_PATH_NOT_FOUND;
  479. if( ERROR_SUCCESS == lRC )
  480. { // Pop3Svc
  481. lRC = RegOpenKeyEx( HKEY_LOCAL_MACHINE, POP3SERVICE_EVENTLOG_KEY, 0, KEY_WRITE, &hKey );
  482. if( ERROR_SUCCESS == lRC )
  483. {
  484. lRC = RegSetValueEx( hKey, VALUENAME_EVENTMSGFILE, NULL, REG_SZ, reinterpret_cast<LPBYTE>( sBuffer ), dwPathSize );
  485. if( ERROR_SUCCESS == lRC )
  486. lRC = RegSetValueEx( hKey, VALUENAME_CATEGORYMSGFILE, NULL, REG_SZ, reinterpret_cast<LPBYTE>( sBuffer ), dwPathSize );
  487. RegCloseKey( hKey );
  488. }
  489. }
  490. if( ERROR_SUCCESS == lRC )
  491. { // POP3 Server
  492. lRC = RegOpenKeyEx( HKEY_LOCAL_MACHINE, POP3SERVER_EVENTLOG_KEY, 0, KEY_WRITE, &hKey );
  493. if( ERROR_SUCCESS == lRC )
  494. {
  495. lRC = RegSetValueEx( hKey, VALUENAME_EVENTMSGFILE, NULL, REG_SZ, reinterpret_cast<LPBYTE>( sBuffer ), dwPathSize );
  496. if( ERROR_SUCCESS == lRC )
  497. lRC = RegSetValueEx( hKey, VALUENAME_CATEGORYMSGFILE, NULL, REG_SZ, reinterpret_cast<LPBYTE>( sBuffer ), dwPathSize );
  498. RegCloseKey( hKey );
  499. }
  500. }
  501. if( ERROR_SUCCESS == lRC )
  502. { // POP3 Server
  503. _tcscpy( ps, WSZ_PERFDLL_FILE_NAME );
  504. dwPathSize = ( _tcslen( sBuffer ) + 1 ) * sizeof(TCHAR);
  505. lRC = RegOpenKeyEx( HKEY_LOCAL_MACHINE, POP3SERVICE_SERVICES_PERF_SUBKEY, 0, KEY_WRITE, &hKey );
  506. if( ERROR_SUCCESS == lRC )
  507. {
  508. lRC = RegSetValueEx( hKey, VALUENAME_PERF_LIBRARY, NULL, REG_SZ, reinterpret_cast<LPBYTE>( sBuffer ), dwPathSize );
  509. RegCloseKey( hKey );
  510. }
  511. }
  512. }
  513. else
  514. lRC = ERROR_PATH_NOT_FOUND;
  515. // Auth GUID
  516. if ( ERROR_SUCCESS == lRC )
  517. lRC = RegOpenKeyEx( HKEY_LOCAL_MACHINE, POP3SERVER_AUTH_SUBKEY, 0, KEY_ALL_ACCESS, &hKey );
  518. if ( ERROR_SUCCESS == lRC )
  519. {
  520. UUID uuid;
  521. TCHAR *szUuid=NULL;
  522. lRC = UuidCreate(&uuid);
  523. if(RPC_S_OK == lRC )
  524. {
  525. lRC = UuidToString(&uuid, &szUuid);
  526. if( RPC_S_OK == lRC )
  527. {
  528. lRC = RegSetValueEx( hKey, VALUENAME_AUTHGUID, NULL, REG_SZ, (LPBYTE)szUuid, sizeof(TCHAR)*(_tcslen(szUuid)+1) );
  529. RpcStringFree(&szUuid);
  530. }
  531. if(ERROR_SUCCESS == lRC)
  532. { //Set the ACLs for the AUTH key
  533. lRC = RegSetKeySecurity( hKey, DACL_SECURITY_INFORMATION, pSD );
  534. }
  535. RegCloseKey( hKey );
  536. }
  537. }
  538. if( ERROR_SUCCESS == lRC )
  539. {
  540. lRC = RegOpenKeyEx( HKEY_LOCAL_MACHINE, POP3SERVICE_SERVICES_SUBKEY, 0, KEY_ALL_ACCESS, &hKey );
  541. if( ERROR_SUCCESS == lRC )
  542. {
  543. // Set ACLs for pop3 service key
  544. lRC = RegSetKeySecurity( hKey, DACL_SECURITY_INFORMATION, pSD );
  545. }
  546. RegCloseKey(hKey);
  547. }
  548. if( ERROR_SUCCESS == lRC )
  549. {
  550. //Set default auth method to AD if the box is a DC
  551. DSROLE_PRIMARY_DOMAIN_INFO_BASIC *pMachineRole=NULL;
  552. //Check the Role of the machine
  553. if( ERROR_SUCCESS== (lRC=
  554. DsRoleGetPrimaryDomainInformation(
  555. NULL,
  556. DsRolePrimaryDomainInfoBasic,
  557. (PBYTE *)(&pMachineRole))) )
  558. {
  559. if(pMachineRole->MachineRole == DsRole_RoleBackupDomainController ||
  560. pMachineRole->MachineRole == DsRole_RolePrimaryDomainController||
  561. pMachineRole->MachineRole == DsRole_RoleMemberServer )
  562. {
  563. //This is DC or a member server, set default auth to AD (1)
  564. // 0:SAM 1:AD 2:Encrypted Password
  565. lRC = RegSetDWORD(POP3SERVER_AUTH_SUBKEY, VALUENAME_DEFAULTAUTH, 1 );
  566. }
  567. DsRoleFreeMemory(pMachineRole);
  568. }
  569. }
  570. if( ERROR_SUCCESS == lRC )
  571. {
  572. lRC = RegOpenKeyEx( HKEY_LOCAL_MACHINE, POP3SERVER_SOFTWARE_SUBKEY, 0, KEY_ALL_ACCESS, &hKey );
  573. if( ERROR_SUCCESS == lRC )
  574. {
  575. // Set ACLs for pop3 server key
  576. lRC = RegSetKeySecurity( hKey, DACL_SECURITY_INFORMATION, pSD );
  577. // Create InstallDir and ConsoleFile values
  578. if( ERROR_SUCCESS == lRC )
  579. {
  580. TCHAR sBuffer[MAX_PATH+1]=_T("");
  581. if( 0==GetSystemDirectory(sBuffer, sizeof(sBuffer)/sizeof(TCHAR)) )
  582. {
  583. lRC = GetLastError();
  584. }
  585. if( ERROR_SUCCESS == lRC )
  586. {
  587. lRC = RegSetValueEx( hKey, VALUENAME_CONSOLE_FILE, NULL, REG_SZ, (LPBYTE)sBuffer, sizeof(TCHAR)*(_tcslen(sBuffer)+1) );
  588. }
  589. if( ERROR_SUCCESS == lRC )
  590. {
  591. if(_tcslen(sBuffer)+_tcslen(WSZ_POP3_SERVER_DIR) <= MAX_PATH)
  592. {
  593. _tcscat(sBuffer, WSZ_POP3_SERVER_DIR);
  594. lRC = RegSetValueEx( hKey, VALUENAME_INSTALL_DIR, NULL, REG_SZ, (LPBYTE)sBuffer, sizeof(TCHAR)*(_tcslen(sBuffer)+1) );
  595. }
  596. else
  597. {
  598. lRC = ERROR_BAD_ENVIRONMENT;
  599. }
  600. }
  601. }
  602. RegCloseKey(hKey);
  603. }
  604. }
  605. if(pSD)
  606. {
  607. LocalFree(pSD);
  608. }
  609. return lRC;
  610. }