samba: user can select devices for sharing

- show share also if a device is not available
 - use folder Freedombox/shares/open_share for sharing on every disk
 - backup and restore share definitions
 - fix: do not hide status block
 - fix: add nmbd to the managed services

Signed-off-by: Veiko Aasa <veiko17@disroot.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Veiko Aasa authored and James Valleroy committed 2019-12-01 11:30:31 -05:00
1 parent eaaa764387
commit 598bcb6fbb
7 files changed
+443 -51

No files matched your search

+43 -6
View File
@@ -18,6 +18,7 @@
FreedomBox app to configure samba.
"""
import json
import socket
from django.urls import reverse_lazy
@@ -34,7 +35,7 @@ from .manifest import backup, clients # noqa, pylint: disable=unused-import
version = 1
managed_services = ['smbd']
managed_services = ['smbd', 'nmbd']
managed_packages = ['samba']
@@ -43,12 +44,13 @@ name = _('Samba')
short_description = _('Samba File Sharing')
description = [
_('Samba file sharing allows to share files between computers in your '
'local network. '),
_('Samba allows to share files and folders between computers in your '
'local network.'),
format_lazy(
_('If enabled, Samba share will be available at \\\\{hostname} on '
'Windows and smb://{hostname} on Linux and Mac'),
hostname=socket.gethostname()),
_('After installation, you can choose which disks to use for sharing. '
'Enabled {hostname} shares are open to everyone in your local '
'network and are accessible under Network section in the file '
'manager.'), hostname=socket.gethostname().upper())
]
clients = clients
@@ -81,6 +83,9 @@ class SambaApp(app_module.App):
daemon = Daemon('daemon-samba', managed_services[0])
self.add(daemon)
daemon_nmbd = Daemon('daemon-samba-nmbd', managed_services[1])
self.add(daemon_nmbd)
def init():
"""Initialize the module."""
@@ -111,3 +116,35 @@ def diagnose():
results.append(action_utils.diagnose_port_listening(445, 'tcp6'))
return results
def add_share(mount_point, filesystem):
"""Add a share."""
command = ['add-share', '--mount-point', mount_point]
if filesystem in ['ntfs', 'vfat']:
command = command + ['--windows-filesystem']
actions.superuser_run('samba', command)
def delete_share(mount_point):
"""Delete a share."""
command = ['delete-share', '--mount-point', mount_point]
actions.superuser_run('samba', command)
def get_shares():
"""Get defined shares."""
output = actions.superuser_run('samba', ['get-shares'])
return json.loads(output)
def backup_pre(packet):
"""Save registry share configuration."""
actions.superuser_run('samba', ['dump-shares'])
def restore_post(packet):
"""Restore configuration."""
actions.superuser_run('samba', ['setup'])
actions.superuser_run('samba', ['restore-config'])
+13 -1
View File
@@ -14,10 +14,22 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Application manifest for Samba.
"""
from plinth.clients import validate
from plinth.modules.backups.api import validate as validate_backup
# A directory where the 'open_share' subdirectory will be created
SHARES_PATH = 'FreedomBox/shares/'
SHARES_CONF_BACKUP_FILE = '/var/lib/plinth/backups-data/samba-shares-dump.conf'
clients = validate([])
backup = validate_backup({})
backup = validate_backup({
'data': {
'files': [SHARES_CONF_BACKUP_FILE]
},
'services': ['smbd', 'nmbd']
})
+7
View File
@@ -0,0 +1,7 @@
const share_checkbox = $(".shareform > input[type='checkbox']");
share_checkbox.change(function(event) {
this.disabled=true;
this.style.cursor='wait';
this.form.submit();
});
+139
View File
@@ -0,0 +1,139 @@
{% extends "app.html" %}
{% comment %}
#
# This file is part of FreedomBox.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
{% endcomment %}
{% load bootstrap %}
{% load i18n %}
{% load static %}
{% block page_head %}
<style type="text/css">
.progress {
margin-bottom: 0;
}
</style>
{% endblock %}
{% block configuration %}
{{ block.super }}
{% if is_enabled %}
<h3>{% trans "Select devices for sharing:" %}</h3>
<p>
{% blocktrans trimmed %}
NB! Selecting device does not share the whole disk, only the folder
FreedomBox/shares/open_share will be shared on that disk.
{% endblocktrans %}
</p>
<table class="table table-bordered table-condensed table-striped">
<thead>
<tr>
<th></th>
<th>{% trans "Device" %}</th>
<th>{% trans "Label" %}</th>
<th>{% trans "Mount Point" %}</th>
<th>{% trans "Type" %}</th>
<th>{% trans "Used" %}</th>
</tr>
</thead>
<tbody>
{% for disk in disks %}
<tr>
<td>
{% if disk.mount_point in shared_mounts %}
<form class="form shareform" method="post"
action="{% url 'samba:unshare' disk.mount_point|urlencode:'' %}">
{% csrf_token %}
<input type="checkbox" value="{{ disk.mount_point }}"
name="mount_point" autocomplete="off" checked/>
</form>
{% else %}
<form class="form shareform" method="post"
action="{% url 'samba:share' disk.mount_point|urlencode:'' %}">
{% csrf_token %}
<input type="hidden" value="{{ disk.filesystem_type }}"
name="filesystem_type">
<input type="checkbox" value="{{ disk.mount_point }}"
name="mount_point" autocomplete="off"/>
</form>
{% endif %}
</td>
<td>{{ disk.device }}</td>
<td>{{ disk.label|default_if_none:"" }}</td>
<td>{{ disk.mount_point }}</td>
<td>{{ disk.filesystem_type }}</td>
<td>
<div class="progress">
{% if disk.percent_used < 75 %}
<div class="progress-bar progress-bar-striped progress-bar-success"
{% elif disk.percent_used < 90 %}
<div class="progress-bar progress-bar-striped progress-bar-warning"
{% else %}
<div class="progress-bar progress-bar-striped progress-bar-danger"
{% endif %}
role="progressbar" aria-valuenow="disk.percent_used"
aria-valuemin="0" aria-valuemax="100"
style="width: {{ disk.percent_used }}%;">
{{ disk.percent_used }}%
</div>
</div>
<div>{{ disk.used_str }} / {{ disk.size_str }}</div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% if unavailable_shares %}
<h3>Shares configured but the disk is not available:</h3>
<p>
{% blocktrans trimmed %}
If the disk is plugged back in, sharing will be automatically enabled.
{% endblocktrans %}
</p>
<table class="table table-bordered table-condensed table-striped">
<thead>
<tr>
<th>{% trans "Share name" %}</th>
<th>{% trans "Action" %}</th>
</tr>
</thead>
<tbody>
{% for share in unavailable_shares %}
<tr>
<td>{{ share.name }}</td>
<td>
<form class="form" method="post"
action="{% url 'samba:unshare' share.mount_point|urlencode:'' %}">
{% csrf_token %}
<input type="submit" class="btn btn-danger" value="Delete">
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
{% endif %}
{% endblock %}
{% block page_js %}
<script type="text/javascript" src="{% static 'samba/samba.js' %}"></script>
{% endblock %}
+8 -9
View File
@@ -15,18 +15,17 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
URLs for the Samba module.
URLs for the samba module.
"""
from django.conf.urls import url
from plinth.modules import samba
from plinth.views import AppView
from . import views
urlpatterns = [
url(
r'^apps/samba/$',
AppView.as_view(app_id='samba', name=samba.name,
diagnostics_module_name='samba',
description=samba.description,
show_status_block=False), name='index')
url(r'^samba/$', views.SambaAppView.as_view(), name='index'),
url(r'^samba/share/(?P<mount_point>[A-Za-z0-9%_.\-~]+)/$', views.share,
name='share'),
url(r'^samba/unshare/(?P<mount_point>[A-Za-z0-9%_.\-~]+)/$', views.unshare,
name='unshare'),
]
+102
View File
@@ -0,0 +1,102 @@
#
# This file is part of FreedomBox.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Views for samba module.
"""
import logging
import urllib.parse
from django.contrib import messages
from django.shortcuts import redirect
from django.urls import reverse
from django.utils.translation import ugettext as _
from django.views.decorators.http import require_POST
from plinth import views
from plinth.modules import samba, storage
logger = logging.getLogger(__name__)
class SambaAppView(views.AppView):
"""Samba sharing basic configuration."""
name = samba.name
description = samba.description
app_id = 'samba'
template_name = 'samba.html'
def get_context_data(self, *args, **kwargs):
"""Return template context data."""
context = super().get_context_data(*args, **kwargs)
disks = storage.get_disks()
context['disks'] = disks
shares = samba.get_shares()
context['shared_mounts'] = [share['mount_point'] for share in shares]
unavailable_shares = []
for share in shares:
for disk in disks:
if share['mount_point'] == disk['mount_point']:
break
else:
unavailable_shares.append(share)
context['unavailable_shares'] = unavailable_shares
return context
@require_POST
def share(request, mount_point):
"""Enable sharing, given its root path.
mount_point is urlquoted.
"""
mount_point = urllib.parse.unquote(mount_point)
filesystem = request.POST.get('filesystem_type', '')
try:
samba.add_share(mount_point, filesystem)
messages.success(request, _('Share enabled.'))
except Exception as exception:
logger.exception('Error enabling share')
messages.error(
request,
_('Error enabling share: {error_message}').format(
error_message=exception))
return redirect(reverse('samba:index'))
@require_POST
def unshare(request, mount_point):
"""Disable sharing, given its name.
mount_point is urlquoted.
"""
mount_point = urllib.parse.unquote(mount_point)
try:
samba.delete_share(mount_point)
messages.success(request, _('Share disabled.'))
except Exception as exception:
logger.exception('Error disabling share')
messages.error(
request,
_('Error disabling share: {error_message}').format(
error_message=exception))
return redirect(reverse('samba:index'))