1 /*
2  * ProFTPD - FTP server daemon
3  * Copyright (c) 2004-2016 The ProFTPD Project team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18  *
19  * As a special exemption, The ProFTPD Project team and other respective
20  * copyright holders give permission to link this program with OpenSSL, and
21  * distribute the resulting executable, without including the source code for
22  * OpenSSL in the source distribution.
23  */
24 
25 /* Configuration parser */
26 
27 #ifndef PR_PARSER_H
28 #define PR_PARSER_H
29 
30 /* Prepares the parser, allocating any necessary internal resources from
31  * the given pool.  If provided, parsed_servers will, after parsing is
32  * complete, contain a pointer to the list of configured server_rec
33  * server configurations.
34  */
35 int pr_parser_prepare(pool *p, xaset_t **parsed_servers);
36 
37 /* Clears any internal state of the parser.  This function should always
38  * be called after any parsing.
39  */
40 int pr_parser_cleanup(void);
41 
42 /* Called to push a "start-of-context" configuration marker onto the
43  * parser stack.  The name of the configuration context (.e.g "Directory"
44  * or "Anonymous") is provided by the name parameter.
45  */
46 config_rec *pr_parser_config_ctxt_open(const char *name);
47 
48 /* Called to push an "end-of-context" configuration marker onto the
49  * parser stack.  If the parser determines that the configuration
50  * context being closed is empty, it will remove the entire context from
51  * the parser stacks: empty contexts are superfluous.  If this happens,
52  * the isempty parameter, if non-NULL, will be set to TRUE.
53  */
54 config_rec *pr_parser_config_ctxt_close(int *isempty);
55 
56 /* Push the config_rec onto the parser stack directly.  This function can
57  * be used, instead of the open/close semantics, for cases where the config_rec
58  * is constructed by means other than file parsing.
59  */
60 int pr_parser_config_ctxt_push(config_rec *c);
61 
62 /* Returns a pointer to the current configuration record on the parser
63  * configuration stack.
64  */
65 config_rec *pr_parser_config_ctxt_get(void);
66 
67 /* Returns the line number of the configuration stream being parsed. */
68 unsigned int pr_parser_get_lineno(void);
69 
70 /* This is the main function to be used by consumers of the Parser
71  * API.  Given a pool, a path to a file containing configuration text,
72  * and a starting configuration context, the function will open and
73  * parse the given data.
74  *
75  * In almost all cases, the starting configuration context given by the
76  * start parameter is NULL, indicating that the path being parsed is
77  * not part of any existing configuration tree.  The start parameter will
78  * be non-NULL in the case of files such as .ftpaccess files, which are
79  * part of the existing configuration tree.
80  *
81  * The flags parameter is used to indicate to the parser what type of
82  * stream is being parsed.  The PR_PARSER_FL_DYNAMIC_CONFIG flag is
83  * used when handling .ftpaccess files, so that the function will treat
84  * unknown directives as warnings, rather than as fatal errors.
85  */
86 int pr_parser_parse_file(pool *p, const char *path, config_rec *start,
87   int flags);
88 #define PR_PARSER_FL_DYNAMIC_CONFIG	0x0001
89 
90 /* The dispatching of configuration data to the registered configuration
91  * handlers is done using a cmd_rec.  This function parses the given line of
92  * text, then allocates a cmd_rec from the given pool p and populates the
93  * struct with data from the line of text.
94  */
95 cmd_rec *pr_parser_parse_line(pool *p, const char *text, size_t text_len);
96 
97 /* This convenience function reads the next line from the configuration
98  * stream, performing any necessary transformations on the text (e.g.
99  * skipping comments, trimming leading and trailing spaces, etc).  NULL
100  * is returned if there are no more lines of configuration text in the
101  * the stream.
102  *
103  * The configuration stream itself is not provided by the caller deliberately;
104  * this allows callers who do not have access to the configuration stream
105  * to read data from it.
106  */
107 char *pr_parser_read_line(char *buf, size_t bufsz);
108 
109 /* Called to push a "start-of-server" configuration marker onto the
110  * parser stack.  The name of the server context, usually a string
111  * containing a DNS name or an IP address, is provided by the addrstr
112  * parameter.
113  */
114 server_rec *pr_parser_server_ctxt_open(const char *addrstr);
115 
116 /* Called to push an "end-of-server" configuration record onto the
117  * parser stack.  If the parser determines that the server context being
118  * closed is empty, it will remove the entire context from the parser stacks:
119  * empty contexts are superfluous.
120  */
121 server_rec *pr_parser_server_ctxt_close(void);
122 
123 /* Push the server_rec onto the parser stack directly.  This function can
124  * be used, instead of the open/close semantics, for cases where the server_rec
125  * is constructed by means other than file parsing.
126  */
127 int pr_parser_server_ctxt_push(server_rec *s);
128 
129 /* Returns a pointer to the current server record on the parser server
130  * stack.
131  */
132 server_rec *pr_parser_server_ctxt_get(void);
133 
134 /* Configure optional Include behavior. Returns the previously set options. */
135 unsigned long pr_parser_set_include_opts(unsigned long opts);
136 #define PR_PARSER_INCLUDE_OPT_ALLOW_SYMLINKS		0x0001
137 #define PR_PARSER_INCLUDE_OPT_IGNORE_TMP_FILES		0x0002
138 #define PR_PARSER_INCLUDE_OPT_IGNORE_WILDCARDS		0x0004
139 
140 /* Internal use only */
141 int parse_config_path(pool *p, const char *path);
142 int parse_config_path2(pool *p, const char *path, unsigned int depth);
143 
144 #endif /* PR_PARSER_H */
145