Sanitize all collected default values to empty fields
[pyi2ncommon] / src / cnfvar / templates.py
CommitLineData
11cbb815
PD
1# The software in this package is distributed under the GNU General
2# Public License version 2 (with a special exception described below).
3#
4# A copy of GNU General Public License (GPL) is included in this distribution,
5# in the file COPYING.GPL.
6#
7# As a special exception, if other files instantiate templates or use macros
8# or inline functions from this file, or you compile this file and link it
9# with other works to produce a work based on this file, this file
10# does not by itself cause the resulting work to be covered
11# by the GNU General Public License.
12#
13# However the source code for this file must still be made available
14# in accordance with section (3) of the GNU General Public License.
15#
16# This exception does not invalidate any other reasons why a work based
17# on this file might be covered by the GNU General Public License.
18#
19# Copyright (c) 2016-2018 Intra2net AG <info@intra2net.com>
20
f49f6323
PD
21"""
22
b5abc93a 23summary
f49f6323 24------------------------------------------------------
188605ae 25Module for one-step dynamic cnfvar generation from default value templates.
f49f6323 26
b5abc93a 27.. codeauthor:: Intra2net
b7e04a3e 28
f49f6323 29
188605ae
PD
30contents
31-------------------------------------------------------
32These templates contain the bare defaults the UI adds upon
33creation of each major and frequently used cnfvar.
34
35
b5abc93a 36interface
f49f6323
PD
37------------------------------------------------------
38
39"""
40
41import time
42import logging
f49f6323
PD
43
44# custom imports
b5abc93a
PD
45from .model import Cnf, CnfList
46
47
48log = logging.getLogger('pyi2ncommon.cnfvar.templates')
f49f6323
PD
49
50
51###############################################################################
188605ae
PD
52# MAJOR CNF DEFAULTS
53###############################################################################
54
55
56#: UI defaults for a user instance
57user_defaults = {
58 "USER_DISABLED": "0",
59 "USER_FULLNAME": "",
4fef584f
PD
60 "USER_GROUPWARE_FOLDER_CALENDAR": "INBOX/Kalender",
61 "USER_GROUPWARE_FOLDER_CONTACTS": "INBOX/Kontakte",
62 "USER_GROUPWARE_FOLDER_DRAFTS": "INBOX/Entwürfe",
63 "USER_GROUPWARE_FOLDER_NOTES": "INBOX/Notizen",
64 "USER_GROUPWARE_FOLDER_OUTBOX": "INBOX/Gesendete Elemente",
65 "USER_GROUPWARE_FOLDER_TASKS": "INBOX/Aufgaben",
66 "USER_GROUPWARE_FOLDER_TRASH": "INBOX/Gelöschte Elemente",
188605ae
PD
67 # always a member of the 'Alle' group
68 "USER_GROUP_MEMBER_REF": "2",
69 "USER_LOCALE": "",
be8c26c1 70 "USER_PASSWORD": "",
188605ae
PD
71 "USER_TRASH_DELETEDAYS": "30",
72 "USER_WEBMAIL_MESSAGES_PER_PAGE": "25",
73 "USER_WEBMAIL_SIGNATURE": "",
74}
b358e4cb
PD
75#: UI defaults for a group instance
76group_defaults = {
77 "GROUP_COMMENT": "",
78 "GROUP_ACCESS_GO_ONLINE_ALLOWED": "1",
79 "GROUP_EMAILFILTER_BAN_FILTERLIST_REF": "-1",
80 "GROUP_EMAIL_RELAY_RIGHTS": "RELAY_FROM_INTRANET",
81 "GROUP_PROXY_PROFILE_REF": "1",
82}
13abc7d0
PD
83#: UI defaults for an intraclient instance
84intraclient_defaults = {
85 "INTRACLIENT_COMMENT": "",
86 "INTRACLIENT_DNS_RELAYING_ALLOWED": "1",
87 "INTRACLIENT_EMAIL_RELAYING_ALLOWED": "1",
88 "INTRACLIENT_FIREWALL_RULESET_REF": "5",
df3ad4c0
PD
89 "INTRACLIENT_IP": "",
90 "INTRACLIENT_MAC": "",
13abc7d0
PD
91 "INTRACLIENT_PROXY_PROFILE_REF": "-1",
92}
93#: UI defaults for a NIC instance
94nic_defaults = {
95 "NIC_COMMENT": "",
be8c26c1
PD
96 "NIC_DRIVER": "",
97 "NIC_MAC": "",
4fef584f 98 "NIC_TYPE": "UNUSED",
13abc7d0
PD
99}
100#: UI defaults for a provider instance
101provider_defaults = {
102 "PROVIDER_PROXY_SERVER": "",
103 "PROVIDER_PROXY_PORT": "",
104 "PROVIDER_PROXY_PASSWORD": "",
105 "PROVIDER_PROXY_LOGIN": "",
106 "PROVIDER_NIC_REF": "1",
107 "PROVIDER_NETMASK": "255.255.0.0",
108 "PROVIDER_MTU_SIZE": "1500",
109 "PROVIDER_MODE": "ROUTER",
110 "PROVIDER_MAILTRANSFER_MODE": "IMMEDIATE",
111 "PROVIDER_LOCALIP": "",
112 "PROVIDER_IP": "",
113 "PROVIDER_FIREWALL_RULESET_REF": "7",
114 "PROVIDER_FALLBACK_TIMEOUT": "60",
115 "PROVIDER_FALLBACK_PROVIDER_REF": "-1",
116 "PROVIDER_EMAIL_RELAY_REF": "-1",
117 "PROVIDER_DYNDNS_WEBCHECKIP": "0",
118 "PROVIDER_DYNDNS_ENABLE": "1",
4fef584f 119 "PROVIDER_DNS_MODE": "ROOT",
13abc7d0
PD
120 "PROVIDER_DNS": "",
121 "PROVIDER_BWIDTH_MANAGEMENT_UPSTREAM_SPEED": "",
122 "PROVIDER_BWIDTH_MANAGEMENT_ENABLE": "0",
123 "PROVIDER_BWIDTH_MANAGEMENT_DOWNSTREAM_SPEED": "",
124 "PROVIDER_PINGCHECK_SERVERLIST_REF": "-2",
125}
126#: UI defaults for a port forwarding instance
127port_forwarding_defaults = {
128 "PORT_FORWARDING_DST_IP_REF": "1",
129 "PORT_FORWARDING_DST_PORT": "",
130 "PORT_FORWARDING_DST_PORT_END": "",
131 "PORT_FORWARDING_PROTOCOL_TYPE": "TCP",
132 "PORT_FORWARDING_SRC_PORT": "",
133 "PORT_FORWARDING_SRC_PORT_END": "",
134}
135#: UI defaults for a firewall ruleset instance
136firewall_ruleset_defaults = {
137 "FIREWALL_RULESET_PROFILE_TYPE": "FULL",
138}
139#: UI defaults for a proxy accesslist instance
140proxy_accesslist_defaults = {
141 "PROXY_ACCESSLIST_ENTRY_COUNT": "123",
142 "PROXY_ACCESSLIST_MODE": "1",
143 "PROXY_ACCESSLIST_SIZETYPE": "1",
144 "PROXY_ACCESSLIST_TYPE": "0",
145}
146#: UI defaults for a key instance
147key_own_defaults = {
be8c26c1
PD
148 "KEY_OWN_FINGERPRINT_MD5": "",
149 "KEY_OWN_FINGERPRINT_SHA1": "",
150 "KEY_OWN_ID_X509": "CN=net.lan",
151 "KEY_OWN_ISSUER": "CN=, C=, L=, ST=, O=, OU=",
13abc7d0
PD
152 "KEY_OWN_KEYSIZE": "2048",
153 "KEY_OWN_HASH_ALGO": "SHA2_256",
be8c26c1
PD
154 # TODO: the key own creation is currently too hacky for better sanitized defaults
155 "KEY_OWN_PRIVATE_KEY": "<CREATE_HACK>",
156 # TODO: the key own creation is currently too hacky for better sanitized defaults
157 "KEY_OWN_PUBLIC_KEY": "<CREATE_HACK>",
158 # TODO: the key own creation is currently too hacky for better sanitized defaults
159 "KEY_OWN_REQUEST": "<CREATE_HACK>",
160 "KEY_OWN_SUBJECT": "CN=net.lan",
161 # TODO: the key own creation is currently too hacky for better sanitized defaults
162 "KEY_OWN_VALIDFROM": "00001122T445566",
163 # TODO: the key own creation is currently too hacky for better sanitized defaults
164 "KEY_OWN_VALIDTILL": "99991122T445566",
13abc7d0
PD
165 "KEY_OWN_TYPE": "SELF",
166 # the ones bellow should be set when using 'generate' to create the key
be8c26c1
PD
167 "KEY_OWN_CREATE_CN": "",
168 "KEY_OWN_CREATE_EMAIL": ""
13abc7d0
PD
169}
170#: UI defaults for a VPN connection instance
171vpnconn_defaults = {
172 "VPNCONN_ACTIVATION": "ALWAYS",
173 "VPNCONN_DISABLED": "0",
174 "VPNCONN_DNS_RELAYING_ALLOWED": "1",
175 "VPNCONN_EMAIL_RELAYING_ALLOWED": "1",
176 "VPNCONN_ENCRYPTION_PROFILE_REF": "0",
177 "VPNCONN_FIREWALL_RULESET_REF": "5",
178 "VPNCONN_IKE_VERSION": "1",
179 "VPNCONN_KEY_FOREIGN_REF": "1",
180 "VPNCONN_KEY_OWN_REF": "1",
181 "VPNCONN_KEY_TYPE": "PUBLIC",
182 "VPNCONN_LAN_NAT_IP": "",
183 "VPNCONN_LAN_NAT_MODE": "UNMODIFIED",
184 "VPNCONN_LAN_NAT_NETWORK": "",
185 "VPNCONN_LAN_NIC_REF": "2",
be8c26c1 186 "VPNCONN_LAN_NET": "",
13abc7d0
PD
187 "VPNCONN_LAN_NETMASK": "255.255.0.0",
188 "VPNCONN_LAN_TYPE": "NIC",
189 "VPNCONN_LIFETIME_IKE": "480",
190 "VPNCONN_LIFETIME_IPSECSA": "60",
191 "VPNCONN_OFFLINE_DETECTION_SEC": "60",
192 "VPNCONN_PEER_DNS": "",
df3ad4c0 193 "VPNCONN_PEER_IP": "",
13abc7d0
PD
194 "VPNCONN_PEER_TYPE": "IP",
195 "VPNCONN_PROXY_PROFILE_REF": "-2",
196 "VPNCONN_PSK": "",
197 "VPNCONN_PSK_FOREIGN_ID": "",
198 "VPNCONN_PSK_FOREIGN_ID_TYPE": "IP",
199 "VPNCONN_PSK_OWN_ID": "",
200 "VPNCONN_PSK_OWN_ID_TYPE": "IP",
201 "VPNCONN_REMOTE_INET_NAT": "1",
be8c26c1 202 "VPNCONN_REMOTE_MODECONFIG_IP": "",
13abc7d0
PD
203 "VPNCONN_REMOTE_NAT_ENABLE": "0",
204 "VPNCONN_REMOTE_NAT_NETWORK": "",
be8c26c1 205 "VPNCONN_REMOTE_NET": "",
13abc7d0
PD
206 "VPNCONN_REMOTE_NETMASK": "255.255.0.0",
207 "VPNCONN_REMOTE_TYPE": "CUSTOM",
208 "VPNCONN_RETRIES": "3",
209 "VPNCONN_SECURED": "ESP",
210 "VPNCONN_XAUTH_SERVER_ENABLE": "0"
211}
188605ae
PD
212
213
214###############################################################################
f49f6323
PD
215# MINOR CONFIGURATION
216###############################################################################
217
7628bc48 218
188605ae 219def template(name, value, instance=-1, defaults=None, **kwargs):
f49f6323 220 """
188605ae 221 Generate a template cnf variable from provided defaults.
f49f6323 222
188605ae
PD
223 :param str name: cnf variable name
224 :param str value: cnf variable data value
b5abc93a 225 :param int instance: cnf variable instance number
188605ae
PD
226 :param defaults: default child variables to populate the cnf variable with
227 :type defaults: {str, str or {}} or None
b5abc93a
PD
228 :returns: generated cnf variable
229 :rtype: :py:class:`Cnf`
188605ae
PD
230
231 All additional keyword arguments will be used to overwrite the defaults.
f49f6323 232 """
188605ae
PD
233 log.info(f"Generating a template {name} cnfvar")
234 cnf = Cnf(name, value=value, instance=instance)
235 defaults = {} if defaults is None else defaults
236 cnf.add_children(*[(key, value) for key, value in defaults.items()])
237 for key in kwargs.keys():
238 cnf.children.single_with_name(f"{name}_{key}").value = kwargs[key]
239 return cnf
f49f6323
PD
240
241
188605ae 242def user(name, password, instance=-1, **kwargs):
f49f6323 243 """
188605ae 244 Generate a user cnf variable.
f49f6323 245
188605ae 246 :param str name: username for the user
b5abc93a
PD
247 :param str password: password for the user
248 :param int instance: instance number for the user
249 :returns: generated cnf variable
250 :rtype: :py:class:`Cnf`
f49f6323 251 """
188605ae
PD
252 log.info(f"Generating a user {name} cnfvar")
253 user_cnf = template("user", name, instance=instance, defaults=user_defaults, **kwargs)
254 user_cnf.children.single_with_name("user_fullname").value = name.capitalize()
255 user_cnf.children.single_with_name("user_password").value = password
f49f6323
PD
256 return user_cnf
257
258
b358e4cb 259def group(name, instance=-1, **kwargs):
f49f6323 260 """
b358e4cb 261 Generate a group cnf variable.
f49f6323 262
b358e4cb
PD
263 :param str name: name for the group
264 :param int instance: instance number for the group
265 :returns: generated cnf variable
266 :rtype: :py:class:`Cnf`
f49f6323 267 """
b358e4cb
PD
268 log.info(f"Generating a group {name} cnfvar")
269 group_cnf = template("group", name, instance=instance, defaults=group_defaults, **kwargs)
f49f6323
PD
270 return group_cnf
271
272
13abc7d0 273def nic(instance=-1, **kwargs):
f49f6323 274 """
13abc7d0
PD
275 Generate a nic cnf variable.
276
277 :param int instance: instance number for the nic
278 :returns: generated cnf variable
279 :rtype: :py:class:`Cnf`
f49f6323 280 """
13abc7d0
PD
281 log.info(f"Generating a nic cnfvar")
282 nic_cnf = template("nic", "", instance=instance, defaults=nic_defaults, **kwargs)
f49f6323
PD
283 return nic_cnf
284
285
13abc7d0 286def intraclient(name, instance=-1, **kwargs):
f49f6323 287 """
13abc7d0
PD
288 Generate an intraclient cnf variable.
289
290 :param str name: name for the intraclient
291 :param int instance: instance number for the intraclient
292 :returns: generated cnf variable
293 :rtype: :py:class:`Cnf`
f49f6323 294 """
13abc7d0
PD
295 log.info(f"Generating an intraclient {name} cnfvar")
296 intraclient_cnf = template("intraclient", name, instance=instance,
297 defaults=intraclient_defaults, **kwargs)
f49f6323
PD
298 return intraclient_cnf
299
300
13abc7d0 301def provider(name, ip, dns, instance=-1, **kwargs):
f49f6323 302 """
13abc7d0 303 Generate a provider cnf variable.
f49f6323 304
13abc7d0 305 :param str name: name for the provider
f49f6323 306 :param str ip: IP address of the provider
f49f6323 307 :param str dns: IP address of the DNS server
13abc7d0
PD
308 :param int instance: instance number for the provider
309 :returns: generated cnf variable
310 :rtype: :py:class:`Cnf`
f49f6323 311 """
13abc7d0
PD
312 log.info(f"Generating a provider {name} cnfvar")
313 provider_cnf = template("provider", name, instance=instance,
314 defaults=provider_defaults, **kwargs)
315 provider_cnf.children.single_with_name("provider_ip").value = ip
316 provider_cnf.children.single_with_name("provider_dns").value = dns
f49f6323
PD
317 return provider_cnf
318
319
13abc7d0 320def port_forwarding(name, src_port="1234", dst_port="1234", instance=-1, **kwargs):
f49f6323 321 """
13abc7d0
PD
322 Generate a port forwarding cnf variable.
323
324 :param str name: name for the port forwarding mapping
325 :param str src_port: forwarded source port
326 :param str dst_port: forwarded destination port
327 :param int instance: instance number for the port forwarding mapping
328 :returns: generated cnf variable
329 :rtype: :py:class:`Cnf`
f49f6323 330 """
13abc7d0
PD
331 log.info(f"Generating a port forwarding {name} cnfvar")
332 port_forwarding_cnf = template("port_forwarding", name, instance=instance,
333 defaults=port_forwarding_defaults, **kwargs)
334 port_forwarding_cnf.children.single_with_name("port_forwarding_src_port").value = src_port
335 port_forwarding_cnf.children.single_with_name("port_forwarding_dst_port").value = dst_port
336 return port_forwarding_cnf
f49f6323
PD
337
338
13abc7d0 339def firewall_ruleset(name, instance=-1, **kwargs):
f49f6323 340 """
13abc7d0 341 Generate a firewall ruleset cnf variable.
f49f6323 342
13abc7d0
PD
343 :param str name: name for the firewall ruleset
344 :param int instance: instance number for the firewall ruleset
345 :returns: generated cnf variable
346 :rtype: :py:class:`Cnf`
f49f6323 347 """
13abc7d0
PD
348 log.info(f"Generating a firewall ruleset {name} cnfvar")
349 firewall_ruleset_cnf = template("firewall_ruleset", name, instance=instance,
350 defaults=firewall_ruleset_defaults, **kwargs)
351 return firewall_ruleset_cnf
352
353
354def proxy_accesslist(name, instance=-1, **kwargs):
f49f6323 355 """
13abc7d0 356 Generate a proxy accesslist cnf variable.
f49f6323 357
13abc7d0
PD
358 :param str name: name for the proxy accesslist
359 :param int instance: instance number for the proxy accesslist
360 :returns: generated cnf variable
361 :rtype: :py:class:`Cnf`
f49f6323 362 """
13abc7d0
PD
363 log.info(f"Generating a proxy accesslist {name} cnfvar")
364 proxy_accesslist_cnf = template("proxy_accesslist", name, instance=instance,
365 defaults=proxy_accesslist_defaults, **kwargs)
366 return proxy_accesslist_cnf
367
368
369def key_own(name, instance=-1, **kwargs):
f49f6323 370 """
13abc7d0 371 Generate an own key cnf variable.
f49f6323 372
13abc7d0
PD
373 :param str name: name for the own key
374 :param int instance: instance number for the own key
375 :returns: generated cnf variable
376 :rtype: :py:class:`Cnf`
f49f6323 377 """
13abc7d0
PD
378 log.info(f"Generating an own key {name} cnfvar")
379 key_own_cnf = template("key_own", name, instance=instance,
380 defaults=key_own_defaults, **kwargs)
381 return key_own_cnf
382
383
384def vpnconn(name, instance=-1, **kwargs):
f49f6323 385 """
13abc7d0 386 Generate a vpn connection cnf variable.
f49f6323 387
13abc7d0
PD
388 :param str name: name for the vpn connection
389 :param int instance: instance number for the vpn connection
390 :returns: generated cnf variable
391 :rtype: :py:class:`Cnf`
f49f6323 392 """
13abc7d0
PD
393 log.info(f"Generating a vpn connection {name} cnfvar")
394 vpnconn_cnf = template("vpnconn", name, instance=instance,
395 defaults=vpnconn_defaults, **kwargs)
396 return vpnconn_cnf