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.

240 lines
6.5 KiB

  1. /*---------------------------------------------------------------------------
  2. File: CommaLog.cpp
  3. Comments: TError based log file with optional NTFS security initialization.
  4. This can be used to write a log file which only administrators can access.
  5. (c) Copyright 1999, Mission Critical Software, Inc., All Rights Reserved
  6. Proprietary and confidential to Mission Critical Software, Inc.
  7. REVISION LOG ENTRY
  8. Revision By: Christy Boles
  9. Revised on 02/15/99 10:49:07
  10. ---------------------------------------------------------------------------
  11. */
  12. //#include "stdafx.h"
  13. #include <windows.h>
  14. #include <stdio.h>
  15. #include <share.h>
  16. #include <lm.h>
  17. #include "Common.hpp"
  18. #include "UString.hpp"
  19. #include "Err.hpp"
  20. #include "ErrDct.hpp"
  21. #include "sd.hpp"
  22. #include "SecObj.hpp"
  23. #include "CommaLog.hpp"
  24. #include "BkupRstr.hpp"
  25. #define ADMINISTRATORS 1
  26. #define ACCOUNT_OPERATORS 2
  27. #define BACKUP_OPERATORS 3
  28. #define DOMAIN_ADMINS 4
  29. #define CREATOR_OWNER 5
  30. #define USERS 6
  31. #define SYSTEM 7
  32. extern TErrorDct err;
  33. #define BYTE_ORDER_MARK (0xFEFF)
  34. PSID // ret- SID for well-known account
  35. GetWellKnownSid(
  36. DWORD wellKnownAccount // in - one of the constants #defined above for the well-known accounts
  37. )
  38. {
  39. PSID pSid = NULL;
  40. // PUCHAR numsubs = NULL;
  41. // DWORD * rid = NULL;
  42. BOOL error = FALSE;
  43. SID_IDENTIFIER_AUTHORITY sia = SECURITY_NT_AUTHORITY;
  44. SID_IDENTIFIER_AUTHORITY creatorIA = SECURITY_CREATOR_SID_AUTHORITY;
  45. //
  46. // Sid is the same regardless of machine, since the well-known
  47. // BUILTIN domain is referenced.
  48. //
  49. switch ( wellKnownAccount )
  50. {
  51. case CREATOR_OWNER:
  52. if( ! AllocateAndInitializeSid(
  53. &creatorIA,
  54. 2,
  55. SECURITY_BUILTIN_DOMAIN_RID,
  56. SECURITY_CREATOR_OWNER_RID,
  57. 0, 0, 0, 0, 0, 0,
  58. &pSid
  59. ))
  60. {
  61. err.SysMsgWrite(ErrE,GetLastError(),DCT_MSG_INITIALIZE_SID_FAILED_D,GetLastError());
  62. }
  63. break;
  64. case ADMINISTRATORS:
  65. if( ! AllocateAndInitializeSid(
  66. &sia,
  67. 2,
  68. SECURITY_BUILTIN_DOMAIN_RID,
  69. DOMAIN_ALIAS_RID_ADMINS,
  70. 0, 0, 0, 0, 0, 0,
  71. &pSid
  72. ))
  73. {
  74. err.SysMsgWrite(ErrE,GetLastError(),DCT_MSG_INITIALIZE_SID_FAILED_D,GetLastError());
  75. }
  76. break;
  77. case ACCOUNT_OPERATORS:
  78. if( ! AllocateAndInitializeSid(
  79. &sia,
  80. 2,
  81. SECURITY_BUILTIN_DOMAIN_RID,
  82. DOMAIN_ALIAS_RID_ACCOUNT_OPS,
  83. 0, 0, 0, 0, 0, 0,
  84. &pSid
  85. ))
  86. {
  87. err.SysMsgWrite(ErrE,GetLastError(),DCT_MSG_INITIALIZE_SID_FAILED_D,GetLastError());
  88. }
  89. break;
  90. case BACKUP_OPERATORS:
  91. if( ! AllocateAndInitializeSid(
  92. &sia,
  93. 2,
  94. SECURITY_BUILTIN_DOMAIN_RID,
  95. DOMAIN_ALIAS_RID_BACKUP_OPS,
  96. 0, 0, 0, 0, 0, 0,
  97. &pSid
  98. ))
  99. {
  100. err.SysMsgWrite(ErrE,GetLastError(),DCT_MSG_INITIALIZE_SID_FAILED_D,GetLastError());
  101. }
  102. break;
  103. case USERS:
  104. if( ! AllocateAndInitializeSid(
  105. &sia,
  106. 2,
  107. SECURITY_BUILTIN_DOMAIN_RID,
  108. DOMAIN_ALIAS_RID_USERS,
  109. 0, 0, 0, 0, 0, 0,
  110. &pSid
  111. ))
  112. {
  113. err.SysMsgWrite(ErrE,GetLastError(),DCT_MSG_INITIALIZE_SID_FAILED_D,GetLastError());
  114. }
  115. break;
  116. case SYSTEM:
  117. if( ! AllocateAndInitializeSid(
  118. &sia,
  119. 1,
  120. SECURITY_LOCAL_SYSTEM_RID,
  121. 0, 0, 0, 0, 0, 0, 0,
  122. &pSid
  123. ))
  124. {
  125. err.SysMsgWrite(ErrE,GetLastError(),DCT_MSG_INITIALIZE_SID_FAILED_D,GetLastError());
  126. }
  127. break;
  128. default:
  129. MCSASSERT(FALSE);
  130. break;
  131. }
  132. if ( error )
  133. {
  134. FreeSid(pSid);
  135. pSid = NULL;
  136. }
  137. return pSid;
  138. }
  139. BOOL // ret- whether log was successfully opened or not
  140. CommaDelimitedLog::LogOpen(
  141. TCHAR const * filename, // in - name for log file
  142. BOOL protect, // in - if TRUE, try to ACL the file so only admins can access
  143. int mode // in - mode 0=overwrite, 1=append
  144. )
  145. {
  146. BOOL retval=TRUE;
  147. if ( fptr )
  148. {
  149. fclose(fptr);
  150. fptr = NULL;
  151. }
  152. if ( filename && filename[0] )
  153. {
  154. // Check to see if the file already exists
  155. WIN32_FIND_DATA fDat;
  156. HANDLE hFind;
  157. BOOL bExisted = FALSE;
  158. hFind = FindFirstFile(filename,&fDat);
  159. if ( hFind != INVALID_HANDLE_VALUE )
  160. {
  161. FindClose(hFind);
  162. bExisted = TRUE;
  163. }
  164. #ifdef UNICODE
  165. fptr = _wfsopen( filename, mode == 0 ? L"wb" : L"ab", _SH_DENYNO );
  166. #else
  167. fptr = _fsopen( filename, mode == 0 ? "w" : "a", _SH_DENYNO );
  168. #endif
  169. if ( !fptr )
  170. {
  171. retval = FALSE;
  172. }
  173. else
  174. {
  175. if (! bExisted )
  176. {
  177. // this is a new file we've just created
  178. // we need to write the byte order mark to the beginning of the file
  179. WCHAR x = BYTE_ORDER_MARK;
  180. fwprintf(fptr,L"%lc",x);
  181. }
  182. }
  183. }
  184. if ( protect )
  185. {
  186. WCHAR fname[MAX_PATH+1];
  187. safecopy(fname,filename);
  188. if ( GetBkupRstrPriv() )
  189. {
  190. // Set the SD for the file to Administrators Full Control only.
  191. TFileSD sd(fname);
  192. if ( sd.GetSecurity() != NULL )
  193. {
  194. PSID mySid = GetWellKnownSid(ADMINISTRATORS);
  195. TACE ace(ACCESS_ALLOWED_ACE_TYPE,0,DACL_FULLCONTROL_MASK,mySid);
  196. PACL acl = NULL; // start with an empty ACL
  197. sd.GetSecurity()->ACLAddAce(&acl,&ace,-1);
  198. sd.GetSecurity()->SetDacl(acl,TRUE);
  199. sd.WriteSD();
  200. }
  201. }
  202. else
  203. {
  204. err.SysMsgWrite(ErrW,GetLastError(),DCT_MSG_NO_BR_PRIV_SD,fname,GetLastError());
  205. }
  206. }
  207. return retval;
  208. }