1 /*
2  *
3   ***** BEGIN LICENSE BLOCK *****
4 
5   Copyright (C) 2009-2016 Olof Hagsand and Benny Holmgren
6   Copyright (C) 2017-2019 Olof Hagsand
7   Copyright (C) 2020 Olof Hagsand and Rubicon Communications, LLC(Netgate)
8 
9   This file is part of CLIXON.
10 
11   Licensed under the Apache License, Version 2.0 (the "License");
12   you may not use this file except in compliance with the License.
13   You may obtain a copy of the License at
14 
15     http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22 
23   Alternatively, the contents of this file may be used under the terms of
24   the GNU General Public License Version 3 or later (the "GPL"),
25   in which case the provisions of the GPL are applicable instead
26   of those above. If you wish to allow use of your version of this file only
27   under the terms of the GPL, and not to allow others to
28   use your version of this file under the terms of Apache License version 2,
29   indicate your decision by deleting the provisions above and replace them with
30   the  notice and other provisions required by the GPL. If you do not delete
31   the provisions above, a recipient may use your version of this file under
32   the terms of any one of the Apache License version 2 or the GPL.
33 
34   ***** END LICENSE BLOCK *****
35 
36  * This file contains access functions for two types of clixon vars:
37  * - options, ie string based variables from Clixon configuration files.
38  *            Accessed with clicon_options(h).
39  * @see clixon_data.[ch] for free-type runtime get/set *
40  */
41 
42 #ifndef _CLIXON_OPTIONS_H_
43 #define _CLIXON_OPTIONS_H_
44 
45 /*
46  * Constants
47  */
48 /*! Clixon configuration namespace
49  * Probably should be defined somewhere else or extracted from yang
50  * @see clixon-config.yang
51  * @see clixon-lib.yang
52  */
53 #define CLIXON_CONF_NS "http://clicon.org/config"
54 #define CLIXON_LIB_NS "http://clicon.org/lib"
55 
56 /*
57  * Types
58  */
59 /*! Controls how keywords a generated in CLI syntax / prints from object model
60  * Example YANG:
61  * container c{
62  *  list a {
63  *    key x;
64  *    leaf x;
65  *    leaf y;
66  *  }
67  * }
68  * NONE: c a <x> <y>;
69  * VARS: c a <x> y <y>;
70  * ALL:  c a x <x> y <y>;
71  * HIDE: a x <x> y <y>;
72  */
73 enum genmodel_type{
74     GT_ERR =-1, /* Error  */
75     GT_NONE=0,  /* No extra keywords */
76     GT_VARS,    /* Keywords on non-key variables */
77     GT_ALL,     /* Keywords on all variables */
78     GT_HIDE,    /* Keywords on all variables and hide container around lists */
79 };
80 
81 /*! See clixon-config.yang type startup_mode */
82 enum startup_mode_t{
83     SM_NONE=0,
84     SM_STARTUP,
85     SM_RUNNING,
86     SM_INIT
87 };
88 
89 /*! See clixon-config.yang type priv_mode (privileges mode) */
90 enum priv_mode_t{
91     PM_NONE=0,        /* Make no drop/change in privileges */
92     PM_DROP_PERM,     /* Drop privileges permanently */
93     PM_DROP_TEMP      /* Drop privileges temporary */
94 };
95 
96 /*! See clixon-config.yang type nacm_cred_mode (user credentials) */
97 enum nacm_credentials_t{
98     NC_NONE=0,   /* Dont match NACM user to any user credentials.  */
99     NC_EXACT,    /* Exact match between NACM user and unix socket peer user. */
100     NC_EXCEPT    /* Exact match except for root and www user  */
101 };
102 
103 /*! Datastore cache behaviour, see clixon_datastore.[ch]
104  * See config option type datastore_cache in clixon-config.yang
105  */
106 enum datastore_cache{
107     DATASTORE_NOCACHE,
108     DATASTORE_CACHE,
109     DATASTORE_CACHE_ZEROCOPY
110 };
111 
112 /*! yang clixon regexp engine
113  * @see regexp_mode in clixon-config.yang
114  */
115 enum regexp_mode{
116     REGEXP_POSIX,
117     REGEXP_LIBXML2
118 };
119 
120 /*
121  * Prototypes
122  */
123 
124 /* Print registry on file. For debugging. */
125 int clicon_option_dump(clicon_handle h, int dblevel);
126 
127 /* Add a clicon options overriding file setting */
128 int clicon_option_add(clicon_handle h, const char *name, char *value);
129 
130 /* Initialize options: set defaults, read config-file, etc */
131 int clicon_options_main(clicon_handle h);
132 
133 /*! Check if a clicon option has a value */
134 int clicon_option_exists(clicon_handle h, const char *name);
135 
136 /* String options, default NULL */
137 char *clicon_option_str(clicon_handle h, const char *name);
138 int clicon_option_str_set(clicon_handle h, const char *name, char *val);
139 
140 /* Option values gixen as int, default -1 */
141 int clicon_option_int(clicon_handle h, const char *name);
142 int clicon_option_int_set(clicon_handle h, const char *name, int val);
143 
144 /* Option values gixen as bool, default false */
145 int clicon_option_bool(clicon_handle h, const char *name);
146 int clicon_option_bool_set(clicon_handle h, const char *name, int val);
147 
148 /* Delete a single option via handle */
149 int clicon_option_del(clicon_handle h, const char *name);
150 
151 /*-- Standard option access functions for YANG options --*/
clicon_configfile(clicon_handle h)152 static inline char *clicon_configfile(clicon_handle h){
153     return clicon_option_str(h, "CLICON_CONFIGFILE");
154 }
clicon_yang_main_file(clicon_handle h)155 static inline char *clicon_yang_main_file(clicon_handle h){
156     return clicon_option_str(h, "CLICON_YANG_MAIN_FILE");
157 }
clicon_yang_main_dir(clicon_handle h)158 static inline char *clicon_yang_main_dir(clicon_handle h){
159     return clicon_option_str(h, "CLICON_YANG_MAIN_DIR");
160 }
clicon_yang_module_main(clicon_handle h)161 static inline char *clicon_yang_module_main(clicon_handle h){
162     return clicon_option_str(h, "CLICON_YANG_MODULE_MAIN");
163 }
clicon_yang_module_revision(clicon_handle h)164 static inline char *clicon_yang_module_revision(clicon_handle h){
165     return clicon_option_str(h, "CLICON_YANG_MODULE_REVISION");
166 }
clicon_backend_dir(clicon_handle h)167 static inline char *clicon_backend_dir(clicon_handle h){
168     return clicon_option_str(h, "CLICON_BACKEND_DIR");
169 }
clicon_netconf_dir(clicon_handle h)170 static inline char *clicon_netconf_dir(clicon_handle h){
171     return clicon_option_str(h, "CLICON_NETCONF_DIR");
172 }
clicon_restconf_dir(clicon_handle h)173 static inline char *clicon_restconf_dir(clicon_handle h){
174     return clicon_option_str(h, "CLICON_RESTCONF_DIR");
175 }
clicon_cli_dir(clicon_handle h)176 static inline char *clicon_cli_dir(clicon_handle h){
177     return clicon_option_str(h, "CLICON_CLI_DIR");
178 }
clicon_clispec_dir(clicon_handle h)179 static inline char *clicon_clispec_dir(clicon_handle h){
180     return clicon_option_str(h, "CLICON_CLISPEC_DIR");
181 }
clicon_cli_mode(clicon_handle h)182 static inline char *clicon_cli_mode(clicon_handle h){
183     return clicon_option_str(h, "CLICON_CLI_MODE");
184 }
clicon_cli_tab_mode(clicon_handle h)185 static inline int clicon_cli_tab_mode(clicon_handle h){
186     return clicon_option_int(h, "CLICON_CLI_TAB_MODE");
187 }
clicon_cli_model_treename(clicon_handle h)188 static inline char *clicon_cli_model_treename(clicon_handle h){
189     return clicon_option_str(h, "CLICON_CLI_MODEL_TREENAME");
190 }
clicon_sock(clicon_handle h)191 static inline char *clicon_sock(clicon_handle h){
192     return clicon_option_str(h, "CLICON_SOCK");
193 }
clicon_sock_group(clicon_handle h)194 static inline char *clicon_sock_group(clicon_handle h){
195     return clicon_option_str(h, "CLICON_SOCK_GROUP");
196 }
clicon_backend_user(clicon_handle h)197 static inline char *clicon_backend_user(clicon_handle h){
198     return clicon_option_str(h, "CLICON_BACKEND_USER");
199 }
clicon_backend_pidfile(clicon_handle h)200 static inline char *clicon_backend_pidfile(clicon_handle h){
201     return clicon_option_str(h, "CLICON_BACKEND_PIDFILE");
202 }
clicon_xmldb_dir(clicon_handle h)203 static inline char *clicon_xmldb_dir(clicon_handle h){
204     return clicon_option_str(h, "CLICON_XMLDB_DIR");
205 }
clicon_nacm_recovery_user(clicon_handle h)206 static inline char *clicon_nacm_recovery_user(clicon_handle h){
207     return clicon_option_str(h, "CLICON_NACM_RECOVERY_USER");
208 }
209 
210 /*-- Specific option access functions for YANG options w type conversion--*/
211 int   clicon_cli_genmodel(clicon_handle h);
212 int   clicon_cli_genmodel_completion(clicon_handle h);
213 enum genmodel_type clicon_cli_genmodel_type(clicon_handle h);
214 int   clicon_cli_varonly(clicon_handle h);
215 int   clicon_sock_family(clicon_handle h);
216 int   clicon_sock_port(clicon_handle h);
217 int   clicon_autocommit(clicon_handle h);
218 int   clicon_startup_mode(clicon_handle h);
219 enum priv_mode_t clicon_backend_privileges_mode(clicon_handle h);
220 enum nacm_credentials_t clicon_nacm_credentials(clicon_handle h);
221 
222 enum datastore_cache clicon_datastore_cache(clicon_handle h);
223 enum regexp_mode clicon_yang_regexp(clicon_handle h);
224 /*-- Specific option access functions for non-yang options --*/
225 int clicon_quiet_mode(clicon_handle h);
226 int clicon_quiet_mode_set(clicon_handle h, int val);
227 
228 #endif  /* _CLIXON_OPTIONS_H_ */
229