Ejemplo n.º 1
0
    def post(self, request, command):
        """
        execute a command on the service
        """
        service_name = 'smb'
        service = Service.objects.get(name=service_name)
        if (command == 'config'):
            aso = Service.objects.get(name='active-directory')
            if (aso.config is not None):
                e_msg = ('Active Directory service is configured, so '
                         'Workgroup is automatically retrieved and cannot '
                         'be set manually')
                handle_exception(Exception(e_msg), request)

            try:
                config = request.data.get('config', {
                    'workgroup': 'MYGROUP',
                })
                workgroup = config['workgroup']
                self._save_config(service, config)
                update_global_config(workgroup)
                restart_samba(hard=True)
            except Exception, e:
                e_msg = ('Samba could not be configured. Try again. '
                         'Exception: %s' % e.__str__())
                handle_exception(Exception(e_msg), request)
Ejemplo n.º 2
0
    def post(self, request, command):
        """
        execute a command on the service
        """
        service = Service.objects.get(name=self.service_name)
        if (command == 'config'):
            aso = Service.objects.get(name='active-directory')
            if (aso.config is not None):
                e_msg = ('Active Directory service is configured, so '
                         'Workgroup is automatically retrieved and cannot '
                         'be set manually')
                handle_exception(Exception(e_msg), request)

            try:
                config = request.data.get('config', {})
                global_config = {}
                gc_lines = config['global_config'].split('\n')
                for l in gc_lines:
                    gc_param = l.strip().split('=')
                    if (len(gc_param) == 2):
                        global_config[gc_param[0].strip().lower()] = gc_param[1].strip()
                if ('workgroup' not in global_config):
                    global_config['workgroup'] = config['workgroup']
                self._save_config(service, global_config)
                adso = Service.objects.get(name='active-directory')
                adconfig = {}
                if (adso.config is not None):
                    adconfig = self._get_config(adso)
                update_global_config(global_config, adconfig)
                restart_samba(hard=True)
            except Exception, e:
                e_msg = ('Samba could not be configured. Try again. '
                         'Exception: %s' % e.__str__())
                handle_exception(Exception(e_msg), request)
Ejemplo n.º 3
0
    def post(self, request, sname):
        with self._handle_exception(request):
            share = Share.objects.get(name=sname)
            try:
                samba_o = SambaShare.objects.get(share=share)
                samba_serializer = SambaShareSerializer(samba_o)
                return Response(samba_serializer.data)
            except:
                options = {
                    'comment': ('samba for %s' % sname),
                    'browsable': 'yes',
                    'guest_ok': 'no',
                    'read_only': 'no',
                    'create_mask': '0755',
                    }
                if ('comment' in request.DATA):
                    options['comment'] = request.DATA['comment']
                if ('browsable' in request.DATA):
                    if (request.DATA['browsable'] != 'yes' and
                        request.DATA['browsable'] != 'no'):
                        e_msg = ('Invalid choice for browsable. Possible '
                                 'choices are yes or no.')
                        handle_exception(Exception(e_msg), request)
                    options['browsable'] = request.DATA['browsable']
                if ('guest_ok' in request.DATA):
                    if (request.DATA['guest_ok'] != 'yes' and
                        request.DATA['guest_ok'] != 'no'):
                        e_msg = ('Invalid choice for guest_ok. Possible '
                                 'options are yes or no.')
                        handle_exception(Exception(e_msg), request)
                    options['guest_ok'] = request.DATA['guest_ok']
                if ('read_only' in request.DATA):
                    if (request.DATA['read_only'] != 'yes' and
                        request.DATA['read_only'] != 'no'):
                        e_msg = ('Invalid choice for read_only. Possible '
                                 'options are yes or no.')
                        handle_exception(Exception(e_msg), request)
                    options['read_only'] = request.DATA['read_only']
                if ('create_mask' in request.DATA):
                    if (request.DATA['create_mask'] not in self.CREATE_MASKS):
                        e_msg = ('Invalid choice for create_mask. Possible '
                                 'options are: %s' % self.CREATE_MASKS)
                        handle_exception(Exception(e_msg), request)

            mnt_pt = ('%s%s' % (settings.MNT_PT, share.name))
            smb_share = SambaShare(share=share, path=mnt_pt,
                                   comment=options['comment'],
                                   browsable=options['browsable'],
                                   read_only=options['read_only'],
                                   guest_ok=options['guest_ok'],
                                   create_mask=options['create_mask'])
            smb_share.save()
            if (not is_share_mounted(share.name)):
                pool_device = Disk.objects.filter(pool=share.pool)[0].name
                mount_share(share.subvol_name, pool_device, mnt_pt)
            refresh_smb_config(list(SambaShare.objects.all()))
            restart_samba()
            samba_serializer = SambaShareSerializer(smb_share)
            return Response(samba_serializer.data)
Ejemplo n.º 4
0
 def delete(self, request, sname):
     with self._handle_exception(request):
         share = Share.objects.get(name=sname)
         if (not SambaShare.objects.filter(share=share).exists()):
             e_msg = ('Share is not exported via Samba. Nothing to delete')
             handle_exception(Exception(e_msg), request)
         samba_share = SambaShare.objects.get(share=share)
         samba_share.delete()
         refresh_smb_config(list(SambaShare.objects.all()))
         restart_samba()
         return Response()
Ejemplo n.º 5
0
    def post(self, request, command):
        """
        execute a command on the service
        """
        service = Service.objects.get(name=self.service_name)

        if (command == 'config'):
            try:
                config = request.data.get('config', {})
                global_config = {}
                gc_lines = config['global_config'].split('\n')
                for l in gc_lines:
                    gc_param = l.strip().split(' = ')
                    if (len(gc_param) == 2):
                        if '=' in gc_param[0]:
                            raise Exception(
                                'Syntax error, one param has wrong spaces around equal signs, '
                                'please check syntax of \'%s\'' %
                                ''.join(gc_param))
                        global_config[
                            gc_param[0].strip().lower()] = gc_param[1].strip()
                #Default set current workgroup to one got via samba config page
                global_config['workgroup'] = config['workgroup']
                #Check Active Directory config and status
                #if AD configured and ON set workgroup to AD retrieved workgroup
                #else AD not running and leave workgroup to one choosen by user
                adso = Service.objects.get(name='active-directory')
                adconfig = None
                adso_status = 1
                if (adso.config is not None):
                    adconfig = self._get_config(adso)
                    adso_out, adso_err, adso_status = service_status(
                        'active-directory', adconfig)
                    if adso_status == 0:
                        global_config['workgroup'] = adconfig['workgroup']
                    else:
                        adconfig = None

                self._save_config(service, global_config)
                update_global_config(global_config, adconfig)
                restart_samba(hard=True)
            except Exception, e:
                e_msg = ('Samba could not be configured. Try again. '
                         'Exception: %s' % e.__str__())
                handle_exception(Exception(e_msg), request)
Ejemplo n.º 6
0
 def post(self, request, command):
     """
     execute a command on the service
     """
     service_name = 'smb'
     service = Service.objects.get(name=service_name)
     if (command == 'config'):
         #nothing to really configure atm. just save the model
         try:
             config = request.data.get('config', {'workgroup': 'MYGROUP',})
             workgroup = config['workgroup']
             self._save_config(service, config)
             update_global_config(workgroup)
             restart_samba(hard=True)
         except Exception, e:
             e_msg = ('Samba could not be configured. Try again. '
                      'Exception: %s' % e.__str__())
             handle_exception(Exception(e_msg), request)
Ejemplo n.º 7
0
    def post(self, request, command):
        """
        execute a command on the service
        """
        service = Service.objects.get(name=self.service_name)

        if (command == 'config'):
            try:
                config = request.data.get('config', {})
                global_config = {}
                gc_lines = config['global_config'].split('\n')
                for l in gc_lines:
                    gc_param = l.strip().split(' = ')
                    if (len(gc_param) == 2):
                        if '=' in gc_param[0]:
                            raise Exception('Syntax error, one param has wrong spaces around equal signs, '
                                            'please check syntax of \'%s\'' % ''.join(gc_param))
                        global_config[gc_param[0].strip().lower()] = gc_param[1].strip()
                #Default set current workgroup to one got via samba config page
                global_config['workgroup'] = config['workgroup']
                #Check Active Directory config and status
                #if AD configured and ON set workgroup to AD retrieved workgroup
                #else AD not running and leave workgroup to one choosen by user
                adso = Service.objects.get(name='active-directory')
                adconfig = None
                adso_status = 1
                if (adso.config is not None):
                    adconfig = self._get_config(adso)
                    adso_out, adso_err, adso_status = service_status('active-directory', adconfig)
                    if adso_status == 0:
                        global_config['workgroup'] = adconfig['workgroup']
                    else:
                        adconfig = None

                self._save_config(service, global_config)
                update_global_config(global_config, adconfig)
                restart_samba(hard=True)
            except Exception, e:
                e_msg = ('Samba could not be configured. Try again. '
                         'Exception: %s' % e.__str__())
                handle_exception(Exception(e_msg), request)
Ejemplo n.º 8
0
    def post(self, request, command):
        """
        execute a command on the service
        """
        service_name = 'smb'
        service = Service.objects.get(name=service_name)
        if (command == 'config'):
            aso = Service.objects.get(name='active-directory')
            if (aso.config is not None):
                e_msg = ('Active Directory service is configured, so '
                         'Workgroup is automatically retrieved and cannot '
                         'be set manually')
                handle_exception(Exception(e_msg), request)

            try:
                config = request.data.get('config', {'workgroup': 'MYGROUP',})
                workgroup = config['workgroup']
                self._save_config(service, config)
                update_global_config(workgroup)
                restart_samba(hard=True)
            except Exception, e:
                e_msg = ('Samba could not be configured. Try again. '
                         'Exception: %s' % e.__str__())
                handle_exception(Exception(e_msg), request)
Ejemplo n.º 9
0
 def _restart_samba():
     out = status()
     if (out[2] == 0):
         restart_samba(hard=True)
Ejemplo n.º 10
0
    def post(self, request, command):
        """
        execute a command on the service
        """
        service = Service.objects.get(name=self.service_name)

        if (command == 'config'):
            try:
                config = request.data.get('config', {})
                global_config = {}
                if 'global_config' in config:
                    gc_lines = config['global_config'].split('\n')
                    for l in gc_lines:
                        gc_param = l.strip().split(' = ')
                        if (len(gc_param) == 2):
                            if '=' in gc_param[0]:
                                raise Exception(
                                    'Syntax error, one param has wrong '
                                    'spaces around equal signs, '
                                    'please check syntax of '
                                    '\'%s\'' % ''.join(gc_param))
                            global_config[gc_param[0].strip().lower(
                            )] = gc_param[1].strip()  # noqa
                    # #E501 Default set current workgroup to one got via samba
                    # config page
                    global_config['workgroup'] = config['workgroup']
                else:
                    global_config = config
                # Check Active Directory config and status if AD configured and
                # ON set workgroup to AD retrieved workgroup else AD not
                # running and leave workgroup to one choosen by user
                adso = Service.objects.get(name='active-directory')
                adconfig = None
                adso_status = 1
                if (adso.config is not None):
                    adconfig = self._get_config(adso)
                    adso_out, adso_err, adso_status = service_status(
                        'active-directory', adconfig)
                    if adso_status == 0:
                        global_config['workgroup'] = adconfig['workgroup']
                    else:
                        adconfig = None

                self._save_config(service, global_config)
                update_global_config(global_config, adconfig)
                restart_samba(hard=True)
            except Exception as e:
                e_msg = ('Samba could not be configured. Try again. '
                         'Exception: %s' % e.__str__())
                handle_exception(Exception(e_msg), request)
        else:
            try:
                if (command == 'stop'):
                    systemctl('smb', 'disable')
                    systemctl('nmb', 'disable')
                else:
                    systemd_name = '%s.service' % self.service_name
                    ss_dest = ('/etc/systemd/system/%s' % systemd_name)
                    ss_src = ('%s/%s' % (settings.CONFROOT, systemd_name))
                    sum1 = md5sum(ss_dest)
                    sum2 = md5sum(ss_src)
                    if (sum1 != sum2):
                        shutil.copy(ss_src, ss_dest)
                    systemctl('smb', 'enable')
                    systemctl('nmb', 'enable')
                systemctl('nmb', command)
                systemctl('smb', command)
            except Exception as e:
                e_msg = ('Failed to %s samba due to a system error: %s' %
                         (command, e.__str__()))
                handle_exception(Exception(e_msg), request)
        return Response()
Ejemplo n.º 11
0
    def post(self, request, command):
        """
        execute a command on the service
        """
        service = Service.objects.get(name=self.service_name)

        if (command == 'config'):
            try:
                config = request.data.get('config', {})
                global_config = {}
                if 'global_config' in config:
                    gc_lines = config['global_config'].split('\n')
                    for l in gc_lines:
                        gc_param = l.strip().split(' = ')
                        if (len(gc_param) == 2):
                            if '=' in gc_param[0]:
                                raise Exception(
                                    'Syntax error, one param has wrong '
                                    'spaces around equal signs, '
                                    'please check syntax of '
                                    '\'%s\'' % ''.join(gc_param))
                            global_config[gc_param[0].strip().lower()] = gc_param[1].strip()  # noqa
                    # #E501 Default set current workgroup to one got via samba
                    # config page
                    global_config['workgroup'] = config['workgroup']
                else:
                    global_config = config
                # Check Active Directory config and status if AD configured and
                # ON set workgroup to AD retrieved workgroup else AD not
                # running and leave workgroup to one choosen by user
                adso = Service.objects.get(name='active-directory')
                adconfig = None
                adso_status = 1
                if (adso.config is not None):
                    adconfig = self._get_config(adso)
                    adso_out, adso_err, adso_status = service_status(
                        'active-directory', adconfig)
                    if adso_status == 0:
                        global_config['workgroup'] = adconfig['workgroup']
                    else:
                        adconfig = None

                self._save_config(service, global_config)
                update_global_config(global_config, adconfig)
                restart_samba(hard=True)
            except Exception as e:
                e_msg = ('Samba could not be configured. Try again. '
                         'Exception: %s' % e.__str__())
                handle_exception(Exception(e_msg), request)
        else:
            try:
                if (command == 'stop'):
                    systemctl('smb', 'disable')
                    systemctl('nmb', 'disable')
                else:
                    systemd_name = '%s.service' % self.service_name
                    ss_dest = ('/etc/systemd/system/%s' % systemd_name)
                    ss_src = ('%s/%s' % (settings.CONFROOT, systemd_name))
                    sum1 = md5sum(ss_dest)
                    sum2 = md5sum(ss_src)
                    if (sum1 != sum2):
                        shutil.copy(ss_src, ss_dest)
                    systemctl('smb', 'enable')
                    systemctl('nmb', 'enable')
                systemctl('nmb', command)
                systemctl('smb', command)
            except Exception as e:
                e_msg = ('Failed to %s samba due to a system error: %s'
                         % (command, e.__str__()))
                handle_exception(Exception(e_msg), request)
        return Response()
Ejemplo n.º 12
0
    def post(self, request, command):
        """
        execute a command on the service
        """
        service = Service.objects.get(name=self.service_name)

        if command == "config":
            try:
                config = request.data.get("config", {})
                global_config = {}
                if "global_config" in config:
                    gc_lines = config["global_config"].split("\n")
                    for l in gc_lines:
                        gc_param = l.strip().split(" = ")
                        if len(gc_param) == 2:
                            if "=" in gc_param[0]:
                                raise Exception(
                                    "Syntax error, one param has wrong "
                                    "spaces around equal signs, "
                                    "please check syntax of "
                                    "'%s'" % "".join(gc_param))
                            global_config[gc_param[0].strip().lower(
                            )] = gc_param[1].strip()  # noqa
                    # #E501 Default set current workgroup to one got via samba
                    # config page
                    global_config["workgroup"] = config["workgroup"]
                else:
                    global_config = config
                # Check Active Directory config and status if AD configured and
                # ON set workgroup to AD retrieved workgroup else AD not
                # running and leave workgroup to one choosen by user
                adso = Service.objects.get(name="active-directory")
                adconfig = None
                adso_status = 1
                if adso.config is not None:
                    adconfig = self._get_config(adso)
                    adso_out, adso_err, adso_status = service_status(
                        "active-directory", adconfig)
                    if adso_status == 0:
                        global_config["workgroup"] = adconfig["workgroup"]
                    else:
                        adconfig = None

                self._save_config(service, global_config)
                update_global_config(global_config, adconfig)
                _, _, smb_rc = service_status(self.service_name)
                # Restart samba only if already ON
                # rc == 0 if service is ON, rc != 0 otherwise
                if smb_rc == 0:
                    restart_samba(hard=True)
            except Exception as e:
                e_msg = ("Samba could not be configured. Try again. "
                         "Exception: %s" % e.__str__())
                handle_exception(Exception(e_msg), request)
        else:
            try:
                if command == "stop":
                    systemctl("smb", "disable")
                    systemctl("nmb", "disable")
                else:
                    systemd_name = "{}.service".format(self.service_name)
                    distro_id = settings.OS_DISTRO_ID
                    self._write_smb_service(systemd_name, distro_id)
                    systemctl("smb", "enable")
                    systemctl("nmb", "enable")
                systemctl("nmb", command)
                systemctl("smb", command)
            except Exception as e:
                e_msg = "Failed to {} samba due to a system error: {}".format(
                    command, e.__str__())
                handle_exception(Exception(e_msg), request)
        return Response()
Ejemplo n.º 13
0
 def _restart_samba():
     out = status()
     if (out[2] == 0):
         restart_samba()