nt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. win32/nt.c - Zip 3
  3. Copyright (c) 1990-2007 Info-ZIP. All rights reserved.
  4. See the accompanying file LICENSE, version 2007-Mar-4 or later
  5. (the contents of which are also included in zip.h) for terms of use.
  6. If, for some reason, all these files are missing, the Info-ZIP license
  7. also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
  8. */
  9. /*++
  10. Copyright (c) 1996 Scott Field
  11. Module Name:
  12. nt.c (formerly nt_zip.c)
  13. Abstract:
  14. This module implements WinNT security descriptor operations for the
  15. Win32 Info-ZIP project. Operation such as querying file security,
  16. using/querying local and remote privileges. The contents of this module
  17. are only relevant when the code is running on Windows NT, and the target
  18. volume supports persistent Acl storage.
  19. User privileges that allow accessing certain privileged aspects of the
  20. security descriptor (such as the Sacl) are only used if the user specified
  21. to do so.
  22. In the future, this module may be expanded to support storage of
  23. OS/2 EA data, Macintosh resource forks, and hard links, which are all
  24. supported by NTFS.
  25. Author:
  26. Scott Field (sfield@microsoft.com) 27-Sep-96
  27. --*/
  28. #include "../zip.h"
  29. #define WIN32_LEAN_AND_MEAN
  30. #include <windows.h>
  31. #ifdef __RSXNT__
  32. # include "../win32/rsxntwin.h"
  33. #endif
  34. #include "../win32/nt.h"
  35. #ifdef NTSD_EAS /* This file is only needed for NTSD handling */
  36. /* Borland C++ does not define FILE_SHARE_DELETE. Others also? */
  37. #ifndef FILE_SHARE_DELETE
  38. # define FILE_SHARE_DELETE 0x00000004
  39. #endif
  40. /* This macro definition is missing in old versions of MS' winbase.h. */
  41. #ifndef InterlockedExchangePointer
  42. # define InterlockedExchangePointer(Target, Value) \
  43. (PVOID)InterlockedExchange((PLONG)(Target), (LONG)(Value))
  44. #endif
  45. /* private prototypes */
  46. static BOOL Initialize(VOID);
  47. #if 0 /* currently unused */
  48. static BOOL Shutdown(VOID);
  49. #endif
  50. static VOID GetRemotePrivilegesGet(CHAR *FileName, PDWORD dwRemotePrivileges);
  51. static VOID InitLocalPrivileges(VOID);
  52. BOOL bZipInitialized = FALSE; /* module level stuff initialized? */
  53. HANDLE hZipInitMutex = NULL; /* prevent multiple initialization */
  54. BOOL g_bBackupPrivilege = FALSE; /* for local get file security override */
  55. BOOL g_bZipSaclPrivilege = FALSE; /* for local get sacl operations, only when
  56. backup privilege not present */
  57. /* our single cached volume capabilities structure that describes the last
  58. volume root we encountered. A single entry like this works well in the
  59. zip/unzip scenario for a number of reasons:
  60. 1. typically one extraction path during unzip.
  61. 2. typically process one volume at a time during zip, and then move
  62. on to the next.
  63. 3. no cleanup code required and no memory leaks.
  64. 4. simple code.
  65. This approach should be reworked to a linked list approach if we expect to
  66. be called by many threads which are processing a variety of input/output
  67. volumes, since lock contention and stale data may become a bottleneck. */
  68. VOLUMECAPS g_VolumeCaps;
  69. CRITICAL_SECTION VolumeCapsLock;
  70. static BOOL Initialize(VOID)
  71. {
  72. HANDLE hMutex;
  73. HANDLE hOldMutex;
  74. if(bZipInitialized) return TRUE;
  75. hMutex = CreateMutex(NULL, TRUE, NULL);
  76. if(hMutex == NULL) return FALSE;
  77. hOldMutex = (HANDLE)InterlockedExchangePointer((void *)&hZipInitMutex,
  78. hMutex);
  79. if(hOldMutex != NULL) {
  80. /* somebody setup the mutex already */
  81. InterlockedExchangePointer((void *)&hZipInitMutex,
  82. hOldMutex);
  83. CloseHandle(hMutex); /* close new, un-needed mutex */
  84. /* wait for initialization to complete and return status */
  85. WaitForSingleObject(hOldMutex, INFINITE);
  86. ReleaseMutex(hOldMutex);
  87. return bZipInitialized;
  88. }
  89. /* initialize module level resources */
  90. InitializeCriticalSection( &VolumeCapsLock );
  91. memset(&g_VolumeCaps, 0, sizeof(VOLUMECAPS));
  92. InitLocalPrivileges();
  93. bZipInitialized = TRUE;
  94. ReleaseMutex(hMutex); /* release correct mutex */
  95. return TRUE;
  96. }
  97. #if 0 /* currently not used ! */
  98. static BOOL Shutdown(VOID)
  99. {
  100. /* really need to free critical sections, disable enabled privilges, etc,
  101. but doing so brings up possibility of race conditions if those resources
  102. are about to be used. The easiest way to handle this is let these
  103. resources be freed when the process terminates... */
  104. return TRUE;
  105. }
  106. #endif /* never */
  107. static VOID GetRemotePrivilegesGet(char *FileName, PDWORD dwRemotePrivileges)
  108. {
  109. HANDLE hFile;
  110. *dwRemotePrivileges = 0;
  111. /* see if we have the SeBackupPrivilege */
  112. hFile = CreateFileA(
  113. FileName,
  114. ACCESS_SYSTEM_SECURITY | GENERIC_READ | READ_CONTROL,
  115. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  116. NULL,
  117. OPEN_EXISTING,
  118. FILE_FLAG_BACKUP_SEMANTICS,
  119. NULL
  120. );
  121. if(hFile != INVALID_HANDLE_VALUE) {
  122. /* no remote way to determine SeBackupPrivilege -- just try a read
  123. to simulate it */
  124. SECURITY_INFORMATION si = DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION;
  125. PSECURITY_DESCRIPTOR sd;
  126. DWORD cbBuf = 0;
  127. GetKernelObjectSecurity(hFile, si, NULL, cbBuf, &cbBuf);
  128. if(ERROR_INSUFFICIENT_BUFFER == GetLastError()) {
  129. if((sd = HeapAlloc(GetProcessHeap(), 0, cbBuf)) != NULL) {
  130. if(GetKernelObjectSecurity(hFile, si, sd, cbBuf, &cbBuf)) {
  131. *dwRemotePrivileges |= OVERRIDE_BACKUP;
  132. }
  133. HeapFree(GetProcessHeap(), 0, sd);
  134. }
  135. }
  136. CloseHandle(hFile);
  137. } else {
  138. /* see if we have the SeSecurityPrivilege */
  139. /* note we don't need this if we have SeBackupPrivilege */
  140. hFile = CreateFileA(
  141. FileName,
  142. ACCESS_SYSTEM_SECURITY,
  143. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, /* maximum sharing */
  144. NULL,
  145. OPEN_EXISTING,
  146. 0,
  147. NULL
  148. );
  149. if(hFile != INVALID_HANDLE_VALUE) {
  150. CloseHandle(hFile);
  151. *dwRemotePrivileges |= OVERRIDE_SACL;
  152. }
  153. }
  154. }
  155. BOOL ZipGetVolumeCaps(
  156. char *rootpath, /* filepath, or NULL */
  157. char *name, /* filename associated with rootpath */
  158. PVOLUMECAPS VolumeCaps /* result structure describing capabilities */
  159. )
  160. {
  161. char TempRootPath[MAX_PATH + 1];
  162. DWORD cchTempRootPath = 0;
  163. BOOL bSuccess = TRUE; /* assume success until told otherwise */
  164. if(!bZipInitialized) if(!Initialize()) return FALSE;
  165. /* process the input path to produce a consistent path suitable for
  166. compare operations and also suitable for certain picky Win32 API
  167. that don't like forward slashes */
  168. if(rootpath != NULL && rootpath[0] != '\0') {
  169. DWORD i;
  170. cchTempRootPath = lstrlen(rootpath);
  171. if(cchTempRootPath > MAX_PATH) return FALSE;
  172. /* copy input, converting forward slashes to back slashes as we go */
  173. for(i = 0 ; i <= cchTempRootPath ; i++) {
  174. if(rootpath[i] == '/') TempRootPath[i] = '\\';
  175. else TempRootPath[i] = rootpath[i];
  176. }
  177. /* check for UNC and Null terminate or append trailing \ as appropriate */
  178. /* possible valid UNCs we are passed follow:
  179. \\machine\foo\bar (path is \\machine\foo\)
  180. \\machine\foo (path is \\machine\foo\)
  181. \\machine\foo\
  182. \\.\c$\ (FIXFIX: Win32API doesn't like this - GetComputerName())
  183. LATERLATER: handling mounted DFS drives in the future will require
  184. slightly different logic which isn't available today.
  185. This is required because directories can point at
  186. different servers which have differing capabilities.
  187. */
  188. if(TempRootPath[0] == '\\' && TempRootPath[1] == '\\') {
  189. DWORD slash = 0;
  190. for(i = 2 ; i < cchTempRootPath ; i++) {
  191. if(TempRootPath[i] == '\\') {
  192. slash++;
  193. if(slash == 2) {
  194. i++;
  195. TempRootPath[i] = '\0';
  196. cchTempRootPath = i;
  197. break;
  198. }
  199. }
  200. }
  201. /* if there was only one slash found, just tack another onto the end */
  202. if(slash == 1 && TempRootPath[cchTempRootPath] != '\\') {
  203. TempRootPath[cchTempRootPath] = TempRootPath[0]; /* '\' */
  204. TempRootPath[cchTempRootPath+1] = '\0';
  205. cchTempRootPath++;
  206. }
  207. } else {
  208. if(TempRootPath[1] == ':') {
  209. /* drive letter specified, truncate to root */
  210. TempRootPath[2] = '\\';
  211. TempRootPath[3] = '\0';
  212. cchTempRootPath = 3;
  213. } else {
  214. /* must be file on current drive */
  215. TempRootPath[0] = '\0';
  216. cchTempRootPath = 0;
  217. }
  218. }
  219. } /* if path != NULL */
  220. /* grab lock protecting cached entry */
  221. EnterCriticalSection( &VolumeCapsLock );
  222. if(!g_VolumeCaps.bValid || lstrcmpi(g_VolumeCaps.RootPath, TempRootPath) != 0) {
  223. /* no match found, build up new entry */
  224. DWORD dwFileSystemFlags;
  225. DWORD dwRemotePrivileges = 0;
  226. BOOL bRemote = FALSE;
  227. /* release lock during expensive operations */
  228. LeaveCriticalSection( &VolumeCapsLock );
  229. bSuccess = GetVolumeInformation(
  230. (TempRootPath[0] == '\0') ? NULL : TempRootPath,
  231. NULL, 0,
  232. NULL, NULL,
  233. &dwFileSystemFlags,
  234. NULL, 0);
  235. /* only if target volume supports Acls, and we were told to use
  236. privileges do we need to go out and test for the remote case */
  237. if(bSuccess && (dwFileSystemFlags & FS_PERSISTENT_ACLS) && VolumeCaps->bUsePrivileges) {
  238. if(GetDriveType( (TempRootPath[0] == '\0') ? NULL : TempRootPath ) == DRIVE_REMOTE) {
  239. bRemote = TRUE;
  240. /* make a determination about our remote capabilities */
  241. GetRemotePrivilegesGet(name, &dwRemotePrivileges);
  242. }
  243. }
  244. /* always take the lock again, since we release it below */
  245. EnterCriticalSection( &VolumeCapsLock );
  246. /* replace the existing data if successful */
  247. if(bSuccess) {
  248. lstrcpynA(g_VolumeCaps.RootPath, TempRootPath, cchTempRootPath+1);
  249. g_VolumeCaps.bProcessDefer = FALSE;
  250. g_VolumeCaps.dwFileSystemFlags = dwFileSystemFlags;
  251. g_VolumeCaps.bRemote = bRemote;
  252. g_VolumeCaps.dwRemotePrivileges = dwRemotePrivileges;
  253. g_VolumeCaps.bValid = TRUE;
  254. }
  255. }
  256. if(bSuccess) {
  257. /* copy input elements */
  258. g_VolumeCaps.bUsePrivileges = VolumeCaps->bUsePrivileges;
  259. g_VolumeCaps.dwFileAttributes = VolumeCaps->dwFileAttributes;
  260. /* give caller results */
  261. memcpy(VolumeCaps, &g_VolumeCaps, sizeof(VOLUMECAPS));
  262. } else {
  263. g_VolumeCaps.bValid = FALSE;
  264. }
  265. LeaveCriticalSection( &VolumeCapsLock ); /* release lock */
  266. return bSuccess;
  267. }
  268. BOOL SecurityGet(
  269. char *resource,
  270. PVOLUMECAPS VolumeCaps,
  271. unsigned char *buffer,
  272. DWORD *cbBuffer
  273. )
  274. {
  275. HANDLE hFile;
  276. DWORD dwDesiredAccess;
  277. DWORD dwFlags;
  278. PSECURITY_DESCRIPTOR sd = (PSECURITY_DESCRIPTOR)buffer;
  279. SECURITY_INFORMATION RequestedInfo;
  280. BOOL bBackupPrivilege = FALSE;
  281. BOOL bSaclPrivilege = FALSE;
  282. BOOL bSuccess = FALSE;
  283. DWORD cchResourceLen;
  284. if(!bZipInitialized) if(!Initialize()) return FALSE;
  285. /* see if we are dealing with a directory */
  286. /* rely on the fact resource has a trailing [back]slash, rather
  287. than calling expensive GetFileAttributes() */
  288. cchResourceLen = lstrlenA(resource);
  289. if(resource[cchResourceLen-1] == '/' || resource[cchResourceLen-1] == '\\')
  290. VolumeCaps->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
  291. /* setup privilege usage based on if told we can use privileges, and if so,
  292. what privileges we have */
  293. if(VolumeCaps->bUsePrivileges) {
  294. if(VolumeCaps->bRemote) {
  295. /* use remotely determined privileges */
  296. if(VolumeCaps->dwRemotePrivileges & OVERRIDE_BACKUP)
  297. bBackupPrivilege = TRUE;
  298. if(VolumeCaps->dwRemotePrivileges & OVERRIDE_SACL)
  299. bSaclPrivilege = TRUE;
  300. } else {
  301. /* use local privileges */
  302. bBackupPrivilege = g_bBackupPrivilege;
  303. bSaclPrivilege = g_bZipSaclPrivilege;
  304. }
  305. }
  306. /* always try to read the basic security information: Dacl, Owner, Group */
  307. dwDesiredAccess = READ_CONTROL;
  308. RequestedInfo = OWNER_SECURITY_INFORMATION |
  309. GROUP_SECURITY_INFORMATION |
  310. DACL_SECURITY_INFORMATION;
  311. /* if we have the SeBackupPrivilege or SeSystemSecurityPrivilege, read
  312. the Sacl, too */
  313. if(bBackupPrivilege || bSaclPrivilege) {
  314. dwDesiredAccess |= ACCESS_SYSTEM_SECURITY;
  315. RequestedInfo |= SACL_SECURITY_INFORMATION;
  316. }
  317. dwFlags = 0;
  318. /* if we have the backup privilege, specify that */
  319. /* opening a directory requires FILE_FLAG_BACKUP_SEMANTICS */
  320. if(bBackupPrivilege || (VolumeCaps->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  321. dwFlags |= FILE_FLAG_BACKUP_SEMANTICS;
  322. hFile = CreateFileA(
  323. resource,
  324. dwDesiredAccess,
  325. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, /* maximum sharing */
  326. NULL,
  327. OPEN_EXISTING,
  328. dwFlags,
  329. NULL
  330. );
  331. if(hFile == INVALID_HANDLE_VALUE) return FALSE;
  332. if(GetKernelObjectSecurity(hFile, RequestedInfo, sd, *cbBuffer, cbBuffer)) {
  333. *cbBuffer = GetSecurityDescriptorLength( sd );
  334. bSuccess = TRUE;
  335. }
  336. CloseHandle(hFile);
  337. return bSuccess;
  338. }
  339. static VOID InitLocalPrivileges(VOID)
  340. {
  341. HANDLE hToken;
  342. TOKEN_PRIVILEGES tp;
  343. /* try to enable some interesting privileges that give us the ability
  344. to get some security information that we normally cannot.
  345. note that enabling privileges is only relevant on the local machine;
  346. when accessing files that are on a remote machine, any privileges
  347. that are present on the remote machine get enabled by default. */
  348. if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
  349. return;
  350. tp.PrivilegeCount = 1;
  351. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  352. /* try to enable SeBackupPrivilege.
  353. if this succeeds, we can read all aspects of the security descriptor */
  354. if(LookupPrivilegeValue(NULL, SE_BACKUP_NAME, &tp.Privileges[0].Luid)) {
  355. if(AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL) &&
  356. GetLastError() == ERROR_SUCCESS) g_bBackupPrivilege = TRUE;
  357. }
  358. /* try to enable SeSystemSecurityPrivilege if SeBackupPrivilege not present.
  359. if this succeeds, we can read the Sacl */
  360. if(!g_bBackupPrivilege &&
  361. LookupPrivilegeValue(NULL, SE_SECURITY_NAME, &tp.Privileges[0].Luid)) {
  362. if(AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL) &&
  363. GetLastError() == ERROR_SUCCESS) g_bZipSaclPrivilege = TRUE;
  364. }
  365. CloseHandle(hToken);
  366. }
  367. #endif /* NTSD_EAS */