Skip to content

Commit

Permalink
refactor allowed paths
Browse files Browse the repository at this point in the history
Signed-off-by: szaimen <szaimen@e.mail.de>
  • Loading branch information
szaimen committed May 9, 2022
1 parent b0d83d4 commit d669a00
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 52 deletions.
25 changes: 6 additions & 19 deletions Containers/mastercontainer/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,34 +73,21 @@ fi

# Check for other options
if [ -n "$NEXTCLOUD_DATADIR" ]; then
if ! echo "$NEXTCLOUD_DATADIR" | grep -q "^/mnt/" \
&& ! echo "$NEXTCLOUD_DATADIR" | grep -q "^/media/" \
&& ! echo "$NEXTCLOUD_DATADIR" | grep -q "^/volume[0-9]/" \
&& ! echo "$NEXTCLOUD_DATADIR" | grep -q "^/host_mnt/"
then
if ! echo "$NEXTCLOUD_DATADIR" | grep -q "^/" || [ "$NEXTCLOUD_DATADIR" = "/" ]; then
echo "You've set NEXTCLOUD_DATADIR but not to an allowed value.
The string must start with '/mnt/', '/media/', '/volume[0-9]/' or '/host_mnt/'. E.g. '/mnt/ncdata'.
The string must start with '/' and must not be equal to '/'.
It is set to '$NEXTCLOUD_DATADIR'."
exit 1
elif [ "$NEXTCLOUD_DATADIR" = "/mnt/" ] || [ "$NEXTCLOUD_DATADIR" = "/media/" ] || [ "$NEXTCLOUD_DATADIR" = "/volume[0-9]/" ] || [ "$NEXTCLOUD_DATADIR" = "/host_mnt/" ]; then
echo "You've set NEXTCLOUD_DATADIR but not to an allowed value.
The string must start with '/mnt/', '/media/', '/volume[0-9]/' or '/host_mnt/' and not be equal to these."
exit 1
fi
fi
if [ -n "$NEXTCLOUD_MOUNT" ]; then
if ! echo "$NEXTCLOUD_MOUNT" | grep -q "^/mnt/" \
&& ! echo "$NEXTCLOUD_MOUNT" | grep -q "^/media/" \
&& ! echo "$NEXTCLOUD_MOUNT" | grep -q "^/volume[0-9]/" \
&& ! echo "$NEXTCLOUD_MOUNT" | grep -q "^/host_mnt/" \
&& ! echo "$NEXTCLOUD_MOUNT" | grep -q "^/var/backups$"
then
if ! echo "$NEXTCLOUD_MOUNT" | grep -q "^/" || [ "$NEXTCLOUD_MOUNT" = "/" ]; then
echo "You've set NEXCLOUD_MOUNT but not to an allowed value.
The string must be equal to/start with '/mnt/', '/media/', '/volume[0-9]/' or '/host_mnt/' or be equal to '/var/backups'.
It is set to '$NEXTCLOUD_DATADIR'."
The string must start with '/' and must not be equal to '/'.
It is set to '$NEXTCLOUD_MOUNT'."
exit 1
elif [ "$NEXTCLOUD_MOUNT" = "/mnt/ncdata" ] || echo "$NEXTCLOUD_MOUNT" | grep -q "^/mnt/ncdata/"; then
echo "'/mnt/ncdata' and '/mnt/ncdata/' are not allowed for NEXTCLOUD_MOUNT."
echo "'/mnt/ncdata' and '/mnt/ncdata/' are not allowed as values for NEXTCLOUD_MOUNT."
exit 1
fi
fi
Expand Down
25 changes: 6 additions & 19 deletions php/src/Data/ConfigurationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,26 +282,13 @@ public function GetAIOURL() : string {
* @throws InvalidSettingConfigurationException
*/
public function SetBorgBackupHostLocation(string $location) : void {
$allowedPrefixes = [
'/mnt/',
'/media/',
'/host_mnt/',
];

$isValidPath = false;
foreach($allowedPrefixes as $allowedPrefix) {
if(str_starts_with($location, $allowedPrefix) && !str_ends_with($location, '/')) {
$isValidPath = true;
break;
}
if ($location === '/var/backups') {
$isValidPath = true;
break;
}
if (str_starts_with($location, '/') && !str_ends_with($location, '/')) {
$isValidPath = true;
}

if(!$isValidPath) {
throw new InvalidSettingConfigurationException("The path must start with '/mnt/', '/media/' or '/host_mnt/' or be equal to '/var/backups'.");
if (!$isValidPath) {
throw new InvalidSettingConfigurationException("The path must start with '/', and must not end with '/'!");
}


Expand All @@ -323,8 +310,8 @@ public function SetBorgRestoreHostLocationAndPassword(string $location, string $
$isValidPath = true;
}

if(!$isValidPath) {
throw new InvalidSettingConfigurationException("The path may start with '/mnt/', '/media/' or '/host_mnt/' or may be equal to '/var/backups'.");
if (!$isValidPath) {
throw new InvalidSettingConfigurationException("The path must start with '/', and must not end with '/'!");
}

if ($password === '') {
Expand Down
14 changes: 8 additions & 6 deletions php/templates/containers.twig
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,10 @@
<input type="hidden" name="{{csrf.keys.value}}" value="{{csrf.value}}">
<input class="button" type="submit" value="Submit" />
</form>
The folder path that you enter may start with <b>/mnt/</b>, <b>/media/</b> or <b>/host_mnt/</b> or may be equal to <b>/var/backups</b>.<br><br>
An example for Linux and macOS is <b>/mnt/backup</b>.<br>
On Windows it might be <b>/host_mnt/c/backup/directory</b>. (This Windows example would be equivalent to 'C:\backup\directory' on the Windows host. So you need to translate the path that you want to use into the correct format.)<br><br>
The folder path that you enter must start with <b>/</b> and must <b>not</b> end with <b>/</b>.<br><br>
An example for Linux is <b>/mnt/backup</b>.<br>
For macOS it may be <b>/var/backup</b>.<br>
On Windows it might be <b>/host_mnt/c/backup</b>. (This Windows example would be equivalent to 'C:\backup' on the Windows host. So you need to translate the path that you want to use into the correct format.)<br><br>
⚠ Note that the backup archive must be located in a subfolder of the folder that you enter here and the subfolder which contains the archive must be named 'borg'. Otherwise will the backup container not find the backup archive!<br><br>
{% endif %}
{% else %}
Expand Down Expand Up @@ -293,9 +294,10 @@
<input type="hidden" name="{{csrf.keys.value}}" value="{{csrf.value}}">
<input class="button" type="submit" value="Submit" />
</form>
The folder path that you enter may start with <b>/mnt/</b>, <b>/media/</b> or <b>/host_mnt/</b> or may be equal to <b>/var/backups</b>.<br><br>
An example for Linux and macOS is <b>/mnt/backup</b>.<br>
On Windows it might be <b>/host_mnt/c/backup/directory</b>. (This Windows example would be equivalent to 'C:\backup\directory' on the Windows host. So you need to translate the path that you want to use into the correct format.)<br><br>
The folder path that you enter must start with <b>/</b> and must <b>not</b> end with <b>/</b>.<br><br>
An example for Linux is <b>/mnt/backup</b>.<br>
For macOS it may be <b>/var/backup</b>.<br>
On Windows it might be <b>/host_mnt/c/backup</b>. (This Windows example would be equivalent to 'C:\backup' on the Windows host. So you need to translate the path that you want to use into the correct format.)<br><br>
{% endif %}

{% if borg_backup_host_location != "" %}
Expand Down
16 changes: 8 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,30 +286,30 @@ Afterwards apply the correct permissions with `sudo chown root:root /root/backup
**Attention:** Make sure that the execution of the script does not collidate with the daily backups from AIO (if configured) since the target backup repository might get into an inconsistent state. (There is no check in place that checks this.)

### How to change the default location of Nextcloud's Datadir?
You can configure the Nextcloud container to use a specific directory on your host as data directory. You can do so by adding the environmental variable `NEXTCLOUD_DATADIR` to the initial startup of the mastercontainer. Allowed values for that variable are strings that start with `/mnt/`, `/media/`, `/volume[0-9]/` or `/host_mnt/`.
You can configure the Nextcloud container to use a specific directory on your host as data directory. You can do so by adding the environmental variable `NEXTCLOUD_DATADIR` to the initial startup of the mastercontainer. Allowed values for that variable are strings that start with `/` and are not equal to `/`.

(`/volume[0-9]/` means that all strings from `/volume1/` to `/volume9/` are allowed.)

- An example for Linux and macOS is `-e NEXTCLOUD_DATADIR="/mnt/ncdata"`.
- An example for Linux is `-e NEXTCLOUD_DATADIR="/mnt/ncdata"`.
- On macOS it might be `-e NEXTCLOUD_DATADIR="/var/nextcloud-data"`
- For Synology it may be `/volume1/docker/nextcloud/data`.
- On Windows it might be `-e NEXTCLOUD_DATADIR="/host_mnt/c/your/data/path"` (This Windows example would be equivalent to `C:\your\data\path` on the Windows host. So you need to translate the path that you want to use into the correct format.)

⚠ Please make sure to apply the correct permissions to the chosen directory before starting Nextcloud the first time (not needed on Windows).

- In this example for macOS and Linux, the command for this be: `sudo chown -R 33:0 /mnt/ncdata`.
- In this example for Linux, the command for this would be `sudo chown -R 33:0 /mnt/ncdata`.
- On macOS, the command for this would be `sudo chown -R 33:0 /var/nextcloud-data`.
- For Synology, the command for this example would be `sudo chown -R 33:0 /volume1/docker/nextcloud/data`
- On Windows, this command is not needed.

**Attention:** It is very important to change the datadir **before** Nextcloud is installed/started the first time and not to change it afterwards!

### How to allow the Nextcloud container to access directories on the host?
By default, the Nextcloud container is confined and cannot access directories on the host OS. You might want to change this when you are planning to use local external storage in Nextcloud to store some files outside the data directory and can do so by adding the environmental variable `NEXTCLOUD_MOUNT` to the initial startup of the mastercontainer. Allowed values for that variable are strings that are equal to or start with `/mnt/`, `/media/`, `/volume[0-9]/` or `/host_mnt/` or are equal to `/var/backups` and unequal to `/mnt/ncdata`.
By default, the Nextcloud container is confined and cannot access directories on the host OS. You might want to change this when you are planning to use local external storage in Nextcloud to store some files outside the data directory and can do so by adding the environmental variable `NEXTCLOUD_MOUNT` to the initial startup of the mastercontainer. Allowed values for that variable are strings that start with `/` and are not equal to `/`.

- Two examples for Linux and macOS are: `-e NEXTCLOUD_MOUNT="/mnt/"` and `-e NEXTCLOUD_MOUNT="/media/"`.
- Two examples for Linux are `-e NEXTCLOUD_MOUNT="/mnt/"` and `-e NEXTCLOUD_MOUNT="/media/"`.
- For Synology it may be `/volume1/`.
- On Windows it might be `-e NEXTCLOUD_MOUNT="/host_mnt/c"` (This Windows example would be equivalent to `C:\` on the Windows host. So you need to translate the path that you want to use into the correct format.)

After using this option, please make sure to apply the correct permissions to the directories that you want to use in Nextcloud (not needed on Windows). E.g. `sudo chown -R 33:0 /mnt/your-drive-mountpoint` should make it work on Linux and macOS when you have used `-e NEXTCLOUD_MOUNT="/mnt/"`.
After using this option, please make sure to apply the correct permissions to the directories that you want to use in Nextcloud (not needed on Windows). E.g. `sudo chown -R 33:0 /mnt/your-drive-mountpoint` should make it work on Linux when you have used `-e NEXTCLOUD_MOUNT="/mnt/"`.

You can then navigate to the apps management page, activate the external storage app, navigate to `https://your-nc-domain.com/settings/admin/externalstorages` and add a local external storage directory that will be accessible inside the container at the same place that you've entered. E.g. `/mnt/your-drive-mountpoint` will be mounted to `/mnt/your-drive-mountpoint` inside the container, etc.

Expand Down

0 comments on commit d669a00

Please sign in to comment.