xref: /aoo41x/main/sfx2/source/doc/sfxacldetect.cxx (revision 79aad27f)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sfx2.hxx"
26 
27 
28 #ifdef WNT
29 
30 // necessary to include system headers without warnings
31 #ifdef _MSC_VER
32 #pragma warning(disable:4668 4917)
33 #endif
34 
35 #include <windows.h>
36 #include <lmaccess.h>
37 #include <sal/types.h>
38 
IsReadonlyAccordingACL(const sal_Unicode * pFilePath)39 sal_Bool IsReadonlyAccordingACL( const sal_Unicode* pFilePath )
40 {
41     sal_Bool bResult = sal_False;
42 
43     sal_uInt32 nFDSize = 0;
44     GetFileSecurityW( reinterpret_cast< LPCWSTR >(pFilePath), DACL_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|OWNER_SECURITY_INFORMATION, NULL, 0, &nFDSize );
45     if ( nFDSize )
46     {
47         PSECURITY_DESCRIPTOR pFileDescr = reinterpret_cast< PSECURITY_DESCRIPTOR >( malloc( nFDSize ) );
48         if ( GetFileSecurityW( reinterpret_cast< LPCWSTR >(pFilePath), DACL_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|OWNER_SECURITY_INFORMATION, pFileDescr, nFDSize, &nFDSize ) )
49         {
50             HANDLE hToken = NULL;
51             if ( OpenThreadToken( GetCurrentThread(), TOKEN_DUPLICATE|TOKEN_QUERY, TRUE, &hToken )
52               || OpenProcessToken( GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &hToken) )
53             {
54                 HANDLE hImpersonationToken = NULL;
55                 if ( DuplicateToken( hToken, SecurityImpersonation, &hImpersonationToken) )
56                 {
57                     sal_uInt32 nDesiredAccess = ACCESS_WRITE;
58                     GENERIC_MAPPING aGenericMapping = { ACCESS_READ, ACCESS_WRITE, 0, ACCESS_READ | ACCESS_WRITE };
59                     MapGenericMask( &nDesiredAccess, &aGenericMapping );
60 
61                     PRIVILEGE_SET aPrivilegeSet;
62                     sal_uInt32 nPrivilegeSetSize = sizeof( PRIVILEGE_SET );
63 
64                     sal_uInt32 nGrantedAccess;
65                     BOOL bAccessible = TRUE;
66                     if ( AccessCheck( pFileDescr,
67                                       hImpersonationToken,
68                                       nDesiredAccess,
69                                       &aGenericMapping,
70                                       &aPrivilegeSet,
71                                       &nPrivilegeSetSize,
72                                       &nGrantedAccess,
73                                       &bAccessible ) )
74                     {
75                         bResult = !bAccessible;
76                     }
77 
78                     CloseHandle( hImpersonationToken );
79                 }
80 
81                 CloseHandle( hToken );
82             }
83         }
84 
85         free( pFileDescr );
86     }
87 
88     return bResult;
89 }
90 
91 #else // this is UNX
92 // MARKER(update_precomp.py): autogen include statement, do not remove
93 #include "precompiled_sfx2.hxx"
94 
95 
96 #include <sal/types.h>
97 
IsReadonlyAccordingACL(const sal_Unicode *)98 sal_Bool IsReadonlyAccordingACL( const sal_Unicode* )
99 {
100     // to be implemented
101     return sal_False;
102 }
103 
104 #endif
105 
106