776 lines
23 KiB
Text
Executable file
776 lines
23 KiB
Text
Executable file
'use strict';
|
|
|
|
/*
|
|
* rpcd ucode plugin: client certificate management for luci-app-ocserv.
|
|
* Issues client certificates from the ocserv CA (/etc/ocserv/ca.pem),
|
|
* bundles them into PKCS#12 and maintains a CRL.
|
|
*
|
|
* Licensed to the public under the Apache License 2.0.
|
|
*/
|
|
|
|
import * as fs from 'fs';
|
|
import { cursor } from 'uci';
|
|
|
|
const OCDIR = '/etc/ocserv';
|
|
const VHOSTDIR = OCDIR + '/vhosts';
|
|
const ZONE = 'ocserv';
|
|
const POLICY_PREFIX = 'ocserv_';
|
|
const LOCAL = OCDIR + '/ocserv.conf.local';
|
|
const TEMPLATE = OCDIR + '/ocserv.conf.template';
|
|
const MARK_BEGIN = '# BEGIN luci-app-ocserv';
|
|
const MARK_END = '# END luci-app-ocserv';
|
|
|
|
function valid_vhost_id(id) {
|
|
return type(id) == 'string' && length(id) <= 64 && match(id, /^[A-Za-z0-9_]+$/) != null;
|
|
}
|
|
|
|
function valid_domain(domain) {
|
|
if (type(domain) != 'string' || length(domain) < 1 || length(domain) > 253 ||
|
|
!match(domain, /^[a-z0-9.-]+$/))
|
|
return false;
|
|
for (let label in split(domain, '.'))
|
|
if (length(label) < 1 || length(label) > 63 ||
|
|
!match(label, /^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/))
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
function context(vhost) {
|
|
let id = vhost ?? '';
|
|
if (type(id) != 'string')
|
|
return { error: 'Invalid vhost' };
|
|
|
|
let root = OCDIR, domain = null;
|
|
if (id != '') {
|
|
if (!valid_vhost_id(id))
|
|
return { error: 'Invalid vhost' };
|
|
|
|
let c = cursor(), section = null;
|
|
c.foreach('ocserv', 'vhost', s => {
|
|
if (s['.name'] == id)
|
|
section = s;
|
|
});
|
|
if (!section)
|
|
return { error: 'Vhost not found' };
|
|
root = VHOSTDIR + '/' + id;
|
|
domain = section.domain;
|
|
}
|
|
|
|
let pki = root + '/pki';
|
|
return {
|
|
id,
|
|
root,
|
|
pki,
|
|
clients: root + '/clients',
|
|
ca_cert: root + '/ca.pem',
|
|
ca_key: root + '/ca-key.pem',
|
|
crl: root + '/crl.pem',
|
|
cnf: pki + '/openssl.cnf',
|
|
per_user: root + '/config-per-user',
|
|
config: root + '/ocserv.conf',
|
|
domain
|
|
};
|
|
}
|
|
|
|
function openssl_cnf(ctx) {
|
|
return join('\n', [
|
|
'[ ca ]',
|
|
'default_ca = CA_default',
|
|
'[ CA_default ]',
|
|
'database = ' + ctx.pki + '/index.txt',
|
|
'new_certs_dir = ' + ctx.pki + '/newcerts',
|
|
'serial = ' + ctx.pki + '/serial',
|
|
'crlnumber = ' + ctx.pki + '/crlnumber',
|
|
'certificate = ' + ctx.ca_cert,
|
|
'private_key = ' + ctx.ca_key,
|
|
'default_md = sha256',
|
|
'default_crl_days = 3650',
|
|
'unique_subject = no',
|
|
'policy = policy_cn',
|
|
'[ policy_cn ]',
|
|
'commonName = supplied',
|
|
'[ req ]',
|
|
'distinguished_name = req_dn',
|
|
'prompt = no',
|
|
'[ req_dn ]',
|
|
'CN = client',
|
|
'[ client_ext ]',
|
|
'basicConstraints = CA:FALSE',
|
|
'keyUsage = digitalSignature, keyEncipherment',
|
|
'extendedKeyUsage = clientAuth',
|
|
'subjectKeyIdentifier = hash',
|
|
'authorityKeyIdentifier = keyid',
|
|
''
|
|
]);
|
|
}
|
|
|
|
/* rpcd is single-threaded for this plugin: keep every external command short */
|
|
function run(argv, timeout) {
|
|
return system(argv, timeout ?? 20000) == 0;
|
|
}
|
|
|
|
function ensure_pki(ctx) {
|
|
if (!fs.stat(ctx.root))
|
|
fs.mkdir(ctx.root, 0755);
|
|
for (let d in [ ctx.pki, ctx.pki + '/newcerts', ctx.clients ])
|
|
if (!fs.stat(d))
|
|
fs.mkdir(d, 0700);
|
|
|
|
if (!fs.access(ctx.ca_cert) || !fs.access(ctx.ca_key)) {
|
|
if (!ctx.id)
|
|
return 'CA certificate/key not found in ' + ctx.root + ' (start ocserv once to create them)';
|
|
if (fs.access(ctx.ca_cert) || fs.access(ctx.ca_key))
|
|
return 'Incomplete CA certificate/key pair in ' + ctx.root;
|
|
if (!valid_domain(ctx.domain) ||
|
|
/* EC P-256 takes milliseconds; RSA 3072 can block a small router for minutes */
|
|
!run([ 'openssl', 'ecparam', '-name', 'prime256v1', '-genkey', '-noout', '-out', ctx.ca_key ]) ||
|
|
!run([ 'openssl', 'req', '-x509', '-new', '-sha256', '-days', '3650',
|
|
'-key', ctx.ca_key, '-subj', '/CN=' + ctx.domain + ' VPN CA', '-out', ctx.ca_cert ]))
|
|
return 'Failed to generate the virtual host CA in ' + ctx.root;
|
|
fs.chmod(ctx.ca_key, 0600);
|
|
fs.chmod(ctx.ca_cert, 0644);
|
|
}
|
|
|
|
if (!fs.access(ctx.cnf))
|
|
fs.writefile(ctx.cnf, openssl_cnf(ctx));
|
|
if (!fs.access(ctx.pki + '/index.txt'))
|
|
fs.writefile(ctx.pki + '/index.txt', '');
|
|
if (!fs.access(ctx.pki + '/index.txt.attr'))
|
|
fs.writefile(ctx.pki + '/index.txt.attr', 'unique_subject = no\n');
|
|
if (!fs.access(ctx.pki + '/serial'))
|
|
fs.writefile(ctx.pki + '/serial', '01\n');
|
|
if (!fs.access(ctx.pki + '/crlnumber'))
|
|
fs.writefile(ctx.pki + '/crlnumber', '01\n');
|
|
|
|
return null;
|
|
}
|
|
|
|
function gen_crl(ctx) {
|
|
return run([ 'openssl', 'ca', '-batch', '-config', ctx.cnf, '-gencrl', '-out', ctx.crl ]);
|
|
}
|
|
|
|
function fmt_date(s) {
|
|
/* openssl index date: YYMMDDHHMMSSZ[,reason] */
|
|
let m = match(s, /^([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/);
|
|
return m ? sprintf('20%s-%s-%s %s:%s', m[1], m[2], m[3], m[4], m[5]) : s;
|
|
}
|
|
|
|
function reload_pbr() {
|
|
if (fs.access('/etc/init.d/pbr'))
|
|
system([ '/etc/init.d/pbr', 'reload' ], 30000);
|
|
}
|
|
|
|
function reload_firewall() {
|
|
system([ '/etc/init.d/firewall', 'reload' ], 30000);
|
|
}
|
|
|
|
function ip4_to_int(s) {
|
|
let m = match(s ?? '', /^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/);
|
|
if (!m)
|
|
return null;
|
|
let o = [ +m[1], +m[2], +m[3], +m[4] ];
|
|
for (let x in o)
|
|
if (x > 255)
|
|
return null;
|
|
return o[0] * 16777216 + o[1] * 65536 + o[2] * 256 + o[3];
|
|
}
|
|
|
|
function int2ip(n) {
|
|
return sprintf('%d.%d.%d.%d', (n >> 24) & 255, (n >> 16) & 255, (n >> 8) & 255, n & 255);
|
|
}
|
|
|
|
/* the client address pool as { net, bcast, server } host integers, or null */
|
|
function pool_range(ctx) {
|
|
let ip, mask;
|
|
if (ctx.id == '') {
|
|
let c = cursor();
|
|
ip = ip4_to_int(c.get('ocserv', 'config', 'ipaddr'));
|
|
mask = ip4_to_int(c.get('ocserv', 'config', 'netmask'));
|
|
} else {
|
|
let config = fs.readfile(ctx.config) ?? '';
|
|
let network = match(config, /(^|\n)[ \t]*ipv4-network[ \t]*=[ \t]*"?([0-9.]+)"?/);
|
|
let netmask = match(config, /(^|\n)[ \t]*ipv4-netmask[ \t]*=[ \t]*"?([0-9.]+)"?/);
|
|
ip = ip4_to_int(network ? network[2] : null);
|
|
mask = ip4_to_int(netmask ? netmask[2] : null);
|
|
}
|
|
if (ip == null || mask == null)
|
|
return null;
|
|
let net = ip & mask, bcast = net | (~mask & 0xffffffff);
|
|
/* ocserv keeps the first address of the pool for itself: with a plain network
|
|
address (10.0.0.0) the server is 10.0.0.1, and a client cannot get it */
|
|
return { net, bcast, server: ip == net ? net + 1 : ip };
|
|
}
|
|
|
|
/* explicit-ipv4 of every config-per-user file: { cn: ip } */
|
|
function user_ips(ctx) {
|
|
let res = {};
|
|
for (let f in fs.lsdir(ctx.per_user) ?? []) {
|
|
let data = fs.readfile(ctx.per_user + '/' + f) ?? '';
|
|
let m = match(data, /(^|\n)[ \t]*explicit-ipv4[ \t]*=[ \t]*"?([0-9.]+)"?/);
|
|
if (m)
|
|
res[f] = m[2];
|
|
}
|
|
return res;
|
|
}
|
|
|
|
/* sets (or, with value == null, removes) one directive in config-per-user/<cn>, keeping the rest */
|
|
function set_directive(ctx, cn, key, value) {
|
|
let file = ctx.per_user + '/' + cn;
|
|
let lines = [];
|
|
for (let l in split(fs.readfile(file) ?? '', '\n'))
|
|
if (!match(l, regexp('^[ \t]*' + key + '[ \t]*=')))
|
|
push(lines, l);
|
|
while (length(lines) && lines[length(lines) - 1] == '')
|
|
pop(lines);
|
|
if (value != null)
|
|
push(lines, key + ' = ' + value);
|
|
|
|
if (!length(lines)) {
|
|
fs.unlink(file);
|
|
return;
|
|
}
|
|
if (!fs.stat(ctx.per_user))
|
|
fs.mkdir(ctx.per_user, 0755);
|
|
fs.writefile(file, join('\n', lines) + '\n');
|
|
}
|
|
|
|
/* ip == '' picks the first free pool address; returns { ip } or { error } */
|
|
function pick_ip(ctx, cn, ip) {
|
|
let pool = pool_range(ctx);
|
|
if (!pool)
|
|
return { error: ctx.id ? 'The client address pool is not configured' : 'The client address pool is not configured (Server page)' };
|
|
|
|
let used = {}, ips = user_ips(ctx);
|
|
for (let u in keys(ips)) {
|
|
if (u == cn)
|
|
continue;
|
|
let n = ip4_to_int(ips[u]);
|
|
if (n != null)
|
|
used[n] = true;
|
|
}
|
|
|
|
if (ip) {
|
|
let n = ip4_to_int(ip);
|
|
if (n == null || n <= pool.net || n >= pool.bcast || (n & ~(pool.net ^ pool.bcast) & 0xffffffff) != pool.net)
|
|
return { error: 'The address is outside the client pool' };
|
|
if (n == pool.server)
|
|
return { error: 'This is the server address' };
|
|
if (used[n])
|
|
return { error: 'The address is already used by another user' };
|
|
return { ip: int2ip(n) };
|
|
}
|
|
|
|
for (let n = pool.net + 1; n < pool.bcast; n++)
|
|
if (n != pool.server && !used[n])
|
|
return { ip: int2ip(n) };
|
|
return { error: 'No free addresses left in the client pool' };
|
|
}
|
|
|
|
function as_list(v) {
|
|
if (v == null)
|
|
return [];
|
|
if (type(v) == 'array')
|
|
return filter(map(v, x => trim('' + x)), x => x != '');
|
|
return filter(split(trim('' + v), /[ ,]+/), x => x != '');
|
|
}
|
|
|
|
function policy_name(ctx, cn) {
|
|
return POLICY_PREFIX + (ctx.id ? ctx.id + '_' : '') + cn;
|
|
}
|
|
|
|
function policy_of(c, ctx, cn) {
|
|
let found = null, name = policy_name(ctx, cn);
|
|
c.foreach('pbr', 'policy', s => {
|
|
if (s.name == name)
|
|
found = s['.name'];
|
|
});
|
|
return found;
|
|
}
|
|
|
|
/* exit interface of a user (from its pbr policy), '' for the default route */
|
|
function via_of(c, ctx, cn) {
|
|
let pol = policy_of(c, ctx, cn);
|
|
return pol ? (c.get('pbr', pol, 'interface') ?? '') : '';
|
|
}
|
|
|
|
/*
|
|
* The tunnel devices (vpns+) must sit in a firewall zone, forwarded to wan and to the zone of every
|
|
* exit interface in use. Creates the missing zone/forwardings (exit zones masquerade like in
|
|
* luci-app-amneziawg) and reloads the firewall if anything changed. c must have 'firewall' loaded.
|
|
*/
|
|
function ensure_zone(c, via) {
|
|
let changed = false, warning = null;
|
|
|
|
let have = false;
|
|
c.foreach('firewall', 'zone', z => {
|
|
if (z.name == ZONE)
|
|
have = true;
|
|
});
|
|
if (!have) {
|
|
c.set('firewall', 'ocserv_zone', 'zone');
|
|
c.set('firewall', 'ocserv_zone', 'name', ZONE);
|
|
c.set('firewall', 'ocserv_zone', 'device', [ 'vpns+' ]);
|
|
c.set('firewall', 'ocserv_zone', 'input', 'ACCEPT');
|
|
c.set('firewall', 'ocserv_zone', 'output', 'ACCEPT');
|
|
c.set('firewall', 'ocserv_zone', 'forward', 'ACCEPT');
|
|
changed = true;
|
|
}
|
|
|
|
let dests = [ 'wan' ];
|
|
if (via) {
|
|
let zone = null, zsid = null;
|
|
c.foreach('firewall', 'zone', z => {
|
|
if (z.name != ZONE && (via in as_list(z.network))) {
|
|
zone = z.name;
|
|
zsid = z['.name'];
|
|
}
|
|
});
|
|
if (!zone) {
|
|
/* fw4 zone names are limited to 11 characters */
|
|
zone = substr('vpn_' + via, 0, 11);
|
|
zsid = 'ocserv_exit_' + via;
|
|
c.set('firewall', zsid, 'zone');
|
|
c.set('firewall', zsid, 'name', zone);
|
|
c.set('firewall', zsid, 'network', [ via ]);
|
|
c.set('firewall', zsid, 'input', 'REJECT');
|
|
c.set('firewall', zsid, 'output', 'ACCEPT');
|
|
c.set('firewall', zsid, 'forward', 'REJECT');
|
|
c.set('firewall', zsid, 'masq', '1');
|
|
c.set('firewall', zsid, 'mtu_fix', '1');
|
|
changed = true;
|
|
}
|
|
else if (c.get('firewall', zsid, 'masq') != '1') {
|
|
warning = 'The firewall zone "' + zone + '" of ' + via + ' has no masquerading: replies from upstream may not come back';
|
|
}
|
|
push(dests, zone);
|
|
}
|
|
|
|
for (let dest in dests) {
|
|
let fwd = false;
|
|
c.foreach('firewall', 'forwarding', f => {
|
|
if (f.src == ZONE && f.dest == dest)
|
|
fwd = true;
|
|
});
|
|
if (!fwd) {
|
|
let n = 'ocserv_fwd_' + replace(dest, '-', '_');
|
|
c.set('firewall', n, 'forwarding');
|
|
c.set('firewall', n, 'src', ZONE);
|
|
c.set('firewall', n, 'dest', dest);
|
|
changed = true;
|
|
}
|
|
}
|
|
|
|
if (changed) {
|
|
c.commit('firewall');
|
|
reload_firewall();
|
|
}
|
|
return { warning };
|
|
}
|
|
|
|
/* via = '' removes the policy (the user goes out through the default route) */
|
|
function apply_route(ctx, cn, ip, via) {
|
|
if (via && !match(via, /^[A-Za-z0-9_]+$/))
|
|
return { error: 'Invalid interface' };
|
|
if (via && !fs.access('/etc/init.d/pbr'))
|
|
return { error: 'The pbr package is not installed' };
|
|
|
|
let c = cursor();
|
|
c.load('pbr');
|
|
c.load('firewall');
|
|
|
|
let exit = ensure_zone(c, via);
|
|
|
|
let pol = policy_of(c, ctx, cn);
|
|
if (!via) {
|
|
if (pol)
|
|
c.delete('pbr', pol);
|
|
} else {
|
|
if (!pol)
|
|
pol = c.add('pbr', 'policy');
|
|
c.set('pbr', pol, 'name', policy_name(ctx, cn));
|
|
c.set('pbr', pol, 'src_addr', ip);
|
|
c.set('pbr', pol, 'interface', via);
|
|
c.set('pbr', pol, 'enabled', '1');
|
|
/* pbr ships disabled; policies do nothing until it is switched on */
|
|
if (c.get('pbr', 'config', 'enabled') != '1')
|
|
c.set('pbr', 'config', 'enabled', '1');
|
|
}
|
|
c.commit('pbr');
|
|
reload_pbr();
|
|
return exit.warning ? { ok: true, warning: exit.warning } : { ok: true };
|
|
}
|
|
|
|
function read_index(ctx) {
|
|
let res = [];
|
|
let data = fs.readfile(ctx.pki + '/index.txt');
|
|
if (!data)
|
|
return res;
|
|
|
|
let c = cursor();
|
|
c.load('pbr');
|
|
let ips = user_ips(ctx);
|
|
for (let line in split(data, '\n')) {
|
|
let f = split(line, '\t');
|
|
if (length(f) < 6)
|
|
continue;
|
|
let cn = match(f[5], /\/CN=([^\/]+)/);
|
|
let status = (f[0] == 'R') ? 'revoked' : 'valid';
|
|
push(res, {
|
|
cn: cn ? cn[1] : f[5],
|
|
serial: f[3],
|
|
status,
|
|
expires: fmt_date(f[1]),
|
|
revoked: f[2] ? fmt_date(f[2]) : null,
|
|
has_p12: (cn && fs.access(ctx.clients + '/' + cn[1] + '.p12')) ? true : false,
|
|
has_key: (cn && fs.access(ctx.clients + '/' + cn[1] + '.key')) ? true : false,
|
|
has_crt: (cn && fs.access(ctx.pki + '/newcerts/' + f[3] + '.pem')) ? true : false,
|
|
ip: cn ? (ips[cn[1]] ?? '') : '',
|
|
via: (cn && status == 'valid') ? via_of(c, ctx, cn[1]) : ''
|
|
});
|
|
}
|
|
return res;
|
|
}
|
|
|
|
function valid_cn(cn) {
|
|
return type(cn) == 'string' && length(cn) <= 64 && match(cn, /^[A-Za-z0-9._-]+$/) != null;
|
|
}
|
|
|
|
function make_vhost_id(c, domain) {
|
|
let base = 'vh_' + substr(replace(domain, /[.-]/g, '_'), 0, 56);
|
|
let id = base, n = 2;
|
|
while (c.get('ocserv', id) != null || fs.stat(VHOSTDIR + '/' + id))
|
|
id = substr(base, 0, 59) + '_' + n++;
|
|
return id;
|
|
}
|
|
|
|
const methods = {
|
|
list: {
|
|
args: { vhost: '' },
|
|
call: function(req) {
|
|
let ctx = context(req.args.vhost);
|
|
if (ctx.error)
|
|
return ctx;
|
|
|
|
/* interfaces a user can be sent out through (the default route if none is chosen) */
|
|
let c = cursor(), ifaces = [];
|
|
c.foreach('network', 'interface', s => {
|
|
if (s['.name'] != 'loopback' && s.proto != 'amneziawg')
|
|
push(ifaces, s['.name']);
|
|
});
|
|
return {
|
|
certs: read_index(ctx),
|
|
ca: fs.access(ctx.ca_cert) ? true : false,
|
|
ifaces,
|
|
pbr: fs.access('/etc/init.d/pbr') ? true : false
|
|
};
|
|
}
|
|
},
|
|
|
|
issue: {
|
|
args: { cn: 'cn', days: 365, password: 'password', ip: '', via: '', vhost: '' },
|
|
call: function(req) {
|
|
let cn = req.args.cn, days = +req.args.days, pw = req.args.password;
|
|
let via = req.args.via ?? '', ctx = context(req.args.vhost);
|
|
if (ctx.error)
|
|
return ctx;
|
|
|
|
if (!valid_cn(cn))
|
|
return { error: 'Invalid name: use letters, digits, ".", "_" and "-" (max 64)' };
|
|
if (!(days >= 1 && days <= 36500))
|
|
return { error: 'Invalid validity period' };
|
|
if (type(pw) != 'string' || length(pw) < 1)
|
|
return { error: 'PKCS#12 password must not be empty' };
|
|
|
|
if (via && !match(via, /^[A-Za-z0-9_]+$/))
|
|
return { error: 'Invalid interface' };
|
|
|
|
let err = ensure_pki(ctx);
|
|
if (err)
|
|
return { error: err };
|
|
|
|
for (let c in read_index(ctx))
|
|
if (c.cn == cn && c.status == 'valid')
|
|
return { error: 'A valid certificate for this name already exists; revoke it first' };
|
|
|
|
let addr = pick_ip(ctx, cn, req.args.ip);
|
|
if (addr.error)
|
|
return addr;
|
|
|
|
let tmp = fs.mkdtemp('/tmp/ocserv-pki.XXXXXX');
|
|
if (!tmp)
|
|
return { error: 'Cannot create temporary directory' };
|
|
|
|
let key = tmp + '/key.pem', csr = tmp + '/req.pem', crt = tmp + '/crt.pem', pwf = tmp + '/pw';
|
|
let p12 = ctx.clients + '/' + cn + '.p12';
|
|
let step = null;
|
|
|
|
fs.writefile(pwf, pw);
|
|
|
|
if (!run([ 'openssl', 'genrsa', '-out', key, '2048' ], 50000))
|
|
step = 'key generation';
|
|
else if (!run([ 'openssl', 'req', '-new', '-config', ctx.cnf, '-key', key, '-subj', '/CN=' + cn, '-out', csr ]))
|
|
step = 'request creation';
|
|
else if (!run([ 'openssl', 'ca', '-batch', '-config', ctx.cnf, '-in', csr, '-out', crt, '-days', '' + days,
|
|
'-notext', '-extensions', 'client_ext', '-subj', '/CN=' + cn ]))
|
|
step = 'signing';
|
|
else if (!run([ 'openssl', 'pkcs12', '-export', '-inkey', key, '-in', crt, '-certfile', ctx.ca_cert,
|
|
'-name', cn, '-out', p12, '-passout', 'file:' + pwf ]))
|
|
step = 'PKCS#12 export';
|
|
else {
|
|
fs.chmod(p12, 0600);
|
|
/* keep the key and certificate too, so they can be downloaded separately */
|
|
fs.writefile(ctx.clients + '/' + cn + '.key', fs.readfile(key));
|
|
fs.chmod(ctx.clients + '/' + cn + '.key', 0600);
|
|
fs.writefile(ctx.clients + '/' + cn + '.crt', fs.readfile(crt));
|
|
fs.chmod(ctx.clients + '/' + cn + '.crt', 0644);
|
|
}
|
|
|
|
system([ 'rm', '-rf', tmp ]);
|
|
|
|
if (step)
|
|
return { error: 'Failed at ' + step };
|
|
|
|
gen_crl(ctx);
|
|
|
|
set_directive(ctx, cn, 'explicit-ipv4', addr.ip);
|
|
let route = apply_route(ctx, cn, addr.ip, via);
|
|
if (route.error)
|
|
return { ok: true, cn, ip: addr.ip, warning: route.error };
|
|
return { ok: true, cn, ip: addr.ip, warning: route.warning };
|
|
}
|
|
},
|
|
|
|
revoke: {
|
|
args: { serial: 'serial', vhost: '' },
|
|
call: function(req) {
|
|
let serial = req.args.serial, ctx = context(req.args.vhost);
|
|
if (ctx.error)
|
|
return ctx;
|
|
if (type(serial) != 'string' || !match(serial, /^[0-9A-Fa-f]+$/))
|
|
return { error: 'Invalid serial' };
|
|
|
|
let err = ensure_pki(ctx);
|
|
if (err)
|
|
return { error: err };
|
|
|
|
let entry = null;
|
|
for (let c in read_index(ctx))
|
|
if (uc(c.serial) == uc(serial))
|
|
entry = c;
|
|
if (!entry || entry.status != 'valid')
|
|
return { error: 'Certificate not found or already revoked' };
|
|
|
|
if (!run([ 'openssl', 'ca', '-batch', '-config', ctx.cnf, '-revoke', ctx.pki + '/newcerts/' + entry.serial + '.pem' ]))
|
|
return { error: 'Revocation failed' };
|
|
if (!gen_crl(ctx))
|
|
return { error: 'CRL generation failed' };
|
|
|
|
for (let ext in [ 'p12', 'key', 'crt' ])
|
|
fs.unlink(ctx.clients + '/' + entry.cn + '.' + ext);
|
|
set_directive(ctx, entry.cn, 'explicit-ipv4', null);
|
|
apply_route(ctx, entry.cn, '', '');
|
|
system([ '/usr/bin/occtl', 'reload' ], 10000);
|
|
return { ok: true };
|
|
}
|
|
},
|
|
|
|
/* changes the fixed address and/or the exit interface of an issued user */
|
|
set_user: {
|
|
args: { cn: 'cn', ip: '', via: '', vhost: '' },
|
|
call: function(req) {
|
|
let cn = req.args.cn, via = req.args.via ?? '', ctx = context(req.args.vhost);
|
|
if (ctx.error)
|
|
return ctx;
|
|
if (!valid_cn(cn))
|
|
return { error: 'Invalid name' };
|
|
|
|
let found = false;
|
|
for (let e in read_index(ctx))
|
|
if (e.cn == cn && e.status == 'valid')
|
|
found = true;
|
|
if (!found)
|
|
return { error: 'No valid certificate for this name' };
|
|
|
|
let addr = pick_ip(ctx, cn, req.args.ip);
|
|
if (addr.error)
|
|
return addr;
|
|
|
|
set_directive(ctx, cn, 'explicit-ipv4', addr.ip);
|
|
return apply_route(ctx, cn, addr.ip, via);
|
|
}
|
|
},
|
|
|
|
/* kind: crt | key | p12; returns the file base64-encoded (the p12 is binary) */
|
|
get_file: {
|
|
args: { cn: 'cn', kind: 'crt', vhost: '' },
|
|
call: function(req) {
|
|
let cn = req.args.cn, kind = req.args.kind, ctx = context(req.args.vhost);
|
|
if (ctx.error)
|
|
return ctx;
|
|
if (!valid_cn(cn))
|
|
return { error: 'Invalid name' };
|
|
if (!(kind in [ 'crt', 'key', 'p12' ]))
|
|
return { error: 'Invalid file type' };
|
|
|
|
let path = ctx.clients + '/' + cn + '.' + kind;
|
|
if (kind == 'crt' && !fs.access(path)) {
|
|
/* certificates issued before the crt was kept: take it from the CA database */
|
|
for (let e in read_index(ctx))
|
|
if (e.cn == cn && e.status == 'valid')
|
|
path = ctx.pki + '/newcerts/' + e.serial + '.pem';
|
|
}
|
|
|
|
let data = fs.readfile(path);
|
|
if (data == null)
|
|
return { error: 'File not found' };
|
|
return { name: cn + '.' + (kind == 'key' ? 'key' : kind == 'crt' ? 'crt' : 'p12'), data: b64enc(data) };
|
|
}
|
|
},
|
|
|
|
create_vhost: {
|
|
args: { domain: 'domain' },
|
|
call: function(req) {
|
|
let domain = req.args.domain;
|
|
if (!valid_domain(domain))
|
|
return { error: 'Invalid domain: use a lowercase DNS hostname' };
|
|
|
|
let c = cursor(), duplicate = false;
|
|
c.foreach('ocserv', 'vhost', s => {
|
|
if (lc(s.domain ?? '') == domain)
|
|
duplicate = true;
|
|
});
|
|
if (duplicate)
|
|
return { error: 'A vhost for this domain already exists' };
|
|
|
|
let id = make_vhost_id(c, domain);
|
|
let root = VHOSTDIR + '/' + id;
|
|
|
|
if (!fs.stat(VHOSTDIR))
|
|
fs.mkdir(VHOSTDIR, 0755);
|
|
fs.mkdir(root, 0755);
|
|
for (let dir in [ root + '/pki', root + '/clients', root + '/config-per-user', root + '/config-per-group' ])
|
|
fs.mkdir(dir, 0700);
|
|
fs.writefile(root + '/ocserv.conf', '');
|
|
|
|
c.set('ocserv', id, 'vhost');
|
|
c.set('ocserv', id, 'domain', domain);
|
|
/* Keep a new host disabled until its external server certificate paths are set. */
|
|
c.set('ocserv', id, 'enabled', '0');
|
|
c.set('ocserv', id, 'auth', 'plain');
|
|
c.commit('ocserv');
|
|
|
|
let vctx = context(id);
|
|
let pki_error = ensure_pki(vctx);
|
|
/* certificate auth needs crl.pem to exist, or ocserv refuses to start */
|
|
if (!pki_error && !gen_crl(vctx))
|
|
pki_error = 'Failed to generate the CRL in ' + vctx.root;
|
|
return pki_error ? { ok: true, id, warning: pki_error } : { ok: true, id };
|
|
}
|
|
},
|
|
|
|
delete_vhost: {
|
|
args: { id: 'id', purge: false },
|
|
call: function(req) {
|
|
let id = req.args.id;
|
|
if (!valid_vhost_id(id))
|
|
return { error: 'Invalid vhost' };
|
|
|
|
let c = cursor(), found = false, users = [];
|
|
c.foreach('ocserv', 'vhost', s => {
|
|
if (s['.name'] == id)
|
|
found = true;
|
|
});
|
|
if (!found)
|
|
return { error: 'Vhost not found' };
|
|
|
|
c.foreach('ocserv', 'ocservusers', s => {
|
|
if (s.vhost == id)
|
|
push(users, s['.name']);
|
|
});
|
|
/* drop the pbr exit policies of this vhost's certificates (exact names, no prefix match) */
|
|
let vctx = context(id), pbr_changed = false;
|
|
if (!vctx.error && fs.access('/etc/init.d/pbr')) {
|
|
let pc = cursor();
|
|
pc.load('pbr');
|
|
for (let e in read_index(vctx)) {
|
|
let pol = policy_of(pc, vctx, e.cn);
|
|
if (pol) {
|
|
pc.delete('pbr', pol);
|
|
pbr_changed = true;
|
|
}
|
|
}
|
|
if (pbr_changed) {
|
|
pc.commit('pbr');
|
|
reload_pbr();
|
|
}
|
|
}
|
|
|
|
for (let sid in users)
|
|
c.delete('ocserv', sid);
|
|
c.delete('ocserv', id);
|
|
c.commit('ocserv');
|
|
|
|
if (req.args.purge == true)
|
|
system([ '/bin/rm', '-rf', '--', VHOSTDIR + '/' + id ], 30000);
|
|
|
|
/* the config is generated by the init script, so a restart is needed to drop the vhost */
|
|
if (fs.access('/etc/init.d/ocserv'))
|
|
system([ '/etc/init.d/ocserv', 'restart' ], 30000);
|
|
return { ok: true };
|
|
}
|
|
},
|
|
|
|
/* Rewrites the auth-related block of ocserv.conf.local from UCI settings */
|
|
apply_auth: {
|
|
args: { auth: 'plain', plain_fallback: false },
|
|
call: function(req) {
|
|
let auth = req.args.auth, also_plain = req.args.plain_fallback == true;
|
|
let ctx = context('');
|
|
if (!(auth in [ 'plain', 'PAM', 'certificate' ]))
|
|
return { error: 'Invalid auth method' };
|
|
|
|
let old = fs.readfile(LOCAL) ?? '';
|
|
let out = [], skip = false;
|
|
for (let line in split(old, '\n')) {
|
|
if (line == MARK_BEGIN) { skip = true; continue; }
|
|
if (line == MARK_END) { skip = false; continue; }
|
|
if (!skip)
|
|
push(out, line);
|
|
}
|
|
while (length(out) && out[length(out) - 1] == '')
|
|
pop(out);
|
|
|
|
if (auth == 'certificate') {
|
|
let err = ensure_pki(ctx);
|
|
if (err)
|
|
return { error: err };
|
|
if (!fs.access(ctx.crl))
|
|
gen_crl(ctx);
|
|
|
|
/* directives already set in the template (edited in LuCI) win */
|
|
let tpl = fs.readfile(TEMPLATE) ?? '';
|
|
let wanted = {
|
|
'ca-cert': ctx.ca_cert,
|
|
'cert-user-oid': '2.5.4.3',
|
|
'crl': fs.access(ctx.crl) ? ctx.crl : null
|
|
};
|
|
|
|
push(out, '', MARK_BEGIN);
|
|
for (let key in keys(wanted))
|
|
if (wanted[key] != null && !match(tpl, regexp('^' + key + '[ \t]*=', 's')))
|
|
push(out, key + ' = ' + wanted[key]);
|
|
if (also_plain)
|
|
push(out, 'enable-auth = "plain[passwd=/var/etc/ocpasswd]"');
|
|
push(out, MARK_END);
|
|
}
|
|
|
|
if (!length(out) && !fs.access(LOCAL))
|
|
return { ok: true };
|
|
|
|
fs.writefile(LOCAL, join('\n', out) + (length(out) ? '\n' : ''));
|
|
return { ok: true };
|
|
}
|
|
}
|
|
};
|
|
|
|
return { 'luci.ocserv-pki': methods };
|