Skip to content

Commit f478a37

Browse files
sdlimegeographikarouault
authored
Support for a mapserv config file. (MapServer#6303)
Adds support for a MapServer configuration file. It is required for CGI use and optional for MapScript. It standardizes site-level configuration across various operating system and web servers. Co-authored-by: sethg <sethg@geographika.co.uk> Co-authored-by: Even Rouault <even.rouault@spatialys.com>
1 parent 67b1f04 commit f478a37

36 files changed

Lines changed: 3234 additions & 2620 deletions

.github/workflows/start.sh

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ cat <<EOF >/etc/apache2/sites-available/001-mapserver.conf
6969
ErrorLog \${APACHE_LOG_DIR}/mapserv-error.log
7070
CustomLog \${APACHE_LOG_DIR}/mapserv-access.log combined
7171
72+
SetEnv MAPSERVER_CONFIG_FILE ${WORK_DIR}/msautotest/etc/mapserv.conf
73+
FcgidInitialEnv MAPSERVER_CONFIG_FILE ${WORK_DIR}/msautotest/etc/mapserv.conf
74+
7275
ScriptAlias /cgi-bin/ "/tmp/install-mapserver/bin/"
7376
<Directory "/tmp/install-mapserver/bin">
7477
AddHandler fcgid-script .fcgi
@@ -107,28 +110,74 @@ cd msautotest/wxs
107110

108111
export PATH=/tmp/install-mapserver/bin:$PATH
109112

113+
# Demonstrate that mapserv will error out if cannot find config file
114+
mapserv 2>&1 | grep "msLoadConfig(): Unable to access file" >/dev/null && echo yes
115+
mapserv QUERY_STRING="MAP=wfs_simple.map&REQUEST=GetCapabilities" 2>&1 | grep "msLoadConfig(): Unable to access file" >/dev/null && echo yes
116+
110117
echo "Check that MS_MAP_NO_PATH works"
111-
MS_MAP_NO_PATH=1 MYMAPFILE=wfs_simple.map mapserv QUERY_STRING="MAP=MYMAPFILE&SERVICE=WFS&REQUEST=GetCapabilities" > /tmp/res.txt
118+
cat <<EOF >/tmp/mapserver.conf
119+
CONFIG
120+
ENV
121+
"MS_MAP_NO_PATH" "1"
122+
END
123+
MAPS
124+
"MYMAPFILE" "wfs_simple.map"
125+
END
126+
END
127+
EOF
128+
# Also demonstrate that mapserv can find config file in ${CMAKE_INSTALL_FULL_SYSCONFDIR}/etc/mapserver.conf by default
129+
ln -s /tmp/mapserver.conf /tmp/install-mapserver/etc
130+
mapserv QUERY_STRING="MAP=MYMAPFILE&SERVICE=WFS&REQUEST=GetCapabilities" > /tmp/res.txt
131+
rm /tmp/install-mapserver/etc/mapserver.conf
112132
cat /tmp/res.txt | grep wfs:WFS_Capabilities >/dev/null || (cat /tmp/res.txt && /bin/false)
113133

114-
echo "Check that MS_MAP_NO_PATH cannot be abused with client-controlled env variables"
115-
MS_MAP_NO_PATH=1 CONTENT_TYPE=wfs_simple.map mapserv QUERY_STRING="MAP=CONTENT_TYPE&SERVICE=WFS&REQUEST=GetCapabilities" > /tmp/res.txt
134+
echo "Check that MS_MAP_NO_PATH works (rejecting a value not defined in the MAPS section)"
135+
MAPSERVER_CONFIG_FILE=/tmp/mapserver.conf mapserv QUERY_STRING="MAP=FOO&SERVICE=WFS&REQUEST=GetCapabilities" > /tmp/res.txt
116136
cat /tmp/res.txt | grep "Web application error" >/dev/null || (cat /tmp/res.txt && /bin/false)
117137

118138
echo "Check that MS_MAP_PATTERN works (accepting valid MAP)"
119-
MS_MAP_PATTERN="^wfs_simple\.map$" mapserv QUERY_STRING="MAP=wfs_simple.map&SERVICE=WFS&REQUEST=GetCapabilities" > /tmp/res.txt
139+
cat <<EOF >/tmp/mapserver.conf
140+
CONFIG
141+
ENV
142+
"MS_MAP_PATTERN" "^wfs_simple\.map$"
143+
END
144+
END
145+
EOF
146+
MAPSERVER_CONFIG_FILE=/tmp/mapserver.conf mapserv QUERY_STRING="MAP=wfs_simple.map&SERVICE=WFS&REQUEST=GetCapabilities" > /tmp/res.txt
120147
cat /tmp/res.txt | grep wfs:WFS_Capabilities >/dev/null || (cat /tmp/res.txt && /bin/false)
121148

122149
echo "Check that MS_MAP_PATTERN works (rejecting invalid MAP)"
123-
MS_MAP_PATTERN=mypatternmapserv mapserv QUERY_STRING="MAP=wfs_simple.map&SERVICE=WFS&REQUEST=GetCapabilities" > /tmp/res.txt
150+
cat <<EOF >/tmp/mapserver.conf
151+
CONFIG
152+
ENV
153+
"MS_MAP_PATTERN" "mypatternmapserv"
154+
END
155+
END
156+
EOF
157+
MAPSERVER_CONFIG_FILE=/tmp/mapserver.conf mapserv QUERY_STRING="MAP=wfs_simple.map&SERVICE=WFS&REQUEST=GetCapabilities" > /tmp/res.txt
124158
cat /tmp/res.txt | grep "Web application error" >/dev/null || (cat /tmp/res.txt && /bin/false)
125159

126160
echo "Check that MS_MAPFILE works alone"
127-
MS_MAPFILE=wfs_simple.map mapserv QUERY_STRING="SERVICE=WFS&REQUEST=GetCapabilities" > /tmp/res.txt
161+
cat <<EOF >/tmp/mapserver.conf
162+
CONFIG
163+
ENV
164+
"MS_MAPFILE" "wfs_simple.map"
165+
END
166+
END
167+
EOF
168+
MAPSERVER_CONFIG_FILE=/tmp/mapserver.conf mapserv QUERY_STRING="SERVICE=WFS&REQUEST=GetCapabilities" > /tmp/res.txt
128169
cat /tmp/res.txt | grep wfs:WFS_Capabilities >/dev/null || (cat /tmp/res.txt && /bin/false)
129170

130171
echo "Check that a MAP query parameter isn't accepted when MS_MAPFILE and MS_MAP_NO_PATH are specified"
131-
MS_MAP_NO_PATH=1 MS_MAPFILE=wfs_simple.map mapserv QUERY_STRING="MAP=wfs_simple.map&SERVICE=WFS&REQUEST=GetCapabilities" > /tmp/res.txt
172+
cat <<EOF >/tmp/mapserver.conf
173+
CONFIG
174+
ENV
175+
"MS_MAPFILE" "wfs_simple.map"
176+
"MS_MAP_NO_PATH" "1"
177+
END
178+
END
179+
EOF
180+
MAPSERVER_CONFIG_FILE=/tmp/mapserver.conf mapserv QUERY_STRING="MAP=wfs_simple.map&SERVICE=WFS&REQUEST=GetCapabilities" > /tmp/res.txt
132181
cat /tmp/res.txt | grep "Web application error" >/dev/null || (cat /tmp/res.txt && /bin/false)
133182

134183
echo "Done !"

CMakeLists.txt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ mapgeomtransform.c mapogroutput.cpp mapwfslayer.c mapagg.cpp mapkml.cpp
291291
mapgeomutil.cpp mapkmlrenderer.cpp fontcache.c textlayout.c maputfgrid.cpp
292292
mapogr.cpp mapcontour.c mapsmoothing.c mapv8.cpp ${REGEX_SOURCES} kerneldensity.c
293293
idw.c interpolation.c
294-
mapcompositingfilter.c mapmvt.c mapiconv.c mapgraph.cpp)
294+
mapcompositingfilter.c mapmvt.c mapiconv.c mapgraph.cpp mapserv-config.cpp)
295295

296296
set(mapserver_HEADERS
297297
cgiutil.h dejavu-sans-condensed.h dxfcolor.h fontcache.h hittest.h mapagg.h
@@ -300,7 +300,7 @@ maphttp.h mapio.h mapkmlrenderer.h maplibxml2.h mapogcfilter.h mapogcsld.h
300300
mapoglcontext.h mapoglrenderer.h mapowscommon.h mapows.h mapparser.h mapogcapi.h
301301
mappostgis.h mapprimitive.h mapproject.h mapraster.h mapregex.h mapresample.h
302302
mapserver-api.h mapserver.h mapserv.h mapshape.h mapsymbol.h maptemplate.h
303-
mapthread.h maptile.h maptime.h maptree.h maputfgrid.h mapwcs.h uthash.h mapiconv.h mapgraph.h)
303+
mapthread.h maptile.h maptime.h maptree.h maputfgrid.h mapwcs.h uthash.h mapiconv.h mapgraph.h mapserv-config.h)
304304

305305
if(WIN32)
306306
configure_file(
@@ -1049,16 +1049,22 @@ if(UNIX)
10491049
else()
10501050
set(DEFAULT_DATA_SUBDIR share/mapserver)
10511051
endif()
1052+
set(DEFAULT_CONFIG_FILE ${CMAKE_INSTALL_FULL_SYSCONFDIR}/mapserver.conf)
10521053

10531054
# Locations are changeable by user to customize layout of MapServer installation
10541055
# (default values are platform-specific)
10551056
set(MAPSERVER_DATA_SUBDIR ${DEFAULT_DATA_SUBDIR} CACHE STRING
10561057
"Subdirectory where data will be installed")
10571058

1059+
set(MAPSERVER_CONFIG_FILE "${DEFAULT_CONFIG_FILE}" CACHE STRING
1060+
"Full file name for default configuration file (mapserver.conf)")
1061+
10581062
# Mark *DIR variables as advanced and dedicated to use by power-users only.
10591063
mark_as_advanced(
10601064
MAPSERVER_DATA_SUBDIR
1065+
MAPSERVER_CONFIG_FILE
10611066
)
1067+
add_definitions(-DMAPSERVER_CONFIG_FILE="${MAPSERVER_CONFIG_FILE}")
10621068

10631069
install(
10641070
FILES ${PROJECT_SOURCE_DIR}/share/ogcapi/templates/html-plain/collection.html
@@ -1073,3 +1079,8 @@ install(
10731079
${PROJECT_SOURCE_DIR}/share/ogcapi/templates/html-plain/openapi.html
10741080
DESTINATION ${MAPSERVER_DATA_SUBDIR}/ogcapi/templates/html-plain/
10751081
)
1082+
1083+
install(
1084+
FILES ${PROJECT_SOURCE_DIR}/etc/mapserver-sample.conf
1085+
DESTINATION ${CMAKE_INSTALL_FULL_SYSCONFDIR}/
1086+
)

cgiutil.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#include "mapserver.h"
3737
#include "cgiutil.h"
3838

39+
#include "cpl_conv.h"
40+
3941
#define LF 10
4042
#define CR 13
4143

@@ -142,7 +144,7 @@ int loadParams(cgiRequestObj *request,
142144

143145
debuglevel = (int)msGetGlobalDebugLevel();
144146

145-
if(strcmp(getenv2("REQUEST_METHOD", thread_context),"POST") == 0) { /* we've got a post from a form */
147+
if(strcmp(getenv2("REQUEST_METHOD", thread_context),"POST") == 0 && CPLGetConfigOption("MS_NO_POST", NULL) == NULL) { /* we've got a post from a form */
146148
char *post_data;
147149
int data_len;
148150
request->type = MS_POST_REQUEST;

etc/mapserver-sample.conf

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#
2+
# Sample MapServer 8.0 Config File
3+
#
4+
CONFIG
5+
6+
#
7+
# Environment variables (see https://mapserver.org/environment_variables.html)
8+
#
9+
ENV
10+
#
11+
# Limit Mapfile Access
12+
#
13+
# MS_MAP_NO_PATH "1"
14+
MS_MAP_PATTERN "^/opt/mapserver" ## required
15+
16+
#
17+
# Global Log/Debug Setup
18+
#
19+
# MS_DEBUGLEVEL "5"
20+
# MS_ERRORFILE "/opt/mapserver/logs/mapserver.log"
21+
22+
#
23+
# Default Map
24+
#
25+
# MS_MAPFILE "/opt/mapserver/test/test.map"
26+
27+
#
28+
# Proj Library
29+
#
30+
# PROJ_LIB ""
31+
32+
#
33+
# Request Control
34+
#
35+
# disable POST requests (allowed by default, any value will do)
36+
# MS_NO_POST "1"
37+
38+
#
39+
# Other Options
40+
#
41+
# MS_ENCRYPTION_KEY
42+
# MS_USE_GLOBAL_FT_CACHE
43+
# MS_PDF_CREATION_DATE
44+
# MS_MAPFILE_PATTERN "\.map$"
45+
# MS_XMLMAPFILE_XSLT
46+
# MS_MODE
47+
# MS_OPENLAYERS_JS_URL
48+
# MS_TEMPPATH
49+
# MS_MAX_OPEN_FILES
50+
51+
#
52+
# OGC API
53+
#
54+
# OGCAPI_HTML_TEMPLATE_DIRECTORY "/usr/local/mapserver/share/ogcapi/templates/html-bootstrap4/"
55+
END
56+
57+
#
58+
# Map Aliases
59+
#
60+
MAPS
61+
# TEST_MAPFILE "/opt/mapserver/test/test.map"
62+
END
63+
64+
END

fontcache.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include "fontcache.h"
3333
#include "dejavu-sans-condensed.h"
3434

35+
#include "cpl_conv.h"
36+
3537
typedef struct {
3638
FT_Library library;
3739
face_element *face_cache;
@@ -160,7 +162,7 @@ void msFontCacheSetup() {
160162
ft_cache *c = msGetFontCache();
161163
msInitFontCache(c);
162164
#else
163-
char* use_global_cache = getenv("MS_USE_GLOBAL_FT_CACHE");
165+
const char *use_global_cache = CPLGetConfigOption("MS_USE_GLOBAL_FT_CACHE", NULL);
164166
if (use_global_cache)
165167
use_global_ft_cache = atoi(use_global_cache);
166168
else

legend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ int main(int argc, char *argv[])
4747
exit(0);
4848
}
4949

50-
map = msLoadMap(argv[1], NULL);
50+
map = msLoadMap(argv[1], NULL, NULL);
5151
if(!map) {
5252
msWriteError(stderr);
5353
exit(0);

0 commit comments

Comments
 (0)