xref: /aoo41x/main/sfx2/source/doc/sfxacldetect.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sfx2.hxx"
30 
31 
32 #ifdef WNT
33 
34 // necessary to include system headers without warnings
35 #ifdef _MSC_VER
36 #pragma warning(disable:4668 4917)
37 #endif
38 
39 #include <windows.h>
40 #include <lmaccess.h>
41 #include <sal/types.h>
42 
43 sal_Bool IsReadonlyAccordingACL( const sal_Unicode* pFilePath )
44 {
45     sal_Bool bResult = sal_False;
46 
47     sal_uInt32 nFDSize = 0;
48     GetFileSecurityW( reinterpret_cast< LPCWSTR >(pFilePath), DACL_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|OWNER_SECURITY_INFORMATION, NULL, 0, &nFDSize );
49     if ( nFDSize )
50     {
51         PSECURITY_DESCRIPTOR pFileDescr = reinterpret_cast< PSECURITY_DESCRIPTOR >( malloc( nFDSize ) );
52         if ( GetFileSecurityW( reinterpret_cast< LPCWSTR >(pFilePath), DACL_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|OWNER_SECURITY_INFORMATION, pFileDescr, nFDSize, &nFDSize ) )
53         {
54             HANDLE hToken = NULL;
55             if ( OpenThreadToken( GetCurrentThread(), TOKEN_DUPLICATE|TOKEN_QUERY, TRUE, &hToken )
56               || OpenProcessToken( GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &hToken) )
57             {
58                 HANDLE hImpersonationToken = NULL;
59                 if ( DuplicateToken( hToken, SecurityImpersonation, &hImpersonationToken) )
60                 {
61                     sal_uInt32 nDesiredAccess = ACCESS_WRITE;
62                     GENERIC_MAPPING aGenericMapping = { ACCESS_READ, ACCESS_WRITE, 0, ACCESS_READ | ACCESS_WRITE };
63                     MapGenericMask( &nDesiredAccess, &aGenericMapping );
64 
65                     PRIVILEGE_SET aPrivilegeSet;
66                     sal_uInt32 nPrivilegeSetSize = sizeof( PRIVILEGE_SET );
67 
68                     sal_uInt32 nGrantedAccess;
69                     BOOL bAccessible = TRUE;
70                     if ( AccessCheck( pFileDescr,
71                                       hImpersonationToken,
72                                       nDesiredAccess,
73                                       &aGenericMapping,
74                                       &aPrivilegeSet,
75                                       &nPrivilegeSetSize,
76                                       &nGrantedAccess,
77                                       &bAccessible ) )
78                     {
79                         bResult = !bAccessible;
80                     }
81 
82                     CloseHandle( hImpersonationToken );
83                 }
84 
85                 CloseHandle( hToken );
86             }
87         }
88 
89         free( pFileDescr );
90     }
91 
92     return bResult;
93 }
94 
95 #else // this is UNX
96 // MARKER(update_precomp.py): autogen include statement, do not remove
97 #include "precompiled_sfx2.hxx"
98 
99 
100 #include <sal/types.h>
101 
102 sal_Bool IsReadonlyAccordingACL( const sal_Unicode* )
103 {
104     // to be implemented
105     return sal_False;
106 }
107 
108 #endif
109 
110