@@ -2141,6 +2141,14 @@ int afp_config_parse(AFPObj *AFPObj, char *processname)
2141
2141
char * q = NULL ;
2142
2142
char * r = NULL ;
2143
2143
char val [MAXVAL ];
2144
+ char * configfiles = NULL ;
2145
+ char * token = NULL ;
2146
+ char * saveptr = NULL ;
2147
+ FILE * combined_file = NULL ;
2148
+ char temp_filename [MAXPATHLEN ] = {0 };
2149
+ char buffer [4096 ];
2150
+ FILE * current_file = NULL ;
2151
+ size_t bytes_read ;
2144
2152
2145
2153
if (processname != NULL )
2146
2154
set_processname (processname );
@@ -2153,11 +2161,69 @@ int afp_config_parse(AFPObj *AFPObj, char *processname)
2153
2161
options -> uuidconf = strdup (_PATH_STATEDIR "afp_voluuid.conf" );
2154
2162
options -> flags = OPTION_UUID | AFPObj -> cmdlineflags ;
2155
2163
2156
- become_root ();
2157
- config = iniparser_load (AFPObj -> options .configfile );
2158
- unbecome_root ();
2159
- if (config == NULL )
2160
- return -1 ;
2164
+ if (strchr (options -> configfile , ',' ) != NULL ) {
2165
+ snprintf (temp_filename , sizeof (temp_filename ), "%s/afp_combined_XXXXXX" , tmpdir ());
2166
+ int temp_fd = mkstemp (temp_filename );
2167
+ if (temp_fd == -1 ) {
2168
+ LOG (log_error , logtype_afpd , "Failed to create temporary file: %s" , strerror (errno ));
2169
+ EC_FAIL ;
2170
+ }
2171
+
2172
+ combined_file = fdopen (temp_fd , "w" );
2173
+ if (!combined_file ) {
2174
+ close (temp_fd );
2175
+ unlink (temp_filename );
2176
+ LOG (log_error , logtype_afpd , "Failed to open temporary file: %s" , strerror (errno ));
2177
+ EC_FAIL ;
2178
+ }
2179
+
2180
+ EC_NULL_LOG ( configfiles = strdup (options -> configfile ) );
2181
+ token = strtok_r (configfiles , "," , & saveptr );
2182
+ while (token != NULL ) {
2183
+ /* Trim whitespace */
2184
+ while (* token && isspace (* token ))
2185
+ token ++ ;
2186
+
2187
+ LOG (log_debug , logtype_afpd , "Processing config file: %s" , token );
2188
+
2189
+ become_root ();
2190
+ current_file = fopen (token , "r" );
2191
+ unbecome_root ();
2192
+
2193
+ if (current_file == NULL ) {
2194
+ LOG (log_error , logtype_afpd , "Failed to open config file %s: %s" ,
2195
+ token , strerror (errno ));
2196
+ token = strtok_r (NULL , "," , & saveptr );
2197
+ continue ;
2198
+ }
2199
+
2200
+ fprintf (combined_file , "\n# Begin included file: %s\n" , token );
2201
+
2202
+ /* Copy the content of the current file to the combined file */
2203
+ while ((bytes_read = fread (buffer , 1 , sizeof (buffer ), current_file )) > 0 ) {
2204
+ fwrite (buffer , 1 , bytes_read , combined_file );
2205
+ }
2206
+
2207
+ fprintf (combined_file , "\n# End included file: %s\n" , token );
2208
+
2209
+ fclose (current_file );
2210
+ token = strtok_r (NULL , "," , & saveptr );
2211
+ }
2212
+
2213
+ fclose (combined_file );
2214
+ become_root ();
2215
+ config = iniparser_load (temp_filename );
2216
+ unbecome_root ();
2217
+ unlink (temp_filename );
2218
+ } else {
2219
+ become_root ();
2220
+ config = iniparser_load (options -> configfile );
2221
+ unbecome_root ();
2222
+ }
2223
+
2224
+ if (config == NULL ) {
2225
+ EC_FAIL_LOG ("Failed to load configuration file(s): %s" , options -> configfile );
2226
+ }
2161
2227
AFPObj -> iniconfig = config ;
2162
2228
2163
2229
/* [Global] */
0 commit comments