1 /* -*-pgsql-c-*- */
2 /*
3  * Scanner for the configuration file
4  *
5  * Copyright (c) 2000-2018, PostgreSQL Global Development Group
6  *
7  * src/backend/utils/misc/guc-file.l
8  */
9 
10 %{
11 
12 #include "postgres.h"
13 
14 #include <ctype.h>
15 #include <unistd.h>
16 
17 #include "mb/pg_wchar.h"
18 #include "miscadmin.h"
19 #include "storage/fd.h"
20 #include "utils/guc.h"
21 
22 
23 /*
24  * flex emits a yy_fatal_error() function that it calls in response to
25  * critical errors like malloc failure, file I/O errors, and detection of
26  * internal inconsistency.  That function prints a message and calls exit().
27  * Mutate it to instead call our handler, which jumps out of the parser.
28  */
29 #undef fprintf
30 #define fprintf(file, fmt, msg) GUC_flex_fatal(msg)
31 
32 enum
33 {
34 	GUC_ID = 1,
35 	GUC_STRING = 2,
36 	GUC_INTEGER = 3,
37 	GUC_REAL = 4,
38 	GUC_EQUALS = 5,
39 	GUC_UNQUOTED_STRING = 6,
40 	GUC_QUALIFIED_ID = 7,
41 	GUC_EOL = 99,
42 	GUC_ERROR = 100
43 };
44 
45 static unsigned int ConfigFileLineno;
46 static const char *GUC_flex_fatal_errmsg;
47 static sigjmp_buf *GUC_flex_fatal_jmp;
48 
49 static void FreeConfigVariable(ConfigVariable *item);
50 
51 static void record_config_file_error(const char *errmsg,
52 						 const char *config_file,
53 						 int lineno,
54 						 ConfigVariable **head_p,
55 						 ConfigVariable **tail_p);
56 
57 static int	GUC_flex_fatal(const char *msg);
58 static char *GUC_scanstr(const char *s);
59 
60 /* LCOV_EXCL_START */
61 
62 %}
63 
64 %option 8bit
65 %option never-interactive
66 %option nodefault
67 %option noinput
68 %option nounput
69 %option noyywrap
70 %option warn
71 %option prefix="GUC_yy"
72 
73 
74 SIGN			("-"|"+")
75 DIGIT			[0-9]
76 HEXDIGIT		[0-9a-fA-F]
77 
78 UNIT_LETTER		[a-zA-Z]
79 
80 INTEGER			{SIGN}?({DIGIT}+|0x{HEXDIGIT}+){UNIT_LETTER}*
81 
82 EXPONENT		[Ee]{SIGN}?{DIGIT}+
83 REAL			{SIGN}?{DIGIT}*"."{DIGIT}*{EXPONENT}?
84 
85 LETTER			[A-Za-z_\200-\377]
86 LETTER_OR_DIGIT [A-Za-z_0-9\200-\377]
87 
88 ID				{LETTER}{LETTER_OR_DIGIT}*
89 QUALIFIED_ID	{ID}"."{ID}
90 
91 UNQUOTED_STRING {LETTER}({LETTER_OR_DIGIT}|[-._:/])*
92 STRING			\'([^'\\\n]|\\.|\'\')*\'
93 
94 %%
95 
96 \n				ConfigFileLineno++; return GUC_EOL;
97 [ \t\r]+		/* eat whitespace */
98 #.*				/* eat comment (.* matches anything until newline) */
99 
100 {ID}			return GUC_ID;
101 {QUALIFIED_ID}	return GUC_QUALIFIED_ID;
102 {STRING}		return GUC_STRING;
103 {UNQUOTED_STRING} return GUC_UNQUOTED_STRING;
104 {INTEGER}		return GUC_INTEGER;
105 {REAL}			return GUC_REAL;
106 =				return GUC_EQUALS;
107 
108 .				return GUC_ERROR;
109 
110 %%
111 
112 /* LCOV_EXCL_STOP */
113 
114 /*
115  * Exported function to read and process the configuration file. The
116  * parameter indicates in what context the file is being read --- either
117  * postmaster startup (including standalone-backend startup) or SIGHUP.
118  * All options mentioned in the configuration file are set to new values.
119  * If a hard error occurs, no values will be changed.  (There can also be
120  * errors that prevent just one value from being changed.)
121  */
122 void
123 ProcessConfigFile(GucContext context)
124 {
125 	int			elevel;
126 	MemoryContext config_cxt;
127 	MemoryContext caller_cxt;
128 
129 	/*
130 	 * Config files are processed on startup (by the postmaster only) and on
131 	 * SIGHUP (by the postmaster and its children)
132 	 */
133 	Assert((context == PGC_POSTMASTER && !IsUnderPostmaster) ||
134 		   context == PGC_SIGHUP);
135 
136 	/*
137 	 * To avoid cluttering the log, only the postmaster bleats loudly about
138 	 * problems with the config file.
139 	 */
140 	elevel = IsUnderPostmaster ? DEBUG2 : LOG;
141 
142 	/*
143 	 * This function is usually called within a process-lifespan memory
144 	 * context.  To ensure that any memory leaked during GUC processing does
145 	 * not accumulate across repeated SIGHUP cycles, do the work in a private
146 	 * context that we can free at exit.
147 	 */
148 	config_cxt = AllocSetContextCreate(CurrentMemoryContext,
149 									   "config file processing",
150 									   ALLOCSET_DEFAULT_SIZES);
151 	caller_cxt = MemoryContextSwitchTo(config_cxt);
152 
153 	/*
154 	 * Read and apply the config file.  We don't need to examine the result.
155 	 */
156 	(void) ProcessConfigFileInternal(context, true, elevel);
157 
158 	/* Clean up */
159 	MemoryContextSwitchTo(caller_cxt);
160 	MemoryContextDelete(config_cxt);
161 }
162 
163 /*
164  * This function handles both actual config file (re)loads and execution of
165  * show_all_file_settings() (i.e., the pg_file_settings view).  In the latter
166  * case we don't apply any of the settings, but we make all the usual validity
167  * checks, and we return the ConfigVariable list so that it can be printed out
168  * by show_all_file_settings().
169  */
170 static ConfigVariable *
ProcessConfigFileInternal(GucContext context,bool applySettings,int elevel)171 ProcessConfigFileInternal(GucContext context, bool applySettings, int elevel)
172 {
173 	bool		error = false;
174 	bool		applying = false;
175 	const char *ConfFileWithError;
176 	ConfigVariable *item,
177 			   *head,
178 			   *tail;
179 	int			i;
180 
181 	/* Parse the main config file into a list of option names and values */
182 	ConfFileWithError = ConfigFileName;
183 	head = tail = NULL;
184 
185 	if (!ParseConfigFile(ConfigFileName, true,
186 						 NULL, 0, 0, elevel,
187 						 &head, &tail))
188 	{
189 		/* Syntax error(s) detected in the file, so bail out */
190 		error = true;
191 		goto bail_out;
192 	}
193 
194 	/*
195 	 * Parse the PG_AUTOCONF_FILENAME file, if present, after the main file to
196 	 * replace any parameters set by ALTER SYSTEM command.  Because this file
197 	 * is in the data directory, we can't read it until the DataDir has been
198 	 * set.
199 	 */
200 	if (DataDir)
201 	{
202 		if (!ParseConfigFile(PG_AUTOCONF_FILENAME, false,
203 							 NULL, 0, 0, elevel,
204 							 &head, &tail))
205 		{
206 			/* Syntax error(s) detected in the file, so bail out */
207 			error = true;
208 			ConfFileWithError = PG_AUTOCONF_FILENAME;
209 			goto bail_out;
210 		}
211 	}
212 	else
213 	{
214 		/*
215 		 * If DataDir is not set, the PG_AUTOCONF_FILENAME file cannot be
216 		 * read.  In this case, we don't want to accept any settings but
217 		 * data_directory from postgresql.conf, because they might be
218 		 * overwritten with settings in the PG_AUTOCONF_FILENAME file which
219 		 * will be read later. OTOH, since data_directory isn't allowed in the
220 		 * PG_AUTOCONF_FILENAME file, it will never be overwritten later.
221 		 */
222 		ConfigVariable *newlist = NULL;
223 
224 		/*
225 		 * Prune all items except the last "data_directory" from the list.
226 		 */
227 		for (item = head; item; item = item->next)
228 		{
229 			if (!item->ignore &&
230 				strcmp(item->name, "data_directory") == 0)
231 				newlist = item;
232 		}
233 
234 		if (newlist)
235 			newlist->next = NULL;
236 		head = tail = newlist;
237 
238 		/*
239 		 * Quick exit if data_directory is not present in file.
240 		 *
241 		 * We need not do any further processing, in particular we don't set
242 		 * PgReloadTime; that will be set soon by subsequent full loading of
243 		 * the config file.
244 		 */
245 		if (head == NULL)
246 			goto bail_out;
247 	}
248 
249 	/*
250 	 * Mark all extant GUC variables as not present in the config file. We
251 	 * need this so that we can tell below which ones have been removed from
252 	 * the file since we last processed it.
253 	 */
254 	for (i = 0; i < num_guc_variables; i++)
255 	{
256 		struct config_generic *gconf = guc_variables[i];
257 
258 		gconf->status &= ~GUC_IS_IN_FILE;
259 	}
260 
261 	/*
262 	 * Check if all the supplied option names are valid, as an additional
263 	 * quasi-syntactic check on the validity of the config file.  It is
264 	 * important that the postmaster and all backends agree on the results of
265 	 * this phase, else we will have strange inconsistencies about which
266 	 * processes accept a config file update and which don't.  Hence, unknown
267 	 * custom variable names have to be accepted without complaint.  For the
268 	 * same reason, we don't attempt to validate the options' values here.
269 	 *
270 	 * In addition, the GUC_IS_IN_FILE flag is set on each existing GUC
271 	 * variable mentioned in the file; and we detect duplicate entries in the
272 	 * file and mark the earlier occurrences as ignorable.
273 	 */
274 	for (item = head; item; item = item->next)
275 	{
276 		struct config_generic *record;
277 
278 		/* Ignore anything already marked as ignorable */
279 		if (item->ignore)
280 			continue;
281 
282 		/*
283 		 * Try to find the variable; but do not create a custom placeholder if
284 		 * it's not there already.
285 		 */
286 		record = find_option(item->name, false, elevel);
287 
288 		if (record)
289 		{
290 			/* If it's already marked, then this is a duplicate entry */
291 			if (record->status & GUC_IS_IN_FILE)
292 			{
293 				/*
294 				 * Mark the earlier occurrence(s) as dead/ignorable.  We could
295 				 * avoid the O(N^2) behavior here with some additional state,
296 				 * but it seems unlikely to be worth the trouble.
297 				 */
298 				ConfigVariable *pitem;
299 
300 				for (pitem = head; pitem != item; pitem = pitem->next)
301 				{
302 					if (!pitem->ignore &&
303 						strcmp(pitem->name, item->name) == 0)
304 						pitem->ignore = true;
305 				}
306 			}
307 			/* Now mark it as present in file */
308 			record->status |= GUC_IS_IN_FILE;
309 		}
310 		else if (strchr(item->name, GUC_QUALIFIER_SEPARATOR) == NULL)
311 		{
312 			/* Invalid non-custom variable, so complain */
313 			ereport(elevel,
314 					(errcode(ERRCODE_UNDEFINED_OBJECT),
315 					 errmsg("unrecognized configuration parameter \"%s\" in file \"%s\" line %u",
316 							item->name,
317 							item->filename, item->sourceline)));
318 			item->errmsg = pstrdup("unrecognized configuration parameter");
319 			error = true;
320 			ConfFileWithError = item->filename;
321 		}
322 	}
323 
324 	/*
325 	 * If we've detected any errors so far, we don't want to risk applying any
326 	 * changes.
327 	 */
328 	if (error)
329 		goto bail_out;
330 
331 	/* Otherwise, set flag that we're beginning to apply changes */
332 	applying = true;
333 
334 	/*
335 	 * Check for variables having been removed from the config file, and
336 	 * revert their reset values (and perhaps also effective values) to the
337 	 * boot-time defaults.  If such a variable can't be changed after startup,
338 	 * report that and continue.
339 	 */
340 	for (i = 0; i < num_guc_variables; i++)
341 	{
342 		struct config_generic *gconf = guc_variables[i];
343 		GucStack   *stack;
344 
345 		if (gconf->reset_source != PGC_S_FILE ||
346 			(gconf->status & GUC_IS_IN_FILE))
347 			continue;
348 		if (gconf->context < PGC_SIGHUP)
349 		{
350 			/* The removal can't be effective without a restart */
351 			gconf->status |= GUC_PENDING_RESTART;
352 			ereport(elevel,
353 					(errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
354 					 errmsg("parameter \"%s\" cannot be changed without restarting the server",
355 							gconf->name)));
356 			record_config_file_error(psprintf("parameter \"%s\" cannot be changed without restarting the server",
357 											  gconf->name),
358 									 NULL, 0,
359 									 &head, &tail);
360 			error = true;
361 			continue;
362 		}
363 
364 		/* No more to do if we're just doing show_all_file_settings() */
365 		if (!applySettings)
366 			continue;
367 
368 		/*
369 		 * Reset any "file" sources to "default", else set_config_option will
370 		 * not override those settings.
371 		 */
372 		if (gconf->reset_source == PGC_S_FILE)
373 			gconf->reset_source = PGC_S_DEFAULT;
374 		if (gconf->source == PGC_S_FILE)
375 			gconf->source = PGC_S_DEFAULT;
376 		for (stack = gconf->stack; stack; stack = stack->prev)
377 		{
378 			if (stack->source == PGC_S_FILE)
379 				stack->source = PGC_S_DEFAULT;
380 		}
381 
382 		/* Now we can re-apply the wired-in default (i.e., the boot_val) */
383 		if (set_config_option(gconf->name, NULL,
384 							  context, PGC_S_DEFAULT,
385 							  GUC_ACTION_SET, true, 0, false) > 0)
386 		{
387 			/* Log the change if appropriate */
388 			if (context == PGC_SIGHUP)
389 				ereport(elevel,
390 						(errmsg("parameter \"%s\" removed from configuration file, reset to default",
391 								gconf->name)));
392 		}
393 	}
394 
395 	/*
396 	 * Restore any variables determined by environment variables or
397 	 * dynamically-computed defaults.  This is a no-op except in the case
398 	 * where one of these had been in the config file and is now removed.
399 	 *
400 	 * In particular, we *must not* do this during the postmaster's initial
401 	 * loading of the file, since the timezone functions in particular should
402 	 * be run only after initialization is complete.
403 	 *
404 	 * XXX this is an unmaintainable crock, because we have to know how to set
405 	 * (or at least what to call to set) every variable that could potentially
406 	 * have PGC_S_DYNAMIC_DEFAULT or PGC_S_ENV_VAR source. However, there's no
407 	 * time to redesign it for 9.1.
408 	 */
409 	if (context == PGC_SIGHUP && applySettings)
410 	{
411 		InitializeGUCOptionsFromEnvironment();
412 		pg_timezone_abbrev_initialize();
413 		/* this selects SQL_ASCII in processes not connected to a database */
414 		SetConfigOption("client_encoding", GetDatabaseEncodingName(),
415 						PGC_BACKEND, PGC_S_DYNAMIC_DEFAULT);
416 	}
417 
418 	/*
419 	 * Now apply the values from the config file.
420 	 */
421 	for (item = head; item; item = item->next)
422 	{
423 		char	   *pre_value = NULL;
424 		int			scres;
425 
426 		/* Ignore anything marked as ignorable */
427 		if (item->ignore)
428 			continue;
429 
430 		/* In SIGHUP cases in the postmaster, we want to report changes */
431 		if (context == PGC_SIGHUP && applySettings && !IsUnderPostmaster)
432 		{
433 			const char *preval = GetConfigOption(item->name, true, false);
434 
435 			/* If option doesn't exist yet or is NULL, treat as empty string */
436 			if (!preval)
437 				preval = "";
438 			/* must dup, else might have dangling pointer below */
439 			pre_value = pstrdup(preval);
440 		}
441 
442 		scres = set_config_option(item->name, item->value,
443 								  context, PGC_S_FILE,
444 								  GUC_ACTION_SET, applySettings, 0, false);
445 		if (scres > 0)
446 		{
447 			/* variable was updated, so log the change if appropriate */
448 			if (pre_value)
449 			{
450 				const char *post_value = GetConfigOption(item->name, true, false);
451 
452 				if (!post_value)
453 					post_value = "";
454 				if (strcmp(pre_value, post_value) != 0)
455 					ereport(elevel,
456 							(errmsg("parameter \"%s\" changed to \"%s\"",
457 									item->name, item->value)));
458 			}
459 			item->applied = true;
460 		}
461 		else if (scres == 0)
462 		{
463 			error = true;
464 			item->errmsg = pstrdup("setting could not be applied");
465 			ConfFileWithError = item->filename;
466 		}
467 		else
468 		{
469 			/* no error, but variable's active value was not changed */
470 			item->applied = true;
471 		}
472 
473 		/*
474 		 * We should update source location unless there was an error, since
475 		 * even if the active value didn't change, the reset value might have.
476 		 * (In the postmaster, there won't be a difference, but it does matter
477 		 * in backends.)
478 		 */
479 		if (scres != 0 && applySettings)
480 			set_config_sourcefile(item->name, item->filename,
481 								  item->sourceline);
482 
483 		if (pre_value)
484 			pfree(pre_value);
485 	}
486 
487 	/* Remember when we last successfully loaded the config file. */
488 	if (applySettings)
489 		PgReloadTime = GetCurrentTimestamp();
490 
491 bail_out:
492 	if (error && applySettings)
493 	{
494 		/* During postmaster startup, any error is fatal */
495 		if (context == PGC_POSTMASTER)
496 			ereport(ERROR,
497 					(errcode(ERRCODE_CONFIG_FILE_ERROR),
498 					 errmsg("configuration file \"%s\" contains errors",
499 							ConfFileWithError)));
500 		else if (applying)
501 			ereport(elevel,
502 					(errcode(ERRCODE_CONFIG_FILE_ERROR),
503 					 errmsg("configuration file \"%s\" contains errors; unaffected changes were applied",
504 							ConfFileWithError)));
505 		else
506 			ereport(elevel,
507 					(errcode(ERRCODE_CONFIG_FILE_ERROR),
508 					 errmsg("configuration file \"%s\" contains errors; no changes were applied",
509 							ConfFileWithError)));
510 	}
511 
512 	/* Successful or otherwise, return the collected data list */
513 	return head;
514 }
515 
516 /*
517  * Given a configuration file or directory location that may be a relative
518  * path, return an absolute one.  We consider the location to be relative to
519  * the directory holding the calling file, or to DataDir if no calling file.
520  */
521 static char *
AbsoluteConfigLocation(const char * location,const char * calling_file)522 AbsoluteConfigLocation(const char *location, const char *calling_file)
523 {
524 	char		abs_path[MAXPGPATH];
525 
526 	if (is_absolute_path(location))
527 		return pstrdup(location);
528 	else
529 	{
530 		if (calling_file != NULL)
531 		{
532 			strlcpy(abs_path, calling_file, sizeof(abs_path));
533 			get_parent_directory(abs_path);
534 			join_path_components(abs_path, abs_path, location);
535 			canonicalize_path(abs_path);
536 		}
537 		else
538 		{
539 			AssertState(DataDir);
540 			join_path_components(abs_path, DataDir, location);
541 			canonicalize_path(abs_path);
542 		}
543 		return pstrdup(abs_path);
544 	}
545 }
546 
547 /*
548  * Read and parse a single configuration file.  This function recurses
549  * to handle "include" directives.
550  *
551  * If "strict" is true, treat failure to open the config file as an error,
552  * otherwise just skip the file.
553  *
554  * calling_file/calling_lineno identify the source of the request.
555  * Pass NULL/0 if not recursing from an inclusion request.
556  *
557  * See ParseConfigFp for further details.  This one merely adds opening the
558  * config file rather than working from a caller-supplied file descriptor,
559  * and absolute-ifying the path name if necessary.
560  */
561 bool
ParseConfigFile(const char * config_file,bool strict,const char * calling_file,int calling_lineno,int depth,int elevel,ConfigVariable ** head_p,ConfigVariable ** tail_p)562 ParseConfigFile(const char *config_file, bool strict,
563 				const char *calling_file, int calling_lineno,
564 				int depth, int elevel,
565 				ConfigVariable **head_p,
566 				ConfigVariable **tail_p)
567 {
568 	char	   *abs_path;
569 	bool		OK = true;
570 	FILE	   *fp;
571 
572 	/*
573 	 * Reject file name that is all-blank (including empty), as that leads to
574 	 * confusion --- we'd try to read the containing directory as a file.
575 	 */
576 	if (strspn(config_file, " \t\r\n") == strlen(config_file))
577 	{
578 		ereport(elevel,
579 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
580 				 errmsg("empty configuration file name: \"%s\"",
581 						config_file)));
582 		record_config_file_error("empty configuration file name",
583 								 calling_file, calling_lineno,
584 								 head_p, tail_p);
585 		return false;
586 	}
587 
588 	/*
589 	 * Reject too-deep include nesting depth.  This is just a safety check to
590 	 * avoid dumping core due to stack overflow if an include file loops back
591 	 * to itself.  The maximum nesting depth is pretty arbitrary.
592 	 */
593 	if (depth > 10)
594 	{
595 		ereport(elevel,
596 				(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
597 				 errmsg("could not open configuration file \"%s\": maximum nesting depth exceeded",
598 						config_file)));
599 		record_config_file_error("nesting depth exceeded",
600 								 calling_file, calling_lineno,
601 								 head_p, tail_p);
602 		return false;
603 	}
604 
605 	abs_path = AbsoluteConfigLocation(config_file, calling_file);
606 
607 	/*
608 	 * Reject direct recursion.  Indirect recursion is also possible, but it's
609 	 * harder to detect and so doesn't seem worth the trouble.  (We test at
610 	 * this step because the canonicalization done by AbsoluteConfigLocation
611 	 * makes it more likely that a simple strcmp comparison will match.)
612 	 */
613 	if (calling_file && strcmp(abs_path, calling_file) == 0)
614 	{
615 		ereport(elevel,
616 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
617 				 errmsg("configuration file recursion in \"%s\"",
618 						calling_file)));
619 		record_config_file_error("configuration file recursion",
620 								 calling_file, calling_lineno,
621 								 head_p, tail_p);
622 		pfree(abs_path);
623 		return false;
624 	}
625 
626 	fp = AllocateFile(abs_path, "r");
627 	if (!fp)
628 	{
629 		if (strict)
630 		{
631 			ereport(elevel,
632 					(errcode_for_file_access(),
633 					 errmsg("could not open configuration file \"%s\": %m",
634 							abs_path)));
635 			record_config_file_error(psprintf("could not open file \"%s\"",
636 											  abs_path),
637 									 calling_file, calling_lineno,
638 									 head_p, tail_p);
639 			OK = false;
640 		}
641 		else
642 		{
643 			ereport(LOG,
644 					(errmsg("skipping missing configuration file \"%s\"",
645 							abs_path)));
646 		}
647 		goto cleanup;
648 	}
649 
650 	OK = ParseConfigFp(fp, abs_path, depth, elevel, head_p, tail_p);
651 
652 cleanup:
653 	if (fp)
654 		FreeFile(fp);
655 	pfree(abs_path);
656 
657 	return OK;
658 }
659 
660 /*
661  * Capture an error message in the ConfigVariable list returned by
662  * config file parsing.
663  */
664 static void
record_config_file_error(const char * errmsg,const char * config_file,int lineno,ConfigVariable ** head_p,ConfigVariable ** tail_p)665 record_config_file_error(const char *errmsg,
666 						 const char *config_file,
667 						 int lineno,
668 						 ConfigVariable **head_p,
669 						 ConfigVariable **tail_p)
670 {
671 	ConfigVariable *item;
672 
673 	item = palloc(sizeof *item);
674 	item->name = NULL;
675 	item->value = NULL;
676 	item->errmsg = pstrdup(errmsg);
677 	item->filename = config_file ? pstrdup(config_file) : NULL;
678 	item->sourceline = lineno;
679 	item->ignore = true;
680 	item->applied = false;
681 	item->next = NULL;
682 	if (*head_p == NULL)
683 		*head_p = item;
684 	else
685 		(*tail_p)->next = item;
686 	*tail_p = item;
687 }
688 
689 /*
690  * Flex fatal errors bring us here.  Stash the error message and jump back to
691  * ParseConfigFp().  Assume all msg arguments point to string constants; this
692  * holds for flex 2.5.31 (earliest we support) and flex 2.5.35 (latest as of
693  * this writing).  Otherwise, we would need to copy the message.
694  *
695  * We return "int" since this takes the place of calls to fprintf().
696 */
697 static int
GUC_flex_fatal(const char * msg)698 GUC_flex_fatal(const char *msg)
699 {
700 	GUC_flex_fatal_errmsg = msg;
701 	siglongjmp(*GUC_flex_fatal_jmp, 1);
702 	return 0;					/* keep compiler quiet */
703 }
704 
705 /*
706  * Read and parse a single configuration file.  This function recurses
707  * to handle "include" directives.
708  *
709  * Input parameters:
710  *	fp: file pointer from AllocateFile for the configuration file to parse
711  *	config_file: absolute or relative path name of the configuration file
712  *	depth: recursion depth (should be 0 in the outermost call)
713  *	elevel: error logging level to use
714  * Input/Output parameters:
715  *	head_p, tail_p: head and tail of linked list of name/value pairs
716  *
717  * *head_p and *tail_p must be initialized, either to NULL or valid pointers
718  * to a ConfigVariable list, before calling the outer recursion level.  Any
719  * name-value pairs read from the input file(s) will be appended to the list.
720  * Error reports will also be appended to the list, if elevel < ERROR.
721  *
722  * Returns TRUE if successful, FALSE if an error occurred.  The error has
723  * already been ereport'd, it is only necessary for the caller to clean up
724  * its own state and release the ConfigVariable list.
725  *
726  * Note: if elevel >= ERROR then an error will not return control to the
727  * caller, so there is no need to check the return value in that case.
728  *
729  * Note: this function is used to parse not only postgresql.conf, but
730  * various other configuration files that use the same "name = value"
731  * syntax.  Hence, do not do anything here or in the subsidiary routines
732  * ParseConfigFile/ParseConfigDirectory that assumes we are processing
733  * GUCs specifically.
734  */
735 bool
ParseConfigFp(FILE * fp,const char * config_file,int depth,int elevel,ConfigVariable ** head_p,ConfigVariable ** tail_p)736 ParseConfigFp(FILE *fp, const char *config_file, int depth, int elevel,
737 			  ConfigVariable **head_p, ConfigVariable **tail_p)
738 {
739 	volatile bool OK = true;
740 	unsigned int save_ConfigFileLineno = ConfigFileLineno;
741 	sigjmp_buf *save_GUC_flex_fatal_jmp = GUC_flex_fatal_jmp;
742 	sigjmp_buf	flex_fatal_jmp;
743 	volatile YY_BUFFER_STATE lex_buffer = NULL;
744 	int			errorcount;
745 	int			token;
746 
747 	if (sigsetjmp(flex_fatal_jmp, 1) == 0)
748 		GUC_flex_fatal_jmp = &flex_fatal_jmp;
749 	else
750 	{
751 		/*
752 		 * Regain control after a fatal, internal flex error.  It may have
753 		 * corrupted parser state.  Consequently, abandon the file, but trust
754 		 * that the state remains sane enough for yy_delete_buffer().
755 		 */
756 		elog(elevel, "%s at file \"%s\" line %u",
757 			 GUC_flex_fatal_errmsg, config_file, ConfigFileLineno);
758 		record_config_file_error(GUC_flex_fatal_errmsg,
759 								 config_file, ConfigFileLineno,
760 								 head_p, tail_p);
761 		OK = false;
762 		goto cleanup;
763 	}
764 
765 	/*
766 	 * Parse
767 	 */
768 	ConfigFileLineno = 1;
769 	errorcount = 0;
770 
771 	lex_buffer = yy_create_buffer(fp, YY_BUF_SIZE);
772 	yy_switch_to_buffer(lex_buffer);
773 
774 	/* This loop iterates once per logical line */
775 	while ((token = yylex()))
776 	{
777 		char	   *opt_name = NULL;
778 		char	   *opt_value = NULL;
779 		ConfigVariable *item;
780 
781 		if (token == GUC_EOL)	/* empty or comment line */
782 			continue;
783 
784 		/* first token on line is option name */
785 		if (token != GUC_ID && token != GUC_QUALIFIED_ID)
786 			goto parse_error;
787 		opt_name = pstrdup(yytext);
788 
789 		/* next we have an optional equal sign; discard if present */
790 		token = yylex();
791 		if (token == GUC_EQUALS)
792 			token = yylex();
793 
794 		/* now we must have the option value */
795 		if (token != GUC_ID &&
796 			token != GUC_STRING &&
797 			token != GUC_INTEGER &&
798 			token != GUC_REAL &&
799 			token != GUC_UNQUOTED_STRING)
800 			goto parse_error;
801 		if (token == GUC_STRING)	/* strip quotes and escapes */
802 			opt_value = GUC_scanstr(yytext);
803 		else
804 			opt_value = pstrdup(yytext);
805 
806 		/* now we'd like an end of line, or possibly EOF */
807 		token = yylex();
808 		if (token != GUC_EOL)
809 		{
810 			if (token != 0)
811 				goto parse_error;
812 			/* treat EOF like \n for line numbering purposes, cf bug 4752 */
813 			ConfigFileLineno++;
814 		}
815 
816 		/* OK, process the option name and value */
817 		if (guc_name_compare(opt_name, "include_dir") == 0)
818 		{
819 			/*
820 			 * An include_dir directive isn't a variable and should be
821 			 * processed immediately.
822 			 */
823 			if (!ParseConfigDirectory(opt_value,
824 									  config_file, ConfigFileLineno - 1,
825 									  depth + 1, elevel,
826 									  head_p, tail_p))
827 				OK = false;
828 			yy_switch_to_buffer(lex_buffer);
829 			pfree(opt_name);
830 			pfree(opt_value);
831 		}
832 		else if (guc_name_compare(opt_name, "include_if_exists") == 0)
833 		{
834 			/*
835 			 * An include_if_exists directive isn't a variable and should be
836 			 * processed immediately.
837 			 */
838 			if (!ParseConfigFile(opt_value, false,
839 								 config_file, ConfigFileLineno - 1,
840 								 depth + 1, elevel,
841 								 head_p, tail_p))
842 				OK = false;
843 			yy_switch_to_buffer(lex_buffer);
844 			pfree(opt_name);
845 			pfree(opt_value);
846 		}
847 		else if (guc_name_compare(opt_name, "include") == 0)
848 		{
849 			/*
850 			 * An include directive isn't a variable and should be processed
851 			 * immediately.
852 			 */
853 			if (!ParseConfigFile(opt_value, true,
854 								 config_file, ConfigFileLineno - 1,
855 								 depth + 1, elevel,
856 								 head_p, tail_p))
857 				OK = false;
858 			yy_switch_to_buffer(lex_buffer);
859 			pfree(opt_name);
860 			pfree(opt_value);
861 		}
862 		else
863 		{
864 			/* ordinary variable, append to list */
865 			item = palloc(sizeof *item);
866 			item->name = opt_name;
867 			item->value = opt_value;
868 			item->errmsg = NULL;
869 			item->filename = pstrdup(config_file);
870 			item->sourceline = ConfigFileLineno - 1;
871 			item->ignore = false;
872 			item->applied = false;
873 			item->next = NULL;
874 			if (*head_p == NULL)
875 				*head_p = item;
876 			else
877 				(*tail_p)->next = item;
878 			*tail_p = item;
879 		}
880 
881 		/* break out of loop if read EOF, else loop for next line */
882 		if (token == 0)
883 			break;
884 		continue;
885 
886 parse_error:
887 		/* release storage if we allocated any on this line */
888 		if (opt_name)
889 			pfree(opt_name);
890 		if (opt_value)
891 			pfree(opt_value);
892 
893 		/* report the error */
894 		if (token == GUC_EOL || token == 0)
895 		{
896 			ereport(elevel,
897 					(errcode(ERRCODE_SYNTAX_ERROR),
898 			  errmsg("syntax error in file \"%s\" line %u, near end of line",
899 					 config_file, ConfigFileLineno - 1)));
900 			record_config_file_error("syntax error",
901 									 config_file, ConfigFileLineno - 1,
902 									 head_p, tail_p);
903 		}
904 		else
905 		{
906 			ereport(elevel,
907 					(errcode(ERRCODE_SYNTAX_ERROR),
908 			 errmsg("syntax error in file \"%s\" line %u, near token \"%s\"",
909 					config_file, ConfigFileLineno, yytext)));
910 			record_config_file_error("syntax error",
911 									 config_file, ConfigFileLineno,
912 									 head_p, tail_p);
913 		}
914 		OK = false;
915 		errorcount++;
916 
917 		/*
918 		 * To avoid producing too much noise when fed a totally bogus file,
919 		 * give up after 100 syntax errors per file (an arbitrary number).
920 		 * Also, if we're only logging the errors at DEBUG level anyway, might
921 		 * as well give up immediately.  (This prevents postmaster children
922 		 * from bloating the logs with duplicate complaints.)
923 		 */
924 		if (errorcount >= 100 || elevel <= DEBUG1)
925 		{
926 			ereport(elevel,
927 					(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
928 			   errmsg("too many syntax errors found, abandoning file \"%s\"",
929 					  config_file)));
930 			break;
931 		}
932 
933 		/* resync to next end-of-line or EOF */
934 		while (token != GUC_EOL && token != 0)
935 			token = yylex();
936 		/* break out of loop on EOF */
937 		if (token == 0)
938 			break;
939 	}
940 
941 cleanup:
942 	yy_delete_buffer(lex_buffer);
943 	/* Each recursion level must save and restore these static variables. */
944 	ConfigFileLineno = save_ConfigFileLineno;
945 	GUC_flex_fatal_jmp = save_GUC_flex_fatal_jmp;
946 	return OK;
947 }
948 
949 /*
950  * Read and parse all config files in a subdirectory in alphabetical order
951  *
952  * includedir is the absolute or relative path to the subdirectory to scan.
953  *
954  * calling_file/calling_lineno identify the source of the request.
955  * Pass NULL/0 if not recursing from an inclusion request.
956  *
957  * See ParseConfigFp for further details.
958  */
959 bool
ParseConfigDirectory(const char * includedir,const char * calling_file,int calling_lineno,int depth,int elevel,ConfigVariable ** head_p,ConfigVariable ** tail_p)960 ParseConfigDirectory(const char *includedir,
961 					 const char *calling_file, int calling_lineno,
962 					 int depth, int elevel,
963 					 ConfigVariable **head_p,
964 					 ConfigVariable **tail_p)
965 {
966 	char	   *directory;
967 	DIR		   *d;
968 	struct dirent *de;
969 	char	  **filenames;
970 	int			num_filenames;
971 	int			size_filenames;
972 	bool		status;
973 
974 	/*
975 	 * Reject directory name that is all-blank (including empty), as that
976 	 * leads to confusion --- we'd read the containing directory, typically
977 	 * resulting in recursive inclusion of the same file(s).
978 	 */
979 	if (strspn(includedir, " \t\r\n") == strlen(includedir))
980 	{
981 		ereport(elevel,
982 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
983 				 errmsg("empty configuration directory name: \"%s\"",
984 						includedir)));
985 		record_config_file_error("empty configuration directory name",
986 								 calling_file, calling_lineno,
987 								 head_p, tail_p);
988 		return false;
989 	}
990 
991 	/*
992 	 * We don't check for recursion or too-deep nesting depth here; the
993 	 * subsequent calls to ParseConfigFile will take care of that.
994 	 */
995 
996 	directory = AbsoluteConfigLocation(includedir, calling_file);
997 	d = AllocateDir(directory);
998 	if (d == NULL)
999 	{
1000 		ereport(elevel,
1001 				(errcode_for_file_access(),
1002 				 errmsg("could not open configuration directory \"%s\": %m",
1003 						directory)));
1004 		record_config_file_error(psprintf("could not open directory \"%s\"",
1005 										  directory),
1006 								 calling_file, calling_lineno,
1007 								 head_p, tail_p);
1008 		status = false;
1009 		goto cleanup;
1010 	}
1011 
1012 	/*
1013 	 * Read the directory and put the filenames in an array, so we can sort
1014 	 * them prior to processing the contents.
1015 	 */
1016 	size_filenames = 32;
1017 	filenames = (char **) palloc(size_filenames * sizeof(char *));
1018 	num_filenames = 0;
1019 
1020 	while ((de = ReadDir(d, directory)) != NULL)
1021 	{
1022 		struct stat st;
1023 		char		filename[MAXPGPATH];
1024 
1025 		/*
1026 		 * Only parse files with names ending in ".conf".  Explicitly reject
1027 		 * files starting with ".".  This excludes things like "." and "..",
1028 		 * as well as typical hidden files, backup files, and editor debris.
1029 		 */
1030 		if (strlen(de->d_name) < 6)
1031 			continue;
1032 		if (de->d_name[0] == '.')
1033 			continue;
1034 		if (strcmp(de->d_name + strlen(de->d_name) - 5, ".conf") != 0)
1035 			continue;
1036 
1037 		join_path_components(filename, directory, de->d_name);
1038 		canonicalize_path(filename);
1039 		if (stat(filename, &st) == 0)
1040 		{
1041 			if (!S_ISDIR(st.st_mode))
1042 			{
1043 				/* Add file to array, increasing its size in blocks of 32 */
1044 				if (num_filenames >= size_filenames)
1045 				{
1046 					size_filenames += 32;
1047 					filenames = (char **) repalloc(filenames,
1048 											size_filenames * sizeof(char *));
1049 				}
1050 				filenames[num_filenames] = pstrdup(filename);
1051 				num_filenames++;
1052 			}
1053 		}
1054 		else
1055 		{
1056 			/*
1057 			 * stat does not care about permissions, so the most likely reason
1058 			 * a file can't be accessed now is if it was removed between the
1059 			 * directory listing and now.
1060 			 */
1061 			ereport(elevel,
1062 					(errcode_for_file_access(),
1063 					 errmsg("could not stat file \"%s\": %m",
1064 							filename)));
1065 			record_config_file_error(psprintf("could not stat file \"%s\"",
1066 											  filename),
1067 									 calling_file, calling_lineno,
1068 									 head_p, tail_p);
1069 			status = false;
1070 			goto cleanup;
1071 		}
1072 	}
1073 
1074 	if (num_filenames > 0)
1075 	{
1076 		int			i;
1077 
1078 		qsort(filenames, num_filenames, sizeof(char *), pg_qsort_strcmp);
1079 		for (i = 0; i < num_filenames; i++)
1080 		{
1081 			if (!ParseConfigFile(filenames[i], true,
1082 								 calling_file, calling_lineno,
1083 								 depth, elevel,
1084 								 head_p, tail_p))
1085 			{
1086 				status = false;
1087 				goto cleanup;
1088 			}
1089 		}
1090 	}
1091 	status = true;
1092 
1093 cleanup:
1094 	if (d)
1095 		FreeDir(d);
1096 	pfree(directory);
1097 	return status;
1098 }
1099 
1100 /*
1101  * Free a list of ConfigVariables, including the names and the values
1102  */
1103 void
FreeConfigVariables(ConfigVariable * list)1104 FreeConfigVariables(ConfigVariable *list)
1105 {
1106 	ConfigVariable *item;
1107 
1108 	item = list;
1109 	while (item)
1110 	{
1111 		ConfigVariable *next = item->next;
1112 
1113 		FreeConfigVariable(item);
1114 		item = next;
1115 	}
1116 }
1117 
1118 /*
1119  * Free a single ConfigVariable
1120  */
1121 static void
FreeConfigVariable(ConfigVariable * item)1122 FreeConfigVariable(ConfigVariable *item)
1123 {
1124 	if (item->name)
1125 		pfree(item->name);
1126 	if (item->value)
1127 		pfree(item->value);
1128 	if (item->errmsg)
1129 		pfree(item->errmsg);
1130 	if (item->filename)
1131 		pfree(item->filename);
1132 	pfree(item);
1133 }
1134 
1135 
1136 /*
1137  *		scanstr
1138  *
1139  * Strip the quotes surrounding the given string, and collapse any embedded
1140  * '' sequences and backslash escapes.
1141  *
1142  * the string returned is palloc'd and should eventually be pfree'd by the
1143  * caller.
1144  */
1145 static char *
GUC_scanstr(const char * s)1146 GUC_scanstr(const char *s)
1147 {
1148 	char	   *newStr;
1149 	int			len,
1150 				i,
1151 				j;
1152 
1153 	Assert(s != NULL && s[0] == '\'');
1154 	len = strlen(s);
1155 	Assert(len >= 2);
1156 	Assert(s[len - 1] == '\'');
1157 
1158 	/* Skip the leading quote; we'll handle the trailing quote below */
1159 	s++, len--;
1160 
1161 	/* Since len still includes trailing quote, this is enough space */
1162 	newStr = palloc(len);
1163 
1164 	for (i = 0, j = 0; i < len; i++)
1165 	{
1166 		if (s[i] == '\\')
1167 		{
1168 			i++;
1169 			switch (s[i])
1170 			{
1171 				case 'b':
1172 					newStr[j] = '\b';
1173 					break;
1174 				case 'f':
1175 					newStr[j] = '\f';
1176 					break;
1177 				case 'n':
1178 					newStr[j] = '\n';
1179 					break;
1180 				case 'r':
1181 					newStr[j] = '\r';
1182 					break;
1183 				case 't':
1184 					newStr[j] = '\t';
1185 					break;
1186 				case '0':
1187 				case '1':
1188 				case '2':
1189 				case '3':
1190 				case '4':
1191 				case '5':
1192 				case '6':
1193 				case '7':
1194 					{
1195 						int			k;
1196 						long		octVal = 0;
1197 
1198 						for (k = 0;
1199 							 s[i + k] >= '0' && s[i + k] <= '7' && k < 3;
1200 							 k++)
1201 							octVal = (octVal << 3) + (s[i + k] - '0');
1202 						i += k - 1;
1203 						newStr[j] = ((char) octVal);
1204 					}
1205 					break;
1206 				default:
1207 					newStr[j] = s[i];
1208 					break;
1209 			}					/* switch */
1210 		}
1211 		else if (s[i] == '\'' && s[i + 1] == '\'')
1212 		{
1213 			/* doubled quote becomes just one quote */
1214 			newStr[j] = s[++i];
1215 		}
1216 		else
1217 			newStr[j] = s[i];
1218 		j++;
1219 	}
1220 
1221 	/* We copied the ending quote to newStr, so replace with \0 */
1222 	Assert(j > 0 && j <= len);
1223 	newStr[--j] = '\0';
1224 
1225 	return newStr;
1226 }
1227