summaryrefslogtreecommitdiffstats
path: root/applications/luci-app-radicale3/htdocs/luci-static/resources/view/radicale3.js
blob: 54fe96a0e85efc7b62072dee2b98d98adb4b3511 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
'use strict';
'require dom';
'require form';
'require view';
'require rpc';
'require uci';
'require ui';

// cspell:words Radicale addremove authtype cfgsections cfgvalue formvalue
// cspell:words hostport htpasswd ipaddrport noopener noreferrer nsections
// cspell:words origcfgsections plainpass ppconfirm rmempty sslon taboption
// cspell:words uinteger multifilesystem hidetitle packagelist libpass
// cspell:words passlib cffi

const callServiceList = rpc.declare({
	object: 'service',
	method: 'list',
	params: ['name'],
	expect: {
		radicale3: {},
	},
});

const callEncrypt = rpc.declare({
	object: 'rad3-enc',
	method: 'encrypt',
	params: ['type', 'plainpass'],
	expect: {},
});

const callPackageList = rpc.declare({
	object: 'rpc-sys',
	method: 'packagelist',
	params: ['all'],
	expect: {},
});

return view.extend({
	load() {
		return Promise.all([
			L.resolveDefault(callServiceList('radicale3')),
			L.resolveDefault(callPackageList(true)),
			uci.load('radicale3'),
		]);
	},
	render([service_list_r3, package_list_r3]) {
		let radicale_address, radicale_host, radicale_port, running, button;
		let has_ssl, package_list, python_ssl_version;

		radicale_host = uci.get('radicale3', 'server', 'host') || ['127.0.0.1:5232'];
		radicale_host = String(radicale_host?.[0]);
		radicale_address = radicale_host.substring(0, radicale_host.lastIndexOf(':')) || '127.0.0.1';

		button = '';

		if ((radicale_address == '127.0.0.1') || (radicale_address == '[::1]') || radicale_host == 'localhost') {
			ui.addNotification(_('Need a listen address'),
				_('Radicale needs a non-loopback IP address for your browser to access the web interface'));
		} else {
			if (radicale_address == '0.0.0.0' || radicale_address == '[::]') {
				radicale_address = window.location.hostname;
			}
			radicale_port = radicale_host.substring(radicale_host.lastIndexOf(':') + 1) || '5232';

			running = Object.keys(service_list_r3.instances || {}).length > 0;

			if (running)
				button = '&#160;&#160;<a class="btn" href="' + window.location.protocol + '//' + radicale_address + ':' + radicale_port + '" target="_blank" rel="noreferrer noopener">' + _('Open Web Interface') + '</a>';
		}

		package_list = package_list_r3.packages;
		python_ssl_version = package_list['python3-openssl'];
		has_ssl = (python_ssl_version != null) && (python_ssl_version != '');

		let m, s, o, at, hte, plainpass, ppconfirm, pass;

		m = new form.Map('radicale3', 'Radicale3', _('Radicale v3 CalDav/CardDAV Server Configuration') + button);

		s = m.section(form.NamedSection, 'server', 'section', _('Server Settings'));
		s.addremove = true;
		s.anonymous = false;

		s.tab('main', _('Main'));

		o = s.taboption('main', form.DynamicList, 'host', _('Host:port'));
		o.optional = true;
		o.datatype = 'or(hostport(0),ipaddrport(1))';
		o.default = ['127.0.0.1:5232', '[::1]:5232'];

		s.tab('advanced', _('Advanced'));

		if (has_ssl) {
			let sslon, cert_file, key_file, ca_file;

			sslon = s.taboption('main', form.Flag, 'ssl', _('SSL'), _('Enable SSL connections'));
			sslon.rmempty = true;
			sslon.default = sslon.disabled;

			cert_file = s.taboption('main', form.FileUpload, 'certificate', _('Certificate'));
			cert_file.rmempty = true;
			cert_file.optional = false;
			cert_file.depends('ssl', sslon.enabled);

			key_file = s.taboption('main', form.FileUpload, 'key', _('Private Key'));
			key_file.rmempty = true;
			key_file.optional = false;
			key_file.depends('ssl', sslon.enabled);

			sslon.cfgvalue = function (section_id) {
				return ((cert_file.cfgvalue(section_id) != null) && (key_file.cfgvalue(section_id) != null));
			};

			ca_file = s.taboption('advanced', form.FileUpload, 'certificate_authority', _('Client Certificate Authority'), _('For verifying client certificates'));
			ca_file.rmempty = true;
			ca_file.depends('ssl', sslon.enabled);

			o = s.taboption('advanced', form.Value, 'ciphers', _('Allowed Ciphers'), _('See python3-openssl documentation for available ciphers'));
			o.rmempty = true;
			o.depends('ssl', sslon.enabled);

			o = s.taboption('advanced', form.Value, 'protocol', _('Use Protocol'), _('See python3-openssl documentation for available protocols'));
			o.rmempty = true;
			o.depends('ssl', sslon.enabled);
			o.placeholder = 'PROTOCOL_TLSv1_2';
		} else {
			o = s.taboption('main', form.DummyValue, '_no_ssl', _('No SSL'), _('No SSL support available. Please install python3-openssl.'));
			o.write = function () {};
			o.cfgvalue = function () {
				return '';
			};
		}

		o = s.taboption('advanced', form.Value, 'max_connection', _('Max Connections'), _('Maximum number of simultaneous connections'));
		o.rmempty = true;
		o.placeholder = 20;
		o.datatype = 'uinteger';

		o = s.taboption('advanced', form.Value, 'max_content_length', _('Max Content Length'), _('Maximum size of request body (bytes)'));
		o.rmempty = true;
		o.datatype = 'uinteger';
		o.placeholder = 100000000;

		o = s.taboption('advanced', form.Value, 'timeout', _('Timeout'), _('Socket timeout (seconds)'));
		o.rmempty = true;
		o.placeholder = 30;
		o.datatype = 'uinteger';

		s = m.section(form.NamedSection, 'auth', 'section', _('Authentication'));
		s.addremove = true;

		let python_bcrypt_version, has_bcrypt, python_argon2_version, has_argon2;
		let python_libpass_version, has_libpass;

		python_libpass_version = package_list['python3-libpass'];
		has_libpass = (python_libpass_version != null) && (python_libpass_version != '');
		python_bcrypt_version = package_list['python3-bcrypt'];
		// Until we update radicale to the latest release that uses libpass instead
		// of passlib, bcrypt is only usable before version 5.0.0
		has_bcrypt = (python_bcrypt_version != null) && (python_bcrypt_version != ''
			&& !((!has_libpass) && (python_bcrypt_version.startsWith('5'))));
		python_argon2_version = package_list['python3-argon2-cffi'];
		has_argon2 = (python_argon2_version != null) && (python_argon2_version != '');

		s.tab('main', _('Main'));

		hte = s.taboption('main', form.ListValue, 'htpasswd_encryption', _('Encryption'), _('Password encryption method'));
		hte.depends({ type: 'htpasswd' });
		hte.depends({ type: '' });
		hte.value('plain', _('Plaintext'));
		hte.value('md5', _('MD5-APR1'));
		hte.value('sha256', _('SHA-256'));
		hte.value('sha512', _('SHA-512'));
		if (has_bcrypt) {
			hte.value('bcrypt', _('BCRYPT'));
		}
		if (has_argon2) {
			hte.value('argon2', _('ARGON2'));
		}
		hte.value('autodetect', _('autodetect'), _('password file can have users with a mix of support encryption methods'));
		hte.default = 'autodetect';
		hte.rmempty = true;

		s.tab('advanced', _('Advanced'));

		at = s.taboption('advanced', form.ListValue, 'type', _('Authentication Type'));
		at.value('', _('Default (htpasswd file from users below)'));
		at.value('htpasswd', _('htpasswd file (manually populated)'));
		at.value('none', _('No authentication'));
		at.value('remote_user', _('REMOTE_USER from web server'));
		at.value('http_x_remote_user', _('X-Remote-User from web server'));
		at.default = '';
		at.rmempty = true;

		o = s.taboption('advanced', form.Value, 'htpasswd_filename', _('Filename'), _('htpasswd-formatted file filename'));
		o.depends('type', 'htpasswd');
		o.rmempty = true;
		o.placeholder = '/etc/radicale3/users';
		o.default = '';

		o = s.taboption('advanced', form.Value, 'realm', _('Realm'), _('HTTP(S) Basic Authentication Realm'));
		o.rmempty = true;
		o.placeholder = 'Radicale - Password Required';

		o = s.taboption('advanced', form.Value, 'delay', _('Retry Delay'), _('Required time between a failed authentication attempt and trying again'));
		o.rmempty = true;
		o.default = 1;
		o.datatype = 'uinteger';
		o.depends({ type: '' });
		o.depends({ type: 'htpasswd' });
		o.depends({ type: 'remote_user' });
		o.depends({ type: 'http_x_remote_user' });

		// User settings
		s = m.section(form.TypedSection, 'user', _('Radicale3 Users'));
		s.addremove = true;
		s.anonymous = true;

		at = s.option(form.ListValue, '_auth_type', _('Encryption method when changing this password'));
		at.value('plain', _('Plaintext'));
		at.value('md5', _('MD5-APR1'));
		at.value('sha256', _('SHA-256'));
		at.value('sha512', _('SHA-512'));
		if (has_bcrypt) {
			at.value('bcrypt', _('BCRYPT'));
		}
		if (has_argon2) {
			at.value('argon2', _('ARGON2'));
		}
		at.default = 'sha512';
		at.depends({ 'radicale3.auth.type': 'htpasswd', 'radicale3.auth.htpasswd_encryption': 'autodetect' });
		at.depends({ 'radicale3.auth.type': '', 'radicale3.auth.htpasswd_encryption': 'autodetect' });

		at.cfgvalue = function () {
			return hte.cfgvalue('auth');
		};

		at.write = function () {};

		o = s.option(form.Value, 'name', _('Username'));
		o.optional = false;
		o.depends({ 'radicale3.auth.type': 'htpasswd' });
		o.depends({ 'radicale3.auth.type': '' });

		o.validate = function (section_id, value) {
			if (!value) {
				return _('Username is required');
			}

			if (value.length < 1) {
				return _('Username must be at least 1 character long');
			}

			if (!/^[a-zA-Z0-9._-]+$/.test(value)) {
				return _('Username can only contain letters, numbers, dots, underscores and hyphens');
			}

			// Check for duplicate usernames
			const sections = this.section.cfgsections();
			for (const section of sections) {
				if (section !== section_id) {
					const existingName = this.cfgvalue(section);
					if (existingName === value) {
						return _('Username already exists');
					}
				}
			}

			return true;
		};

		plainpass = s.option(form.Value, 'plain_pass', _('Plaintext Password'));
		plainpass.placeholder = 'Example password';
		plainpass.password = true;
		plainpass.depends({ 'radicale3.auth.type': 'htpasswd' });
		plainpass.depends({ 'radicale3.auth.type': '' });

		ppconfirm = s.option(form.Value, 'plain_pass_confirm', _('Confirm Plaintext Password'));
		ppconfirm.placeholder = 'Example password';
		ppconfirm.password = true;
		ppconfirm.depends({ 'radicale3.auth.type': 'htpasswd' });
		ppconfirm.depends({ 'radicale3.auth.type': '' });

		plainpass.write = function () {};
		ppconfirm.write = function () {};

		plainpass.validate = function (section, value) {
			if (value != ppconfirm.formvalue(section)) {
				return _('\'Plaintext Password\' and \'Confirm Plaintext Password\' do not match');
			} else if ((plainpass.formvalue(section) == '') && (hte.formvalue('auth') != 'autodetect') && (hte.formvalue('auth') != hte.cfgvalue('auth'))) {
				return _('When changing encryption method, you must change all passwords');
			} else {
				return true;
			}
		};

		ppconfirm.validate = function (section, value) {
			if (value != plainpass.formvalue(section)) {
				return _('\'Plaintext Password\' and \'Confirm Plaintext Password\' do not match');
			} else if ((ppconfirm.formvalue(section) == '') && (hte.formvalue('auth') != 'autodetect') && (hte.formvalue('auth') != hte.cfgvalue('auth'))) {
				return _('When changing encryption method, you must change all passwords');
			} else {
				return true;
			}
		};

		pass = s.option(form.Value, 'password', _('Encrypted Password'), _('If \'Plaintext Password\' is filled and matches \'Confirm Plaintext Password\' then this field becomes of hash of that password, otherwise this field remains the existing hash (you can also put your own hash value for the type of hash listed above).'));
		pass.password = true;
		pass.rmempty = false;
		pass.depends({ 'radicale3.auth.type': 'htpasswd' });
		pass.depends({ 'radicale3.auth.type': '' });
		pass.optional = true;

		pass.parse = (section_id) => {
			let active, plainValue;

			active = pass.isActive(section_id);
			plainValue = '';

			if (active && (plainpass.formvalue(section_id) === ppconfirm.formvalue(section_id))) {
				plainValue = plainpass.formvalue(section_id);
			} else {
				active = false;
			}

			if (active) {
				if (plainValue != '') {
					let variant;

					const authtype = hte.formvalue('auth') || 'plain';
					variant = at.formvalue(section_id) || 'plain';

					if (authtype != 'autodetect') {
						variant = authtype;
					}

					return callEncrypt(variant, plainValue).then((encryption_result) => {
						if (!encryption_result.encrypted_password) {
							m.reset();
							return Promise.reject(new TypeError(`${_('Unable to encrypt plaintext password')} ${encryption_result.error}`));
						} else {
							return pass.write(section_id, encryption_result.encrypted_password);
						}
					});
				} else {
					if (!pass.formvalue(section_id) && (plainpass.formvalue(section_id) === ppconfirm.formvalue(section_id)) && !plainpass.formvalue(section_id)) {
						return Promise.reject(new TypeError(
							_('Password is required')));
					} else {
						return Promise.resolve(pass.write(section_id, pass.formvalue(section_id)));
					}
				}
			} else if (!this.retain) {
				return Promise.resolve(pass.remove(section_id));
			}

			return Promise.resolve();
		};

		pass.validate = function (section_id, value) {
			const active = pass.isActive(section_id);

			if (active && !value && (plainpass.formvalue(section_id) === ppconfirm.formvalue(section_id)) && !plainpass.formvalue(section_id)) {
				return _('Password is required');
			}
			return true;
		};

		s = m.section(form.NamedSection, 'storage', 'section', _('Storage'));
		s.addremove = true;

		s.tab('main', _('Main'));

		o = s.taboption('main', form.Value, 'filesystem_folder', _('Folder'), _('Folder in which to store collections'));
		o.rmempty = true;
		o.placeholder = '/var/radicale3/data';

		s.tab('advanced', _('Advanced'));

		o = s.taboption('advanced', form.ListValue, 'type', _('Storage Type'));
		o.value('', _('Default (Multiple files on filesystem)'));
		o.value('multifilesystem', _('Multiple files on filesystem'));
		o.value('multifilesystem_nolock', _('Multiple files on filesystem wit no file-based locking. Must only be used with a single process.'));
		o.default = '';
		o.rmempty = true;

		o = s.taboption('advanced', form.Value, 'max_sync_token_age', _('Max Sync Token Age'), _('Delete sync tokens that are older (seconds)'));
		o.rmempty = true;
		o.placeholder = 2592000;
		o.datatype = 'uinteger';

		o = s.taboption('advanced', form.Value, 'hook', _('Hook'), _('Command that is run after changes to storage'));
		o.rmempty = true;

		o = s.taboption('advanced', form.DummyValue, '_example_storage_hook', _('Example storage hook'));
		o.depends('hook', '');
		o.cfgvalue = function () {
			return _('([ -d .git ] || git init) && git add -A && (git diff --cached --quiet || git commit -m \'Changes by \'%(user)s');
		};
		o.write = function () {};

		// TODO: Allow configuration of rights file from this page

		s = m.section(form.NamedSection, 'rights', 'section', _('Rights'), _('User-based ACL Settings'));
		s.addremove = true;

		o = s.option(form.ListValue, 'type', _('Rights Type'));
		o.value('', _('Default (owner only)'));
		o.value('owner_only', _('RO: None, RW: Owner'));
		o.value('authenticated', _('RO: None, RW: Authenticated Users'));
		o.value('owner_write', _('RO: Authenticated Users, RW: Owner'));
		o.value('from_file', _('Based on settings in \'Rights File\''));
		o.value('none', _('RO: All, RW: All'));
		o.default = '';
		o.rmempty = true;

		o = s.option(form.FileUpload, 'file', _('Rights File'));
		o.rmempty = true;
		o.depends('type', 'from_file');

		s = m.section(form.NamedSection, 'web', 'section', _('Web UI'));
		s.addremove = true;

		o = s.option(form.ListValue, 'type', _('Web UI Type'));
		o.value('', 'Default (Built-in)');
		o.value('internal', 'Built-in');
		o.value('none', 'None');
		o.default = '';
		o.rmempty = true;

		s = m.section(form.NamedSection, 'headers', 'section', _('Headers'), _('HTTP(S) Headers'));
		s.addremove = true;

		o = s.option(form.Value, 'cors', _('CORS'), _('Header: X-Access-Control-Allow-Origin'));
		o.rmempty = true;
		o.placeholder = '*';

		s = m.section(form.NamedSection, 'encoding', 'section', _('Document Encoding'));
		s.addremove = true;

		o = s.option(form.Value, 'request', _('Request'), _('Encoding for responding to requests/events'));
		o.rmempty = true;
		o.placeholder = 'utf-8';

		o = s.option(form.Value, 'stock', _('Storage'), _('Encoding for storing local collections'));
		o.rmempty = true;
		o.placeholder = 'utf-8';

		s = m.section(form.NamedSection, 'logging', 'section', _('Logging'));
		s.addremove = true;

		s.tab('main', _('Main'));

		o = s.taboption('main', form.ListValue, 'level', _('Log Level'));
		s.rmempty = true;
		o.value('', _('Default (info)'));
		o.value('trace', _('Trace'));
		o.value('debug', _('Debug'));
		o.value('info', _('Info'));
		o.value('notice', _('Notice'));
		o.value('warning', _('Warning'));
		o.value('error', _('Error'));
		o.value('critical', _('Critical'));
		o.value('alert', _('Alert'));
		o.default = '';

		// TODO: Add more logging options

		s.tab('advanced', _('Advanced'));

		o = s.taboption('advanced', form.Flag, 'mask_passwords', _('Mask Passwords'), _('Redact passwords in logs'));
		o.rmempty = true;
		o.default = o.enabled;

		return m.render();
	},
});