nginx-util: use UCI for server configuration
**tl;dr:** The functions `{add,del}_ssl` modify a server
section of the UCI config if there is no `.conf` file with
the same name in `/etc/nginx/conf.d/`.
Then `init_lan` creates `/var/lib/nginx/uci.conf` files by
copying the `/etc/nginx/uci.conf.template` and standard
options from the UCI config; additionally the special path
`logd` can be used in `{access,error}_log`.
The init does not change the configuration beside
re-creating self-signed certificates when needed. This is
also the only purpose of the new `check_ssl`, which is
installed as yearly cron job.
**Initialization:**
Invoking `nginx-util init_lan` parses the UCI configuration
for package `nginx`. It creates a server part in
`/var/lib/nginx/uci.conf` for each `section server '$name'`
by copying all UCI options but the following:
* `option uci_manage_ssl` is skipped. It is set to
'self-signed' by `nginx-util add_ssl $name`, removed by
`nginx-util del_ssl $name` and used by
`nginx-util check_ssl` (see below).
* `logd` as path in `error_log` or `access_log` writes them
to STDERR respective STDOUT, which are fowarded by Nginx's
init to the log daemon. Specifically:
`option error_log 'logd'` becomes `error_log stderr;` and
`option access_log 'logd openwrt'` becomes
`access_log /proc/self/fd/1 openwrt;`
Other `[option|list] key 'value'` entries just become
`key value;` directives.
The init.d calls internally also `check_ssl` for rebuilding
self-signed SSL certificates if needed (see below). And it
still sets up `/var/lib/nginx/lan{,_ssl}.listen` files as
it is doing in the current version (so they stay available).
**Defaults:**
The package installs the file `/etc/nginx/restrict_locally`
containing allow/deny directives for restricting the access
to LAN addresses by including it into a server part. The
default server '_lan' includes this file and listens on all
IPs (instead of only the local IPs as it did before; other
servers do not need to listen explicitly on the local IPs
anymore). The default server is contained together with a
server that redirects HTTP requests for inexistent URLs to
HTTPS in the UCI configuration file `/etc/config/nginx`.
Furthermore, the packages installs a
`/etc/nginx/uci.conf.template` containing the current setup
and a marker, which will be replaced by the created UCI
servers when calling `init_lan`.
**Other:**
If there is a file named `/etc/nginx/conf.d/$name.conf` the
functions `init_lan`, `add_ssl $name` and `del_ssl $name`
will use that file instead of a UCI server section (this is
similar to the current version).
Else it selects the UCI `section server $name`, or, when
there is no such section, it searches for the first one
having `option server_name '… $name …'`. For this section:
* `nginx-util add_ssl $name` will add to it:
`option uci_manage_ssl 'self-signed'`
`option ssl_certificate '/etc/nginx/conf.d/$name.crt'`
`option ssl_certificate_key '/etc/nginx/conf.d/$name.key'`
`option ssl_session_cache 'shared:SSL:32k'`
`option ssl_session_timeout '64m'`
If these options are already present, they will stay the
same; just the first option `uci_manage_ssl` will always be
changed to 'self-signed'. The command also changes all
`listen` list items to use port 443 and ssl instead of port
80 (without ssl). If they stated another port than 80
before, they are kept the same. Furthermore, it creates a
self-signed SSL certificate if necessary, i.e., if there is
no *valid* certificate and key at the locations given by
the options `ssl_certificate` and `ssl_certificate_key`.
* `nginx-util del_ssl $name` checks if `uci_manage_ssl` is
set 'self-signed' in the corresponding UCI section. Only
then it removes all of the above options regardless of the
value looking just at the key name. Then, it also changes
all `listen` list items to use port 80 (without ssl)
instead of port 443 with ssl. If stating another port than
443, they are kept the same. Furthermore, it removes the
SSL certificate and key that were indicated by
`ssl_certificate{,_key}`.
* `nginx-util check_ssl` looks through all server sections
of the UCI config for `uci_manage_ssl 'self-signed'`. On
every hit it checks if the SSL certificate-key-pair
indicated by the options `ssl_certificate{,_key}` is
expired. Then it re-creates a self-signed certificate.
If there exists at least one `section server` with
`uci_manage_ssl 'self-signed'`, it will try to install
itself as cron job. If there are no such sections, it
removes that cron job if possible.
For installing a ssl certificate and key managed by
another app, you can call:
`nginx-util add_ssl $name $manager $crtpath $keypath`
Hereby `$name` is as above, `$manager` is an arbitrary
string, and the the ssl certificate and its key are
indicated by their absolute path. If you want to remove
the directives again, then you can use:
`nginx-util del_ssl $name $manager`
Signed-off-by: Peter Stadler <peter.stadler@student.uibk.ac.at>
4 years ago |
|
- #ifndef _UCI_CXX_HPP
- #define _UCI_CXX_HPP
-
- #include <uci.h>
- #include <memory>
- #include <mutex>
- #include <stdexcept>
- #include <string>
- #include <string_view>
-
- namespace uci {
-
- template <class T>
- class iterator { // like uci_foreach_element_safe.
-
- private:
- const uci_ptr& _ptr;
-
- uci_element* _it = nullptr;
-
- uci_element* _next = nullptr;
-
- // wrapper for clang-tidy
- inline auto _list_to_element(const uci_list* cur) -> uci_element*
- {
- // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic,cppcoreguidelines-pro-type-cstyle-cast)
- return list_to_element(cur); // macro casting container=pointer-offset.
- }
-
- public:
- inline explicit iterator(const uci_ptr& ptr, const uci_list* cur)
- : _ptr{ptr}, _it{_list_to_element(cur)}
- {
- _next = _list_to_element(_it->list.next);
- }
-
- inline iterator(iterator&&) noexcept = default;
-
- inline iterator(const iterator&) = delete;
-
- inline auto operator=(const iterator&) -> iterator& = delete;
-
- inline auto operator=(iterator &&) -> iterator& = delete;
-
- auto operator*() -> T
- {
- return T{_ptr, _it};
- }
-
- inline auto operator!=(const iterator& rhs) -> bool
- {
- return (&_it->list != &rhs._it->list);
- }
-
- inline auto operator++() -> iterator&
- {
- _it = _next;
- _next = _list_to_element(_next->list.next);
- return *this;
- }
-
- inline ~iterator() = default;
- };
-
- class locked_context {
- private:
- static std::mutex inuse;
-
- public:
- inline locked_context()
- {
- inuse.lock();
- }
-
- inline locked_context(locked_context&&) noexcept = default;
-
- inline locked_context(const locked_context&) = delete;
-
- inline auto operator=(const locked_context&) -> locked_context& = delete;
-
- inline auto operator=(locked_context &&) -> locked_context& = delete;
-
- // NOLINTNEXTLINE(readability-convert-member-functions-to-static)
- inline auto get() -> uci_context* // is member to enforce inuse
- {
- static auto free_ctx = [](uci_context* ctx) { uci_free_context(ctx); };
- static std::unique_ptr<uci_context, decltype(free_ctx)> lazy_ctx{uci_alloc_context(),
- free_ctx};
-
- if (!lazy_ctx) { // it could be available on a later call:
- lazy_ctx.reset(uci_alloc_context());
- if (!lazy_ctx) {
- throw std::runtime_error("uci error: cannot allocate context");
- }
- }
-
- return lazy_ctx.get();
- }
-
- inline ~locked_context()
- {
- inuse.unlock();
- }
- };
-
- template <class T>
- class element {
- private:
- uci_list* _begin = nullptr;
-
- uci_list* _end = nullptr;
-
- uci_ptr _ptr{};
-
- protected:
- [[nodiscard]] inline auto ptr() -> uci_ptr&
- {
- return _ptr;
- }
-
- [[nodiscard]] inline auto ptr() const -> const uci_ptr&
- {
- return _ptr;
- }
-
- void init_begin_end(uci_list* begin, uci_list* end)
- {
- _begin = begin;
- _end = end;
- }
-
- inline explicit element(const uci_ptr& pre, uci_element* last) : _ptr{pre}
- {
- _ptr.last = last;
- }
-
- inline explicit element() = default;
-
- public:
- inline element(element&&) noexcept = default;
-
- inline element(const element&) = delete;
-
- inline auto operator=(const element&) -> element& = delete;
-
- inline auto operator=(element &&) -> element& = delete;
-
- auto operator[](std::string_view key) const -> T;
-
- [[nodiscard]] inline auto name() const -> std::string
- {
- return _ptr.last->name;
- }
-
- void rename(const char* value) const;
-
- void commit() const;
-
- [[nodiscard]] inline auto begin() const -> iterator<T>
- {
- return iterator<T>{_ptr, _begin};
- }
-
- [[nodiscard]] inline auto end() const -> iterator<T>
- {
- return iterator<T>{_ptr, _end};
- }
-
- inline ~element() = default;
- };
-
- class section;
-
- class option;
-
- class item;
-
- class package : public element<section> {
- public:
- inline package(const uci_ptr& pre, uci_element* last) : element{pre, last}
- {
- // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic,cppcoreguidelines-pro-type-cstyle-cast)
- ptr().p = uci_to_package(ptr().last); // macro casting pointer-offset.
- ptr().package = ptr().last->name;
-
- auto* end = &ptr().p->sections;
- auto* begin = end->next;
- init_begin_end(begin, end);
- }
-
- explicit package(const char* name);
-
- auto set(const char* key, const char* type) const -> section;
- };
-
- class section : public element<option> {
- public:
- inline section(const uci_ptr& pre, uci_element* last) : element{pre, last}
- {
- // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic,cppcoreguidelines-pro-type-cstyle-cast)
- ptr().s = uci_to_section(ptr().last); // macro casting pointer-offset.
- ptr().section = ptr().last->name;
-
- auto* end = &ptr().s->options;
- auto* begin = end->next;
- init_begin_end(begin, end);
- }
-
- auto set(const char* key, const char* value) const -> option;
-
- void del();
-
- [[nodiscard]] inline auto anonymous() const -> bool
- {
- return ptr().s->anonymous;
- }
-
- [[nodiscard]] inline auto type() const -> std::string
- {
- return ptr().s->type;
- }
- };
-
- class option : public element<item> {
- public:
- inline option(const uci_ptr& pre, uci_element* last) : element{pre, last}
- {
- // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic,cppcoreguidelines-pro-type-cstyle-cast)
- ptr().o = uci_to_option(ptr().last); // macro casting pointer-offset.
- ptr().option = ptr().last->name;
-
- if (ptr().o->type == UCI_TYPE_LIST) { // use union ptr().o->v as list:
- // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
- auto* end = &ptr().o->v.list;
- auto* begin = end->next;
- init_begin_end(begin, end);
- }
- else {
- auto* begin = &ptr().last->list;
- auto* end = begin->next;
- init_begin_end(begin, end);
- }
- }
-
- void del();
-
- [[nodiscard]] inline auto type() const -> std::string
- {
- return (ptr().o->type == UCI_TYPE_LIST ? "list" : "option");
- }
- };
-
- class item : public element<item> {
- public:
- inline item(const uci_ptr& pre, uci_element* last) : element{pre, last}
- {
- ptr().value = ptr().last->name;
- }
-
- [[nodiscard]] inline auto type() const -> std::string
- {
- return (ptr().o->type == UCI_TYPE_LIST ? "list" : "option");
- }
-
- [[nodiscard]] inline auto name() const -> std::string
- {
- return (ptr().last->type == UCI_TYPE_ITEM
- ? ptr().last->name
- :
- // else: use union ptr().o->v as string:
- // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
- ptr().o->v.string);
- }
-
- inline explicit operator bool() const
- {
- const auto x = std::string_view{name()};
-
- if (x == "0" || x == "off" || x == "false" || x == "no" || x == "disabled") {
- return false;
- }
-
- if (x == "1" || x == "on" || x == "true" || x == "yes" || x == "enabled") {
- return true;
- }
-
- auto errmsg = std::string{"uci_error: item is not bool "} + name();
- throw std::runtime_error(errmsg);
- }
-
- void rename(const char* value) const;
- };
-
- // ------------------------- implementation: ----------------------------------
-
- std::mutex locked_context::inuse{};
-
- inline auto uci_error(uci_context* ctx, const char* prefix = nullptr) -> std::runtime_error
- {
- char* errmsg = nullptr;
- uci_get_errorstr(ctx, &errmsg, prefix);
-
- std::unique_ptr<char, decltype(&std::free)> auto_free{errmsg, std::free};
- return std::runtime_error{errmsg};
- }
-
- template <class T>
- auto element<T>::operator[](std::string_view key) const -> T
- {
- for (auto elmt : *this) {
- if (elmt.name() == key) {
- return elmt;
- }
- }
-
- auto errmsg = std::string{"uci error: cannot find "}.append(key);
- throw uci_error(locked_context{}.get(), errmsg.c_str());
- }
-
- template <class T>
- void element<T>::rename(const char* value) const
- {
- if (value == name()) {
- return;
- }
-
- auto ctx = locked_context{};
- auto tmp_ptr = uci_ptr{_ptr};
- tmp_ptr.value = value;
- if (uci_rename(ctx.get(), &tmp_ptr) != 0) {
- auto errmsg = std::string{"uci error: cannot rename "}.append(name());
- throw uci_error(ctx.get(), errmsg.c_str());
- }
- }
-
- template <class T>
- void element<T>::commit() const
- {
- auto ctx = locked_context{};
- // TODO(pst) use when possible:
- // if (uci_commit(ctx.get(), &_ptr.p, true) != 0) {
- // auto errmsg = std::string{"uci error: cannot commit "} + _ptr.package;
- // throw uci_error(ctx.get(), errmsg.c_str());
- // }
- auto err = uci_save(ctx.get(), _ptr.p);
- if (err == 0) {
- uci_package* tmp_pkg = nullptr;
- uci_context* tmp_ctx = uci_alloc_context();
- err = (tmp_ctx == nullptr ? 1 : 0);
- if (err == 0) {
- err = uci_load(tmp_ctx, _ptr.package, &tmp_pkg);
- }
- if (err == 0) {
- err = uci_commit(tmp_ctx, &tmp_pkg, false);
- }
- if (err == 0) {
- err = uci_unload(tmp_ctx, tmp_pkg);
- }
- if (tmp_ctx != nullptr) {
- uci_free_context(tmp_ctx);
- }
- }
-
- if (err != 0) {
- auto errmsg = std::string{"uci error: cannot commit "} + _ptr.package;
- throw uci_error(ctx.get(), errmsg.c_str());
- }
- }
-
- package::package(const char* name)
- {
- auto ctx = locked_context{};
-
- auto* pkg = uci_lookup_package(ctx.get(), name);
- if (pkg == nullptr) {
- if (uci_load(ctx.get(), name, &pkg) != 0) {
- auto errmsg = std::string{"uci error: cannot load package "} + name;
- throw uci_error(ctx.get(), errmsg.c_str());
- }
- }
-
- ptr().package = name;
- ptr().p = pkg;
- ptr().last = &pkg->e;
-
- auto* end = &ptr().p->sections;
- auto* begin = end->next;
- init_begin_end(begin, end);
- }
-
- auto package::set(const char* key, const char* type) const -> section
- {
- auto ctx = locked_context{};
-
- auto tmp_ptr = uci_ptr{ptr()};
- tmp_ptr.section = key;
- tmp_ptr.value = type;
- if (uci_set(ctx.get(), &tmp_ptr) != 0) {
- auto errmsg = std::string{"uci error: cannot set section "} + type + "'" + key +
- "' in package " + name();
- throw uci_error(ctx.get(), errmsg.c_str());
- }
-
- return section{ptr(), tmp_ptr.last};
- }
-
- auto section::set(const char* key, const char* value) const -> option
- {
- auto ctx = locked_context{};
-
- auto tmp_ptr = uci_ptr{ptr()};
- tmp_ptr.option = key;
- tmp_ptr.value = value;
- if (uci_set(ctx.get(), &tmp_ptr) != 0) {
- auto errmsg = std::string{"uci error: cannot set option "} + key + "'" + value +
- "' in package " + name();
- throw uci_error(ctx.get(), errmsg.c_str());
- }
-
- return option{ptr(), tmp_ptr.last};
- }
-
- void section::del()
- {
- auto ctx = locked_context{};
- if (uci_delete(ctx.get(), &ptr()) != 0) {
- auto errmsg = std::string{"uci error: cannot delete section "} + name();
- throw uci_error(ctx.get(), errmsg.c_str());
- }
- }
-
- void option::del()
- {
- auto ctx = locked_context{};
- if (uci_delete(ctx.get(), &ptr()) != 0) {
- auto errmsg = std::string{"uci error: cannot delete option "} + name();
- throw uci_error(ctx.get(), errmsg.c_str());
- }
- }
-
- void item::rename(const char* value) const
- {
- if (value == name()) {
- return;
- }
-
- auto ctx = locked_context{};
- auto tmp_ptr = uci_ptr{ptr()};
-
- if (tmp_ptr.last->type != UCI_TYPE_ITEM) {
- tmp_ptr.value = value;
- if (uci_set(ctx.get(), &tmp_ptr) != 0) {
- auto errmsg = std::string{"uci error: cannot rename item "} + name();
- throw uci_error(ctx.get(), errmsg.c_str());
- }
- return;
- } // else:
-
- tmp_ptr.value = tmp_ptr.last->name;
- if (uci_del_list(ctx.get(), &tmp_ptr) != 0) {
- auto errmsg = std::string{"uci error: cannot rename (del) "} + name();
- throw uci_error(ctx.get(), errmsg.c_str());
- }
-
- tmp_ptr.value = value;
- if (uci_add_list(ctx.get(), &tmp_ptr) != 0) {
- auto errmsg = std::string{"uci error: cannot rename (add) "} + value;
- throw uci_error(ctx.get(), errmsg.c_str());
- }
- }
-
- } // namespace uci
-
- #endif
|