1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package fr.paris.lutece.plugins.managelogs.web;
36
37 import fr.paris.lutece.plugins.managelogs.util.ManageLogsUtil;
38 import fr.paris.lutece.portal.service.util.AppLogService;
39 import fr.paris.lutece.portal.service.util.AppPathService;
40 import fr.paris.lutece.portal.service.util.AppPropertiesService;
41 import fr.paris.lutece.portal.util.mvc.admin.MVCAdminJspBean;
42
43 import java.nio.file.AccessDeniedException;
44 import java.nio.file.Path;
45 import java.nio.file.Paths;
46 import java.util.ArrayList;
47 import java.util.List;
48
49
50
51
52 public abstract class AbstractManageLogsPropertiesJspBean extends MVCAdminJspBean
53 {
54
55 protected static final String SLASH = "/";
56 protected static final String EMPTY = "";
57
58
59 public static final String RIGHT_MANAGELOGSPROPERTIES = "MANAGELOGS_MANAGEMENT";
60
61
62
63 protected static final String TMP_LOG_PATH = AppPropertiesService.getProperty( "managelogs.tmp.log.path", "WEB-INF/conf/override/" );
64 protected static final String TMP_LOG_FILE_NAME = AppPropertiesService.getProperty( "managelogs.tmp.log.filename", "tmp_log.properties" );
65 protected static final String TMP_LOG_PATH_ABSOLUTE = getAbsolutePath( TMP_LOG_PATH );
66 protected static final String TMP_LOG_ABSOLUTE = TMP_LOG_PATH_ABSOLUTE + ( TMP_LOG_PATH_ABSOLUTE.endsWith( SLASH ) ? EMPTY : SLASH ) + TMP_LOG_FILE_NAME;
67
68 protected static final String LUTECE_LOG_PATH = AppPropertiesService.getProperty( "managelogs.lutece.log.path", "WEB-INF/conf/" );
69 protected static final String LUTECE_LOG_FILE = AppPropertiesService.getProperty( "managelogs.lutecec.log.file", "config.properties" );
70
71 protected static final String ALTERNATE_LOG_CONF_FILE_ABSOLUTE = getAbsolutePath( LUTECE_LOG_PATH + ( LUTECE_LOG_PATH.endsWith( SLASH ) ? EMPTY : SLASH ) + "override/" + "log.properties" );
72 protected static final String LUTECE_CONF_FILE_ABSOLUTE = getAbsolutePath( LUTECE_LOG_PATH + ( LUTECE_LOG_PATH.endsWith( SLASH ) ? EMPTY : SLASH ) + LUTECE_LOG_FILE );
73
74 protected static final boolean APP_SERVER_MULTI_WEBAPP = AppPropertiesService.getPropertyBoolean( "managelogs.is.multi.webapp", false );
75
76 private static List<String> _listLogFoldersAllowed = new ArrayList<>( );
77
78 static
79 {
80
81 String strListFoldersRelative = AppPropertiesService.getProperty( "managelogs.limit.folder" );
82 if ( strListFoldersRelative != null )
83 {
84 String[] arrFoldersRelative = strListFoldersRelative.split( ";" );
85 for ( String relativeFolder : arrFoldersRelative )
86 {
87 if ( !ManageLogsUtil.isNullOrEmptyWithTrim( relativeFolder ) )
88 {
89 _listLogFoldersAllowed.add( getAbsolutePath( relativeFolder ) );
90 }
91 }
92 }
93 }
94
95
96
97
98
99
100
101 protected static String getAbsolutePath( String strPath )
102 {
103 Path path = Paths.get( strPath );
104 if ( path.isAbsolute( ) )
105 {
106 return strPath;
107 }
108 else
109 {
110
111 return AppPathService.getAbsolutePathFromRelativePath( ( strPath.startsWith( SLASH ) ? EMPTY : SLASH ) + strPath );
112 }
113 }
114
115
116
117
118
119
120 protected static List<String> getFilesFromConfiguration( List<String> lines, boolean throwExceptionOnInaccessibleFile ) throws AccessDeniedException
121 {
122 if ( lines == null )
123 {
124 return new ArrayList<>( );
125 }
126
127 List<String> listConfiguredFiles = new ArrayList<>( );
128 for ( String line : lines )
129 {
130
131 if ( !ManageLogsUtil.isNullOrEmptyWithTrim( line ) && !line.trim( ).startsWith( "#" ) && line.split( "=" ).length == 2 && line.contains( ".File=" ) )
132 {
133 String fileName = line.split( "=" )[ 1 ];
134 String strParentAbsoluteDirectory = Paths.get( getAbsolutePath( fileName.replaceAll( "\r", EMPTY ) ) ).getParent( ).toString( );
135 if ( isLogFileAccessible( strParentAbsoluteDirectory ) )
136 {
137 listConfiguredFiles.add( fileName );
138 }
139 else
140 {
141 AppLogService.debug( "File " + fileName + " inaccessible" );
142 if ( throwExceptionOnInaccessibleFile )
143 {
144 throw new AccessDeniedException( "Folder " + strParentAbsoluteDirectory + " is denied (check 'limit.folder' configuration" );
145 }
146 }
147 }
148 }
149
150 return listConfiguredFiles;
151 }
152
153
154
155
156
157
158 protected static boolean isLogFileAccessible( String file )
159 {
160 if ( ManageLogsUtil.isNullOrEmptyWithTrim( file ) )
161 {
162 return false;
163 }
164
165 boolean bIsFileOK = false;
166
167
168 for ( String strValidFolder : _listLogFoldersAllowed )
169 {
170 if ( !ManageLogsUtil.isNullOrEmptyWithTrim( strValidFolder ) && Paths.get( file.trim() ).startsWith( Paths.get( strValidFolder.trim() ) ) )
171 {
172 bIsFileOK = true;
173 break;
174 }
175 }
176
177 return bIsFileOK && ManageLogsUtil.isFileReadable( file );
178 }
179
180 }