luci-mod-network: fix invalid markup in the relay tab
[project/luci.git] / modules / luci-mod-network / htdocs / luci-static / resources / view / network / dhcp.js
1 'use strict';
2 'require view';
3 'require dom';
4 'require poll';
5 'require rpc';
6 'require uci';
7 'require form';
8 'require network';
9 'require validation';
10 'require tools.widgets as widgets';
11
12 var callHostHints, callDUIDHints, callDHCPLeases, CBILeaseStatus, CBILease6Status;
13
14 callHostHints = rpc.declare({
15 object: 'luci-rpc',
16 method: 'getHostHints',
17 expect: { '': {} }
18 });
19
20 callDUIDHints = rpc.declare({
21 object: 'luci-rpc',
22 method: 'getDUIDHints',
23 expect: { '': {} }
24 });
25
26 callDHCPLeases = rpc.declare({
27 object: 'luci-rpc',
28 method: 'getDHCPLeases',
29 expect: { '': {} }
30 });
31
32 CBILeaseStatus = form.DummyValue.extend({
33 renderWidget: function(section_id, option_id, cfgvalue) {
34 return E([
35 E('h4', _('Active DHCP Leases')),
36 E('table', { 'id': 'lease_status_table', 'class': 'table' }, [
37 E('tr', { 'class': 'tr table-titles' }, [
38 E('th', { 'class': 'th' }, _('Hostname')),
39 E('th', { 'class': 'th' }, _('IPv4 address')),
40 E('th', { 'class': 'th' }, _('MAC address')),
41 E('th', { 'class': 'th' }, _('Lease time remaining'))
42 ]),
43 E('tr', { 'class': 'tr placeholder' }, [
44 E('td', { 'class': 'td' }, E('em', _('Collecting data...')))
45 ])
46 ])
47 ]);
48 }
49 });
50
51 CBILease6Status = form.DummyValue.extend({
52 renderWidget: function(section_id, option_id, cfgvalue) {
53 return E([
54 E('h4', _('Active DHCPv6 Leases')),
55 E('table', { 'id': 'lease6_status_table', 'class': 'table' }, [
56 E('tr', { 'class': 'tr table-titles' }, [
57 E('th', { 'class': 'th' }, _('Host')),
58 E('th', { 'class': 'th' }, _('IPv6 address')),
59 E('th', { 'class': 'th' }, _('DUID')),
60 E('th', { 'class': 'th' }, _('Lease time remaining'))
61 ]),
62 E('tr', { 'class': 'tr placeholder' }, [
63 E('td', { 'class': 'td' }, E('em', _('Collecting data...')))
64 ])
65 ])
66 ]);
67 }
68 });
69
70 function calculateNetwork(addr, mask) {
71 addr = validation.parseIPv4(String(addr));
72
73 if (!isNaN(mask))
74 mask = validation.parseIPv4(network.prefixToMask(+mask));
75 else
76 mask = validation.parseIPv4(String(mask));
77
78 if (addr == null || mask == null)
79 return null;
80
81 return [
82 [
83 addr[0] & (mask[0] >>> 0 & 255),
84 addr[1] & (mask[1] >>> 0 & 255),
85 addr[2] & (mask[2] >>> 0 & 255),
86 addr[3] & (mask[3] >>> 0 & 255)
87 ].join('.'),
88 mask.join('.')
89 ];
90 }
91
92 function getDHCPPools() {
93 return uci.load('dhcp').then(function() {
94 let sections = uci.sections('dhcp', 'dhcp'),
95 tasks = [], pools = [];
96
97 for (var i = 0; i < sections.length; i++) {
98 if (sections[i].ignore == '1' || !sections[i].interface)
99 continue;
100
101 tasks.push(network.getNetwork(sections[i].interface).then(L.bind(function(section_id, net) {
102 var cidr = net ? (net.getIPAddrs()[0] || '').split('/') : null;
103
104 if (cidr && cidr.length == 2) {
105 var net_mask = calculateNetwork(cidr[0], cidr[1]);
106
107 pools.push({
108 section_id: section_id,
109 network: net_mask[0],
110 netmask: net_mask[1]
111 });
112 }
113 }, null, sections[i]['.name'])));
114 }
115
116 return Promise.all(tasks).then(function() {
117 return pools;
118 });
119 });
120 }
121
122 function validateHostname(sid, s) {
123 if (s == null || s == '')
124 return true;
125
126 if (s.length > 256)
127 return _('Expecting: %s').format(_('valid hostname'));
128
129 var labels = s.replace(/^\.+|\.$/g, '').split(/\./);
130
131 for (var i = 0; i < labels.length; i++)
132 if (!labels[i].match(/^[a-z0-9_](?:[a-z0-9-]{0,61}[a-z0-9])?$/i))
133 return _('Expecting: %s').format(_('valid hostname'));
134
135 return true;
136 }
137
138 function validateAddressList(sid, s) {
139 if (s == null || s == '')
140 return true;
141
142 var m = s.match(/^\/(.+)\/$/),
143 names = m ? m[1].split(/\//) : [ s ];
144
145 for (var i = 0; i < names.length; i++) {
146 var res = validateHostname(sid, names[i]);
147
148 if (res !== true)
149 return res;
150 }
151
152 return true;
153 }
154
155 function validateServerSpec(sid, s) {
156 if (s == null || s == '')
157 return true;
158
159 var m = s.match(/^(?:\/(.+)\/)?(.*)$/);
160 if (!m)
161 return _('Expecting: %s').format(_('valid hostname'));
162
163 var res = validateAddressList(sid, m[1]);
164 if (res !== true)
165 return res;
166
167 if (m[2] == '' || m[2] == '#')
168 return true;
169
170 // ipaddr%scopeid#srvport@source@interface#srcport
171
172 m = m[2].match(/^([0-9a-f:.]+)(?:%[^#@]+)?(?:#(\d+))?(?:@([0-9a-f:.]+)(?:@[^#]+)?(?:#(\d+))?)?$/);
173
174 if (!m)
175 return _('Expecting: %s').format(_('valid IP address'));
176
177 if (validation.parseIPv4(m[1])) {
178 if (m[3] != null && !validation.parseIPv4(m[3]))
179 return _('Expecting: %s').format(_('valid IPv4 address'));
180 }
181 else if (validation.parseIPv6(m[1])) {
182 if (m[3] != null && !validation.parseIPv6(m[3]))
183 return _('Expecting: %s').format(_('valid IPv6 address'));
184 }
185 else {
186 return _('Expecting: %s').format(_('valid IP address'));
187 }
188
189 if ((m[2] != null && +m[2] > 65535) || (m[4] != null && +m[4] > 65535))
190 return _('Expecting: %s').format(_('valid port value'));
191
192 return true;
193 }
194
195 function validateMACAddr(pools, sid, s) {
196 if (s == null || s == '')
197 return true;
198
199 var leases = uci.sections('dhcp', 'host'),
200 this_macs = L.toArray(s).map(function(m) { return m.toUpperCase() });
201
202 for (var i = 0; i < pools.length; i++) {
203 var this_net_mask = calculateNetwork(this.section.formvalue(sid, 'ip'), pools[i].netmask);
204
205 if (!this_net_mask)
206 continue;
207
208 for (var j = 0; j < leases.length; j++) {
209 if (leases[j]['.name'] == sid || !leases[j].ip)
210 continue;
211
212 var lease_net_mask = calculateNetwork(leases[j].ip, pools[i].netmask);
213
214 if (!lease_net_mask || this_net_mask[0] != lease_net_mask[0])
215 continue;
216
217 var lease_macs = L.toArray(leases[j].mac).map(function(m) { return m.toUpperCase() });
218
219 for (var k = 0; k < lease_macs.length; k++)
220 for (var l = 0; l < this_macs.length; l++)
221 if (lease_macs[k] == this_macs[l])
222 return _('The MAC address %h is already used by another static lease in the same DHCP pool').format(this_macs[l]);
223 }
224 }
225
226 return true;
227 }
228
229 return view.extend({
230 load: function() {
231 return Promise.all([
232 callHostHints(),
233 callDUIDHints(),
234 getDHCPPools(),
235 network.getDevices()
236 ]);
237 },
238
239 render: function(hosts_duids_pools) {
240 var has_dhcpv6 = L.hasSystemFeature('dnsmasq', 'dhcpv6') || L.hasSystemFeature('odhcpd'),
241 hosts = hosts_duids_pools[0],
242 duids = hosts_duids_pools[1],
243 pools = hosts_duids_pools[2],
244 ndevs = hosts_duids_pools[3],
245 m, s, o, ss, so;
246
247 m = new form.Map('dhcp', _('DHCP and DNS'),
248 _('Dnsmasq is a lightweight <abbr title="Dynamic Host Configuration Protocol">DHCP</abbr> server and <abbr title="Domain Name System">DNS</abbr> forwarder.'));
249
250 s = m.section(form.TypedSection, 'dnsmasq');
251 s.anonymous = true;
252 s.addremove = false;
253
254 s.tab('general', _('General Settings'));
255 s.tab('relay', _('Relay'));
256 s.tab('files', _('Resolv and Hosts Files'));
257 s.tab('pxe_tftp', _('PXE/TFTP Settings'));
258 s.tab('advanced', _('Advanced Settings'));
259 s.tab('leases', _('Static Leases'));
260 s.tab('hosts', _('Hostnames'));
261 s.tab('srvhosts', _('SRV'));
262 s.tab('mxhosts', _('MX'));
263 s.tab('ipsets', _('IP Sets'));
264
265 s.taboption('general', form.Flag, 'domainneeded',
266 _('Domain required'),
267 _('Do not forward DNS queries without dots or domain parts.'));
268
269 s.taboption('general', form.Flag, 'authoritative',
270 _('Authoritative'),
271 _('This is the only DHCP server in the local network.'));
272
273 s.taboption('general', form.Value, 'local',
274 _('Local server'),
275 _('Never forward matching domains and subdomains, resolve from DHCP or hosts files only.'));
276
277 s.taboption('general', form.Value, 'domain',
278 _('Local domain'),
279 _('Local domain suffix appended to DHCP names and hosts file entries.'));
280
281 o = s.taboption('general', form.Flag, 'logqueries',
282 _('Log queries'),
283 _('Write received DNS queries to syslog.'));
284 o.optional = true;
285
286 o = s.taboption('general', form.DynamicList, 'server',
287 _('DNS forwardings'),
288 _('List of upstream resolvers to forward queries to.'));
289 o.optional = true;
290 o.placeholder = '/example.org/10.1.2.3';
291 o.validate = validateServerSpec;
292
293 o = s.taboption('general', form.DynamicList, 'address',
294 _('Addresses'),
295 _('Resolve specified FQDNs to an IP.') + '<br />' +
296 _('Syntax: <code>/fqdn[/fqdn…]/[ipaddr]</code>.') + '<br />' +
297 _('<code>/#/</code> matches any domain. <code>/example.com/</code> returns NXDOMAIN.') + '<br />' +
298 _('<code>/example.com/#</code> returns NULL addresses (<code>0.0.0.0</code> and <code>::</code>) for example.com and its subdomains.'));
299 o.optional = true;
300 o.placeholder = '/router.local/router.lan/192.168.0.1';
301
302 o = s.taboption('general', form.DynamicList, 'ipset',
303 _('IP sets'),
304 _('List of IP sets to populate with the specified domain IPs.'));
305 o.optional = true;
306 o.placeholder = '/example.org/ipset,ipset6';
307
308 o = s.taboption('general', form.Flag, 'rebind_protection',
309 _('Rebind protection'),
310 _('Discard upstream responses containing <a href="%s">RFC1918</a> addresses.').format('https://datatracker.ietf.org/doc/html/rfc1918'));
311 o.rmempty = false;
312
313 o = s.taboption('general', form.Flag, 'rebind_localhost',
314 _('Allow localhost'),
315 _('Exempt <code>127.0.0.0/8</code> and <code>::1</code> from rebinding checks, e.g. for RBL services.'));
316 o.depends('rebind_protection', '1');
317
318 o = s.taboption('general', form.DynamicList, 'rebind_domain',
319 _('Domain whitelist'),
320 _('List of domains to allow RFC1918 responses for.'));
321 o.depends('rebind_protection', '1');
322 o.optional = true;
323 o.placeholder = 'ihost.netflix.com';
324 o.validate = validateAddressList;
325
326 o = s.taboption('general', form.Flag, 'localservice',
327 _('Local service only'),
328 _('Accept DNS queries only from hosts whose address is on a local subnet.'));
329 o.optional = false;
330 o.rmempty = false;
331
332 o = s.taboption('general', form.Flag, 'nonwildcard',
333 _('Non-wildcard'),
334 _('Bind dynamically to interfaces rather than wildcard address.'));
335 o.default = o.enabled;
336 o.optional = false;
337 o.rmempty = true;
338
339 o = s.taboption('general', form.DynamicList, 'interface',
340 _('Listen interfaces'),
341 _('Listen only on the specified interfaces, and loopback if not excluded explicitly.'));
342 o.optional = true;
343 o.placeholder = 'lan';
344
345 o = s.taboption('general', form.DynamicList, 'notinterface',
346 _('Exclude interfaces'),
347 _('Do not listen on the specified interfaces.'));
348 o.optional = true;
349 o.placeholder = 'loopback';
350
351 o = s.taboption('relay', form.SectionValue, '__relays__', form.TableSection, 'relay', null,
352 _('Relay DHCP requests elsewhere. OK: v4↔v4, v6↔v6. Not OK: v4↔v6, v6↔v4.')
353 + '<br />' + _('Note: you may also need a DHCP Proxy (currently unavailable) when specifying a non-standard Relay To port(<code>addr#port</code>).')
354 + '<br />' + _('You may add multiple unique Relay To on the same Listen addr.'));
355
356 ss = o.subsection;
357
358 ss.addremove = true;
359 ss.anonymous = true;
360 ss.sortable = true;
361 ss.rowcolors = true;
362 ss.nodescriptions = true;
363
364 so = ss.option(form.Value, 'id', _('ID'));
365 so.rmempty = false;
366 so.optional = true;
367
368 so = ss.option(widgets.NetworkSelect, 'interface', _('Interface'));
369 so.optional = true;
370 so.rmempty = false;
371 so.placeholder = 'lan';
372
373 so = ss.option(form.Value, 'local_addr', _('Listen address'));
374 so.rmempty = false;
375 so.datatype = 'ipaddr';
376
377 for (var family = 4; family <= 6; family += 2) {
378 for (var i = 0; i < ndevs.length; i++) {
379 var addrs = (family == 6) ? ndevs[i].getIP6Addrs() : ndevs[i].getIPAddrs();
380 for (var j = 0; j < addrs.length; j++)
381 so.value(addrs[j].split('/')[0]);
382 }
383 }
384
385 so = ss.option(form.Value, 'server_addr', _('Relay To address'));
386 so.rmempty = false;
387 so.optional = false;
388 so.placeholder = '192.168.10.1#535';
389
390 so.validate = function(section, value) {
391 var m = this.section.formvalue(section, 'local_addr'),
392 n = this.section.formvalue(section, 'server_addr'),
393 p;
394 if (n != null && n != '')
395 p = n.split('#');
396 if (p.length > 1 && !/^[0-9]+$/.test(p[1]))
397 return _('Expected port number.');
398 else
399 n = p[0];
400
401 if ((m == null || m == '') && (n == null || n == ''))
402 return _('Both Listen addr and Relay To must be specified.');
403
404 if ((validation.parseIPv6(m) && validation.parseIPv6(n)) ||
405 validation.parseIPv4(m) && validation.parseIPv4(n))
406 return true;
407 else
408 return _('Listen and Relay To IP family must be homogeneous.')
409 };
410
411 s.taboption('files', form.Flag, 'readethers',
412 _('Use <code>/etc/ethers</code>'),
413 _('Read <code>/etc/ethers</code> to configure the DHCP server.'));
414
415 s.taboption('files', form.Value, 'leasefile',
416 _('Lease file'),
417 _('File to store DHCP lease information.'));
418
419 o = s.taboption('files', form.Flag, 'noresolv',
420 _('Ignore resolv file'));
421 o.optional = true;
422
423 o = s.taboption('files', form.Value, 'resolvfile',
424 _('Resolv file'),
425 _('File with upstream resolvers.'));
426 o.depends('noresolv', '0');
427 o.placeholder = '/tmp/resolv.conf.d/resolv.conf.auto';
428 o.optional = true;
429
430 o = s.taboption('files', form.Flag, 'nohosts',
431 _('Ignore <code>/etc/hosts</code>'));
432 o.optional = true;
433
434 o = s.taboption('files', form.DynamicList, 'addnhosts',
435 _('Additional hosts files'));
436 o.optional = true;
437 o.placeholder = '/etc/dnsmasq.hosts';
438
439 o = s.taboption('advanced', form.Flag, 'quietdhcp',
440 _('Suppress logging'),
441 _('Suppress logging of the routine operation for the DHCP protocol.'));
442 o.optional = true;
443
444 o = s.taboption('advanced', form.Flag, 'sequential_ip',
445 _('Allocate IPs sequentially'),
446 _('Allocate IP addresses sequentially, starting from the lowest available address.'));
447 o.optional = true;
448
449 o = s.taboption('advanced', form.Flag, 'boguspriv',
450 _('Filter private'),
451 _('Do not forward reverse lookups for local networks.'));
452 o.default = o.enabled;
453
454 s.taboption('advanced', form.Flag, 'filterwin2k',
455 _('Filter useless'),
456 _('Avoid uselessly triggering dial-on-demand links (filters SRV/SOA records and names with underscores).') + '<br />' +
457 _('May prevent VoIP or other services from working.'));
458
459 o = s.taboption('advanced', form.Flag, 'filter_aaaa',
460 _('Filter IPv6 AAAA records'),
461 _('Remove IPv6 addresses from the results and only return IPv4 addresses.') + '<br />' +
462 _('Can be useful if ISP has IPv6 nameservers but does not provide IPv6 routing.'));
463 o.optional = true;
464
465 o = s.taboption('advanced', form.Flag, 'filter_a',
466 _('Filter IPv4 A records'),
467 _('Remove IPv4 addresses from the results and only return IPv6 addresses.'));
468 o.optional = true;
469
470 s.taboption('advanced', form.Flag, 'localise_queries',
471 _('Localise queries'),
472 _('Return answers to DNS queries matching the subnet from which the query was received if multiple IPs are available.'));
473
474 if (L.hasSystemFeature('dnsmasq', 'dnssec')) {
475 o = s.taboption('advanced', form.Flag, 'dnssec',
476 _('DNSSEC'),
477 _('Validate DNS replies and cache DNSSEC data, requires upstream to support DNSSEC.'));
478 o.optional = true;
479
480 o = s.taboption('advanced', form.Flag, 'dnsseccheckunsigned',
481 _('DNSSEC check unsigned'),
482 _('Verify unsigned domain responses really come from unsigned domains.'));
483 o.default = o.enabled;
484 o.optional = true;
485 }
486
487 s.taboption('advanced', form.Flag, 'expandhosts',
488 _('Expand hosts'),
489 _('Add local domain suffix to names served from hosts files.'));
490
491 s.taboption('advanced', form.Flag, 'nonegcache',
492 _('No negative cache'),
493 _('Do not cache negative replies, e.g. for non-existent domains.'));
494
495 o = s.taboption('advanced', form.Value, 'serversfile',
496 _('Additional servers file'),
497 _('File listing upstream resolvers, optionally domain-specific, e.g. <code>server=1.2.3.4</code>, <code>server=/domain/1.2.3.4</code>.'));
498 o.placeholder = '/etc/dnsmasq.servers';
499
500 o = s.taboption('advanced', form.Flag, 'strictorder',
501 _('Strict order'),
502 _('Upstream resolvers will be queried in the order of the resolv file.'));
503 o.optional = true;
504
505 o = s.taboption('advanced', form.Flag, 'allservers',
506 _('All servers'),
507 _('Query all available upstream resolvers.'));
508 o.optional = true;
509
510 o = s.taboption('advanced', form.DynamicList, 'bogusnxdomain',
511 _('IPs to override with NXDOMAIN'),
512 _('List of IP addresses to convert into NXDOMAIN responses.'));
513 o.optional = true;
514 o.placeholder = '64.94.110.11';
515
516 o = s.taboption('advanced', form.Value, 'port',
517 _('DNS server port'),
518 _('Listening port for inbound DNS queries.'));
519 o.optional = true;
520 o.datatype = 'port';
521 o.placeholder = 53;
522
523 o = s.taboption('advanced', form.Value, 'queryport',
524 _('DNS query port'),
525 _('Fixed source port for outbound DNS queries.'));
526 o.optional = true;
527 o.datatype = 'port';
528 o.placeholder = _('any');
529
530 o = s.taboption('advanced', form.Value, 'dhcpleasemax',
531 _('Max. DHCP leases'),
532 _('Maximum allowed number of active DHCP leases.'));
533 o.optional = true;
534 o.datatype = 'uinteger';
535 o.placeholder = _('unlimited');
536
537 o = s.taboption('advanced', form.Value, 'ednspacket_max',
538 _('Max. EDNS0 packet size'),
539 _('Maximum allowed size of EDNS0 UDP packets.'));
540 o.optional = true;
541 o.datatype = 'uinteger';
542 o.placeholder = 1280;
543
544 o = s.taboption('advanced', form.Value, 'dnsforwardmax',
545 _('Max. concurrent queries'),
546 _('Maximum allowed number of concurrent DNS queries.'));
547 o.optional = true;
548 o.datatype = 'uinteger';
549 o.placeholder = 150;
550
551 o = s.taboption('advanced', form.Value, 'cachesize',
552 _('Size of DNS query cache'),
553 _('Number of cached DNS entries, 10000 is maximum, 0 is no caching.'));
554 o.optional = true;
555 o.datatype = 'range(0,10000)';
556 o.placeholder = 150;
557
558 o = s.taboption('pxe_tftp', form.Flag, 'enable_tftp',
559 _('Enable TFTP server'),
560 _('Enable the built-in single-instance TFTP server.'));
561 o.optional = true;
562
563 o = s.taboption('pxe_tftp', form.Value, 'tftp_root',
564 _('TFTP server root'),
565 _('Root directory for files served via TFTP. <em>Enable TFTP server</em> and <em>TFTP server root</em> turn on the TFTP server and serve files from <em>TFTP server root</em>.'));
566 o.depends('enable_tftp', '1');
567 o.optional = true;
568 o.placeholder = '/';
569
570 o = s.taboption('pxe_tftp', form.Value, 'dhcp_boot',
571 _('Network boot image'),
572 _('Filename of the boot image advertised to clients.'));
573 o.depends('enable_tftp', '1');
574 o.optional = true;
575 o.placeholder = 'pxelinux.0';
576
577 /* PXE - https://openwrt.org/docs/guide-user/base-system/dhcp#booting_options */
578 o = s.taboption('pxe_tftp', form.SectionValue, '__pxe__', form.GridSection, 'boot', null,
579 _('Special <abbr title="Preboot eXecution Environment">PXE</abbr> boot options for Dnsmasq.'));
580 ss = o.subsection;
581 ss.addremove = true;
582 ss.anonymous = true;
583 ss.nodescriptions = true;
584
585 so = ss.option(form.Value, 'filename',
586 _('Filename'),
587 _('Host requests this filename from the boot server.'));
588 so.optional = false;
589 so.placeholder = 'pxelinux.0';
590
591 so = ss.option(form.Value, 'servername',
592 _('Server name'),
593 _('The hostname of the boot server'));
594 so.optional = false;
595 so.placeholder = 'myNAS';
596
597 so = ss.option(form.Value, 'serveraddress',
598 _('Server address'),
599 _('The IP address of the boot server'));
600 so.optional = false;
601 so.placeholder = '192.168.1.2';
602
603 so = ss.option(form.DynamicList, 'dhcp_option',
604 _('DHCP Options'),
605 _('Options for the Network-ID. (Note: needs also Network-ID.) E.g. "<code>42,192.168.1.4</code>" for NTP server, "<code>3,192.168.4.4</code>" for default route. <code>0.0.0.0</code> means "the address of the system running dnsmasq".'));
606 so.optional = true;
607 so.placeholder = '42,192.168.1.4';
608
609 so = ss.option(widgets.DeviceSelect, 'networkid',
610 _('Network-ID'),
611 _('Apply DHCP Options to this net. (Empty = all clients).'));
612 so.optional = true;
613 so.noaliases = true;
614
615 so = ss.option(form.Flag, 'force',
616 _('Force'),
617 _('Always send DHCP Options. Sometimes needed, with e.g. PXELinux.'));
618 so.optional = true;
619
620 so = ss.option(form.Value, 'instance',
621 _('Instance'),
622 _('Dnsmasq instance to which this boot section is bound. If unspecified, the section is valid for all dnsmasq instances.'));
623 so.optional = true;
624
625 Object.values(L.uci.sections('dhcp', 'dnsmasq')).forEach(function(val, index) {
626 so.value(index, '%s (Domain: %s, Local: %s)'.format(index, val.domain || '?', val.local || '?'));
627 });
628
629 o = s.taboption('srvhosts', form.SectionValue, '__srvhosts__', form.TableSection, 'srvhost', null,
630 _('Bind service records to a domain name: specify the location of services. See <a href="%s">RFC2782</a>.').format('https://datatracker.ietf.org/doc/html/rfc2782')
631 + '<br />' + _('_service: _sip, _ldap, _imap, _stun, _xmpp-client, … . (Note: while _http is possible, no browsers support SRV records.)')
632 + '<br />' + _('_proto: _tcp, _udp, _sctp, _quic, … .')
633 + '<br />' + _('You may add multiple records for the same Target.')
634 + '<br />' + _('Larger weights (of the same prio) are given a proportionately higher probability of being selected.'));
635
636 ss = o.subsection;
637
638 ss.addremove = true;
639 ss.anonymous = true;
640 ss.sortable = true;
641 ss.rowcolors = true;
642
643 so = ss.option(form.Value, 'srv', _('SRV'), _('Syntax: <code>_service._proto.example.com</code>.'));
644 so.rmempty = false;
645 so.datatype = 'hostname';
646 so.placeholder = '_sip._tcp.example.com';
647
648 so = ss.option(form.Value, 'target', _('Target'), _('CNAME or fqdn'));
649 so.rmempty = false;
650 so.datatype = 'hostname';
651 so.placeholder = 'sip.example.com';
652
653 so = ss.option(form.Value, 'port', _('Port'));
654 so.rmempty = false;
655 so.datatype = 'port';
656 so.placeholder = '5060';
657
658 so = ss.option(form.Value, 'class', _('Priority'), _('Ordinal: lower comes first.'));
659 so.rmempty = true;
660 so.datatype = 'range(0,65535)';
661 so.placeholder = '10';
662
663 so = ss.option(form.Value, 'weight', _('Weight'));
664 so.rmempty = true;
665 so.datatype = 'range(0,65535)';
666 so.placeholder = '50';
667
668 o = s.taboption('mxhosts', form.SectionValue, '__mxhosts__', form.TableSection, 'mxhost', null,
669 _('Bind service records to a domain name: specify the location of services.')
670 + '<br />' + _('You may add multiple records for the same domain.'));
671
672 ss = o.subsection;
673
674 ss.addremove = true;
675 ss.anonymous = true;
676 ss.sortable = true;
677 ss.rowcolors = true;
678 ss.nodescriptions = true;
679
680 so = ss.option(form.Value, 'domain', _('Domain'));
681 so.rmempty = false;
682 so.datatype = 'hostname';
683 so.placeholder = 'example.com';
684
685 so = ss.option(form.Value, 'relay', _('Relay'));
686 so.rmempty = false;
687 so.datatype = 'hostname';
688 so.placeholder = 'relay.example.com';
689
690 so = ss.option(form.Value, 'pref', _('Priority'), _('Ordinal: lower comes first.'));
691 so.rmempty = true;
692 so.datatype = 'range(0,65535)';
693 so.placeholder = '0';
694
695 o = s.taboption('hosts', form.SectionValue, '__hosts__', form.GridSection, 'domain', null,
696 _('Hostnames are used to bind a domain name to an IP address. This setting is redundant for hostnames already configured with static leases, but it can be useful to rebind an FQDN.'));
697
698 ss = o.subsection;
699
700 ss.addremove = true;
701 ss.anonymous = true;
702 ss.sortable = true;
703
704 so = ss.option(form.Value, 'name', _('Hostname'));
705 so.rmempty = false;
706 so.datatype = 'hostname';
707
708 so = ss.option(form.Value, 'ip', _('IP address'));
709 so.rmempty = false;
710 so.datatype = 'ipaddr';
711
712 var ipaddrs = {};
713
714 Object.keys(hosts).forEach(function(mac) {
715 var addrs = L.toArray(hosts[mac].ipaddrs || hosts[mac].ipv4);
716
717 for (var i = 0; i < addrs.length; i++)
718 ipaddrs[addrs[i]] = hosts[mac].name || mac;
719 });
720
721 L.sortedKeys(ipaddrs, null, 'addr').forEach(function(ipv4) {
722 so.value(ipv4, '%s (%s)'.format(ipv4, ipaddrs[ipv4]));
723 });
724
725 o = s.taboption('ipsets', form.SectionValue, '__ipsets__', form.GridSection, 'ipset', null,
726 _('List of IP sets to populate with the specified domain IPs.'));
727
728 ss = o.subsection;
729
730 ss.addremove = true;
731 ss.anonymous = true;
732 ss.sortable = true;
733
734 so = ss.option(form.DynamicList, 'name', _('IP set'));
735 so.rmempty = false;
736 so.datatype = 'string';
737
738 so = ss.option(form.DynamicList, 'domain', _('Domain'));
739 so.rmempty = false;
740 so.datatype = 'hostname';
741
742 o = s.taboption('leases', form.SectionValue, '__leases__', form.GridSection, 'host', null,
743 _('Static leases are used to assign fixed IP addresses and symbolic hostnames to DHCP clients. They are also required for non-dynamic interface configurations where only hosts with a corresponding lease are served.') + '<br />' +
744 _('Use the <em>Add</em> Button to add a new lease entry. The <em>MAC address</em> identifies the host, the <em>IPv4 address</em> specifies the fixed address to use, and the <em>Hostname</em> is assigned as a symbolic name to the requesting host. The optional <em>Lease time</em> can be used to set non-standard host-specific lease time, e.g. 12h, 3d or infinite.'));
745
746 ss = o.subsection;
747
748 ss.addremove = true;
749 ss.anonymous = true;
750 ss.sortable = true;
751
752 so = ss.option(form.Value, 'name', _('Hostname'));
753 so.validate = validateHostname;
754 so.rmempty = true;
755 so.write = function(section, value) {
756 uci.set('dhcp', section, 'name', value);
757 uci.set('dhcp', section, 'dns', '1');
758 };
759 so.remove = function(section) {
760 uci.unset('dhcp', section, 'name');
761 uci.unset('dhcp', section, 'dns');
762 };
763
764 so = ss.option(form.Value, 'mac', _('MAC address'));
765 so.datatype = 'list(macaddr)';
766 so.rmempty = true;
767 so.cfgvalue = function(section) {
768 var macs = L.toArray(uci.get('dhcp', section, 'mac')),
769 result = [];
770
771 for (var i = 0, mac; (mac = macs[i]) != null; i++)
772 if (/^([0-9a-fA-F]{1,2}):([0-9a-fA-F]{1,2}):([0-9a-fA-F]{1,2}):([0-9a-fA-F]{1,2}):([0-9a-fA-F]{1,2}):([0-9a-fA-F]{1,2})$/.test(mac))
773 result.push('%02X:%02X:%02X:%02X:%02X:%02X'.format(
774 parseInt(RegExp.$1, 16), parseInt(RegExp.$2, 16),
775 parseInt(RegExp.$3, 16), parseInt(RegExp.$4, 16),
776 parseInt(RegExp.$5, 16), parseInt(RegExp.$6, 16)));
777
778 return result.length ? result.join(' ') : null;
779 };
780 so.renderWidget = function(section_id, option_index, cfgvalue) {
781 var node = form.Value.prototype.renderWidget.apply(this, [section_id, option_index, cfgvalue]),
782 ipopt = this.section.children.filter(function(o) { return o.option == 'ip' })[0];
783
784 node.addEventListener('cbi-dropdown-change', L.bind(function(ipopt, section_id, ev) {
785 var mac = ev.detail.value.value;
786 if (mac == null || mac == '' || !hosts[mac])
787 return;
788
789 var iphint = L.toArray(hosts[mac].ipaddrs || hosts[mac].ipv4)[0];
790 if (iphint == null)
791 return;
792
793 var ip = ipopt.formvalue(section_id);
794 if (ip != null && ip != '')
795 return;
796
797 var node = ipopt.map.findElement('id', ipopt.cbid(section_id));
798 if (node)
799 dom.callClassMethod(node, 'setValue', iphint);
800 }, this, ipopt, section_id));
801
802 return node;
803 };
804 so.validate = validateMACAddr.bind(so, pools);
805 Object.keys(hosts).forEach(function(mac) {
806 var hint = hosts[mac].name || L.toArray(hosts[mac].ipaddrs || hosts[mac].ipv4)[0];
807 so.value(mac, hint ? '%s (%s)'.format(mac, hint) : mac);
808 });
809
810 so = ss.option(form.Value, 'ip', _('IPv4 address'));
811 so.datatype = 'or(ip4addr,"ignore")';
812 so.validate = function(section, value) {
813 var m = this.section.formvalue(section, 'mac'),
814 n = this.section.formvalue(section, 'name');
815
816 if ((m == null || m == '') && (n == null || n == ''))
817 return _('One of hostname or MAC address must be specified!');
818
819 if (value == null || value == '' || value == 'ignore')
820 return true;
821
822 var leases = uci.sections('dhcp', 'host');
823
824 for (var i = 0; i < leases.length; i++)
825 if (leases[i]['.name'] != section && leases[i].ip == value)
826 return _('The IP address %h is already used by another static lease').format(value);
827
828 for (var i = 0; i < pools.length; i++) {
829 var net_mask = calculateNetwork(value, pools[i].netmask);
830
831 if (net_mask && net_mask[0] == pools[i].network)
832 return true;
833 }
834
835 return _('The IP address is outside of any DHCP pool address range');
836 };
837
838 L.sortedKeys(ipaddrs, null, 'addr').forEach(function(ipv4) {
839 so.value(ipv4, ipaddrs[ipv4] ? '%s (%s)'.format(ipv4, ipaddrs[ipv4]) : ipv4);
840 });
841
842 so = ss.option(form.Value, 'leasetime', _('Lease time'));
843 so.rmempty = true;
844
845 so = ss.option(form.Value, 'duid', _('DUID'));
846 so.datatype = 'and(rangelength(20,36),hexstring)';
847 Object.keys(duids).forEach(function(duid) {
848 so.value(duid, '%s (%s)'.format(duid, duids[duid].hostname || duids[duid].macaddr || duids[duid].ip6addr || '?'));
849 });
850
851 so = ss.option(form.Value, 'hostid', _('IPv6 suffix (hex)'));
852
853 o = s.taboption('leases', CBILeaseStatus, '__status__');
854
855 if (has_dhcpv6)
856 o = s.taboption('leases', CBILease6Status, '__status6__');
857
858 return m.render().then(function(mapEl) {
859 poll.add(function() {
860 return callDHCPLeases().then(function(leaseinfo) {
861 var leases = Array.isArray(leaseinfo.dhcp_leases) ? leaseinfo.dhcp_leases : [],
862 leases6 = Array.isArray(leaseinfo.dhcp6_leases) ? leaseinfo.dhcp6_leases : [];
863
864 cbi_update_table(mapEl.querySelector('#lease_status_table'),
865 leases.map(function(lease) {
866 var exp;
867
868 if (lease.expires === false)
869 exp = E('em', _('unlimited'));
870 else if (lease.expires <= 0)
871 exp = E('em', _('expired'));
872 else
873 exp = '%t'.format(lease.expires);
874
875 var hint = lease.macaddr ? hosts[lease.macaddr] : null,
876 name = hint ? hint.name : null,
877 host = null;
878
879 if (name && lease.hostname && lease.hostname != name)
880 host = '%s (%s)'.format(lease.hostname, name);
881 else if (lease.hostname)
882 host = lease.hostname;
883
884 return [
885 host || '-',
886 lease.ipaddr,
887 lease.macaddr,
888 exp
889 ];
890 }),
891 E('em', _('There are no active leases')));
892
893 if (has_dhcpv6) {
894 cbi_update_table(mapEl.querySelector('#lease6_status_table'),
895 leases6.map(function(lease) {
896 var exp;
897
898 if (lease.expires === false)
899 exp = E('em', _('unlimited'));
900 else if (lease.expires <= 0)
901 exp = E('em', _('expired'));
902 else
903 exp = '%t'.format(lease.expires);
904
905 var hint = lease.macaddr ? hosts[lease.macaddr] : null,
906 name = hint ? (hint.name || L.toArray(hint.ipaddrs || hint.ipv4)[0] || L.toArray(hint.ip6addrs || hint.ipv6)[0]) : null,
907 host = null;
908
909 if (name && lease.hostname && lease.hostname != name && lease.ip6addr != name)
910 host = '%s (%s)'.format(lease.hostname, name);
911 else if (lease.hostname)
912 host = lease.hostname;
913 else if (name)
914 host = name;
915
916 return [
917 host || '-',
918 lease.ip6addrs ? lease.ip6addrs.join(' ') : lease.ip6addr,
919 lease.duid,
920 exp
921 ];
922 }),
923 E('em', _('There are no active leases')));
924 }
925 });
926 });
927
928 return mapEl;
929 });
930 }
931 });