The apache and nginx webservers have a predefined folders for sites configuration e.g. sites-available and sites-enabled. So you know where to put your custom configuration.
In the Tor current approach is to edit the torrc file but during and update you may lost your changes because they will be overwritten.
This becomes a bigger problem for OpenWrt firmware for routers where during a firmware sysupgrade all files from the /etc/ are cleared unless they clearly specified to be kept. The ClearLinux also use immutable configs design.
Likely the torrc file has a %include directive that can be somehow give the same behavior:
So we can add hidden service configurations as a separate files into the directory /etc/torrc.d/ e.g. /etc/torrc.d/sshd.conf or /etc/torrc.d/nextcloud.conf.
Here I found few problems:
The directive is commented out. But it should work out of the box so user don’t need to change the torrc file.
On Ubuntu/Debian the line is not present at all. And the /etc/torrc.d/ is not created on the tor package install. Maintainers should fix this.
Each service also needs for it’s own folder to keep keys. To make a layout simpler we can put keys in the same folder where the conf file is located. But it may be parsed as a config so instead create a subfolder keys. I tried and made the following structure:
File with the onion service config /etc/torrc.d/sshd/sshd.conf with content:
Your concern is valid, and I would say that in general systems should not break config files during upgrades, but that may be hard to follow on image-based system upgrades like what may be happening with OpenWrt.
I don’t see a way that Tor could standardize it’s default folders in a way that would work seamlessly without changes across all distributions and operating systems. That’s usually a concern for distribution and package maintainers to adapt configurations to their systems.
That said, I recommend the following:
Try to specify your Tor configs to be kept during OpenWrt upgrades.
If that’s not possible, you may try to have a minimalistic /etc/torrc with things like %include /some/persistent/path/etc/tor/*.conf and DataDirectory /some/persistent/path/var/lib/tor, so whenever an upgrade happen you can easily fix the main config file, and keep your full config, keys etc in a persistent folder.
Try to advocate with the distro/package maintainer to ship a torrc with defaults suited for persistency.
The torrc config in OpenWrt is based on /src/config/torrc.sample.in from official source tarball https://dist.torproject.org/tor-0.4.8.9.tar.gz
In there the #%include /etc/torrc.d/*.conf is commented.
So it would be much better if you uncomment the line in the mainline sources. Distro packages will follow this.
Similarly it would be great to add %include /var/run/tor/*.conf directive. This would be useful for some GUI tools that allows to configure the Tor by generating a temp file with directives. They can put their files into the in-memory folder and it will be automatically picked up.
The main issue remains: we need a pre-defined folder for onion services. In the OpenWrt there is a tor-hs package that declares it in /etc/tor/hidden_service. Each service has own folder:
The hidden_service probably should be renamed to onion_services but that’s not a big deal.
What I’m asking for is to make the tor to create the folder /etc/tor/onion_services/ on installation and look for a service.conf file inside. So users can easily copy\backup such folders and they don’t need to change anything in the torrc.
A NextCloud package can just put own config inside of the folder and it will start working.
As an additional improvement it would be great to have some hook to trigger when Tor creates an onion service and generates keys. The hook will update the NextCloud config and configure it for the new generated onion domain.