Example #1
0
def add_to_db(dir, action, file=None):
    try:
        if file:
            path = '%s/%s' % (dir, file)
        else:
            path = dir

        path = os.path.normpath(path)

        tz = pytz.timezone('UTC')
        now_utc = datetime.datetime.now(tz)

        time = now_utc.strftime('%Y-%m-%d %H:%M:%S')

        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        db_path = '%s/db/inotify.db' % config_dir
        ret, err = db.execute_iud(db_path, [[
            "insert into logs (path, actions, timestamp) values (?, ?, ?)",
            (
                path,
                action,
                time,
            )
        ]])
        if err:
            print err

    except Exception, e:
        return False, 'Error inserting into database dir - %s, action - %s, file = %s : %s' % (
            dir, action, file, str(e))
Example #2
0
def generate_smb_conf():
    try:
        d, err = get_auth_settings()
        if err:
            raise Exception(err)
        smb_conf_path, err = config.get_smb_conf_path()
        if err:
            raise Exception(err)
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        # For customer specific or non configurable smb.conf lines
        extra_share_param_lines = None
        extra_global_param_lines = None
        if os.path.isfile('%s/customer_specific/extra_smb_share_params.conf' %
                          config_dir):
            with open(
                    '%s/customer_specific/extra_smb_share_params.conf' %
                    config_dir, 'r') as f1:
                extra_share_param_lines = f1.readlines()
        if os.path.isfile('%s/customer_specific/extra_smb_global_params.conf' %
                          config_dir):
            with open(
                    '%s/customer_specific/extra_smb_global_params.conf' %
                    config_dir, 'r') as f1:
                extra_global_param_lines = f1.readlines()
        # print extra_share_param_lines
        # print extra_global_param_lines
        with open("%s/smb.conf" % smb_conf_path, "w+") as f:
            ret, err = generate_global_header(f)
            if err:
                raise Exception(err)
            ret, err = generate_global_section(f, d, extra_global_param_lines)
            if err:
                raise Exception(err)
            shl, err = get_shares_list()
            if err:
                raise Exception(err)
            if shl:
                for share in shl:
                    ret, err = generate_share_section(
                        f, share["name"], d["workgroup"], share["path"],
                        share["read_only"], share["browseable"],
                        share["comment"], d["security"],
                        extra_share_param_lines, share['hosts_allow'],
                        share['hosts_deny'])
                    if err:
                        raise Exception(err)
            # Generate share section for /var/log
            ret, err = generate_share_section(
                f, 'system_logs', '', '/var/log', True, False,
                'Integralstor system logs share', '')
            if err:
                raise Exception(err)
        ret, errors = reload_configuration(action='reload')
        if errors:
            raise Exception(errors)
    except Exception, e:
        return False, 'Error generating CIFS configuration : %s' % str(e)
def add_to_db(dir, action, file=None):
    try:
        if file:
            path = '%s/%s' % (dir, file)
        else:
            path = dir

        path = os.path.normpath(path)

        tz = pytz.timezone('UTC')
        now_utc = datetime.datetime.now(tz)

        time = now_utc.strftime('%Y-%m-%d %H:%M:%S')

        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        db_path = '%s/db/inotify.db' % config_dir
        ret, err = db.execute_iud(db_path, [
                                  ["insert into logs (path, actions, timestamp) values (?, ?, ?)", (path, action, time,)]])
        if err:
            print err

    except Exception, e:
        return False, 'Error inserting into database dir - %s, action - %s, file = %s : %s' % (dir, action, file, str(e))
def download_sys_info(request):
    return_dict = {}
    try:
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)
        zf_name = "system_info.zip"
        try:
            out = io.BytesIO()
            zf = zipfile.ZipFile(out, 'w')
            abs_src = os.path.abspath(config_dir)
            lu, err = local_users.get_local_users()
            if err:
                raise Exception(err)
            lg, err = local_users.get_local_groups()
            if err:
                raise Exception(err)
            with open('/tmp/local_users_tmp', 'w') as fd:
                json.dump(lu, fd, indent=2)
            with open('/tmp/local_groups_tmp', 'w') as fd:
                json.dump(lg, fd, indent=2)
            zf.write('/tmp/local_users_tmp', 'local_users')
            zf.write('/tmp/local_groups_tmp', 'local_groups')
            for conf_subdir in upload_download_conf_dirs:
                for dirname, subdirs, files in os.walk(
                        '%s/%s' % (config_dir, conf_subdir)):
                    for filename in files:
                        absname = os.path.abspath(
                            os.path.join(dirname, filename))
                        arcname = absname[len(abs_src) + 1:]
                        zf.write(absname, arcname)
            for key, value in upload_download_logs.iteritems():
                if os.path.isfile(value):
                    zf.write(value, key)
            zf.close()
            audit_str = 'Downloaded system configuration.'
            audit.audit('download_configuration', audit_str, request)
        except Exception as e:
            raise Exception("Error compressing remote log file : %s" % str(e))
        response = django.http.HttpResponse(
            out.getvalue(), content_type='application/x-compressed')
        response['Content-disposition'] = 'attachment; filename=%s' % (zf_name)
        return response
    except Exception as e:
        return_dict["base_template"] = 'system_base.html'
        return_dict['tab'] = 'system_info_tab'
        return_dict["page_title"] = 'Download system configuration'
        return_dict["error"] = 'Error downloading system configuration'
        return_dict["error_details"] = str(e)
        return django.shortcuts.render_to_response(
            "logged_in_error.html",
            return_dict,
            context_instance=django.template.context.RequestContext(request))
def download_sys_info(request):
    return_dict = {}
    try:
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)
        zf_name = "system_info.zip"
        try:
            out = io.BytesIO()
            zf = zipfile.ZipFile(out, 'w')
            abs_src = os.path.abspath(config_dir)
            lu, err = local_users.get_local_users()
            if err:
                raise Exception(err)
            lg, err = local_users.get_local_groups()
            if err:
                raise Exception(err)
            with open('/tmp/local_users_tmp', 'w') as fd:
                json.dump(lu, fd, indent=2)
            with open('/tmp/local_groups_tmp', 'w') as fd:
                json.dump(lg, fd, indent=2)
            zf.write('/tmp/local_users_tmp', 'local_users')
            zf.write('/tmp/local_groups_tmp', 'local_groups')
            for conf_subdir in upload_download_conf_dirs:
                for dirname, subdirs, files in os.walk('%s/%s' % (config_dir, conf_subdir)):
                    for filename in files:
                        absname = os.path.abspath(
                            os.path.join(dirname, filename))
                        arcname = absname[len(abs_src) + 1:]
                        zf.write(absname, arcname)
            for key, value in upload_download_logs.iteritems():
                if os.path.isfile(value):
                    zf.write(value, key)
            zf.close()
            audit_str = 'Downloaded system configuration.'
            audit.audit('download_configuration',
                        audit_str, request)
        except Exception as e:
            raise Exception("Error compressing remote log file : %s" % str(e))
        response = django.http.HttpResponse(
            out.getvalue(), content_type='application/x-compressed')
        response['Content-disposition'] = 'attachment; filename=%s' % (
            zf_name)
        return response
    except Exception as e:
        return_dict["base_template"] = 'system_base.html'
        return_dict['tab'] = 'system_info_tab'
        return_dict["page_title"] = 'Download system configuration'
        return_dict["error"] = 'Error downloading system configuration'
        return_dict["error_details"] = str(e)
        return django.shortcuts.render_to_response("logged_in_error.html", return_dict, context_instance=django.template.context.RequestContext(request))
Example #6
0
def remove_old_entries(older_than=43200):
    try:
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)
        query = "delete from logs where timestamp < Datetime('now', '-%d seconds');" % older_than
        # print query
        db_path = '%s/db/inotify.db' % config_dir
        ret, err = db.execute_iud(db_path, [[query]])
        if err:
            print err
        # print ret
    except Exception, e:
        return False, 'Error purging old entries : %s' % str(e)
def remove_old_entries(older_than=43200):
    try:
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)
        query = "delete from logs where timestamp < Datetime('now', '-%d seconds');" % older_than
        # print query
        db_path = '%s/db/inotify.db' % config_dir
        ret, err = db.execute_iud(db_path, [[query]])
        if err:
            print err
        # print ret
    except Exception, e:
        return False, 'Error purging old entries : %s' % str(e)
Example #8
0
def generate_smb_conf():
    try:
        d, err = get_auth_settings()
        if err:
            raise Exception(err)
        smb_conf_path, err = config.get_smb_conf_path()
        if err:
            raise Exception(err)
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        # For customer specific or non configurable smb.conf lines
        extra_share_param_lines = None
        extra_global_param_lines = None
        if os.path.isfile('%s/customer_specific/extra_smb_share_params.conf' % config_dir):
            with open('%s/customer_specific/extra_smb_share_params.conf' % config_dir, 'r') as f1:
                extra_share_param_lines = f1.readlines()
        if os.path.isfile('%s/customer_specific/extra_smb_global_params.conf' % config_dir):
            with open('%s/customer_specific/extra_smb_global_params.conf' % config_dir, 'r') as f1:
                extra_global_param_lines = f1.readlines()
        # print extra_share_param_lines
        # print extra_global_param_lines
        with open("%s/smb.conf" % smb_conf_path, "w+") as f:
            ret, err = generate_global_header(f)
            if err:
                raise Exception(err)
            ret, err = generate_global_section(
                f, d, extra_global_param_lines)
            if err:
                raise Exception(err)
            shl, err = get_shares_list()
            if err:
                raise Exception(err)
            if shl:
                for share in shl:
                    ret, err = generate_share_section(
                        f, share["name"], d["workgroup"], share["path"], share["read_only"], share["browseable"], share["comment"], d["security"], extra_share_param_lines, share['hosts_allow'], share['hosts_deny'])
                    if err:
                        raise Exception(err)
            # Generate share section for /var/log
            ret, err = generate_share_section(
                f, 'system_logs', '', '/var/log', True, False, 'Integralstor system logs share', '')
            if err:
                raise Exception(err)
        ret, errors = reload_configuration()
        if errors:
            raise Exception(errors)
    except Exception, e:
        return False, 'Error generating CIFS configuration : %s' % str(e)
Example #9
0
def auto_rotate_logs():
    """DEPRICATED?

    Called from a script to rotate the audit and alert logs.
    """
    try:
        config, err = config.get_config_dir()
        if err:
            raise Exception(err)
        # Takes the log dir inside the config/logs and the log file name and
        # auto-rotates it.
        log_dict = {'audit': 'audit.log', 'alerts': 'alerts.log'}
        for key, value in log_dict.items():
            status, err = rotate_log(
                config + "/logs/" + key + "/", value, None)
            if err:
                raise Exception(err)
    except Exception, e:
        return False, "Error rotating logs " + str(e)
Example #10
0
def auto_rotate_logs():
    """DEPRICATED?

    Called from a script to rotate the audit and alert logs.
    """
    try:
        config, err = config.get_config_dir()
        if err:
            raise Exception(err)
        # Takes the log dir inside the config/logs and the log file name and
        # auto-rotates it.
        log_dict = {'audit': 'audit.log', 'alerts': 'alerts.log'}
        for key, value in log_dict.items():
            status, err = rotate_log(config + "/logs/" + key + "/", value,
                                     None)
            if err:
                raise Exception(err)
    except Exception, e:
        return False, "Error rotating logs " + str(e)
Example #11
0
def get_count(action='READ', past_x_seconds=60):
    count = -1
    try:
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        if action != 'ALL':
            query = "select count(*) as count from logs where timestamp >= Datetime('now', '-%d seconds');" % past_x_seconds
        else:
            query = "select count(*) as count from logs where actions = '%s' and timestamp >= Datetime('now', '-%d seconds');" % (
                action.upper(), past_x_seconds)
        # print query
        db_path = '%s/db/inotify.db' % config_dir
        ret, err = db.get_single_row(db_path, query)
        if err:
            print err
        count = ret['count']
        # print ret
    except Exception, e:
        return -1, 'Error getting counts : %s' % str(e)
Example #12
0
def get_count(action='READ', past_x_seconds=60):
    count = -1
    try:
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)

        if action != 'ALL':
            query = "select count(*) as count from logs where timestamp >= Datetime('now', '-%d seconds');" % past_x_seconds
        else:
            query = "select count(*) as count from logs where actions = '%s' and timestamp >= Datetime('now', '-%d seconds');" % (
                action.upper(), past_x_seconds)
        # print query
        db_path = '%s/db/inotify.db' % config_dir
        ret, err = db.get_single_row(db_path, query)
        if err:
            print err
        count = ret['count']
        # print ret
    except Exception, e:
        return -1, 'Error getting counts : %s' % str(e)
def upload_sys_info(request):
    return_dict = {}
    try:
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)
        if request.method == "POST":
            status, err = _handle_uploaded_file(request.FILES['file_field'])
            if err:
                raise Exception(err)
            display_name, err = config.get_config_dir()
            if err:
                raise Exception(err)
            zip = zipfile.ZipFile('/tmp/upload.zip', 'r')
            data = zip.namelist()
            move = zip.extractall("/tmp/upload_system_config/")
            logs = {
                'smb_conf': '/etc/samba/smb.conf',
                'ntp_conf': '/etc/ntp.conf',
                'krb5_conf': '/etc/krb5.conf',
                'nfs': '/etc/exports',
                'ftp': '/etc/vsftpd/vsftpd.conf'
            }
            if os.path.isfile('/tmp/upload_system_config/local_groups'):
                groups = None
                with open('/tmp/upload_system_config/local_groups', 'r') as f:
                    groups = json.load(f)
                if groups:
                    for group in groups:
                        # print 'creating group ', group
                        ret, err = local_users.create_local_group(
                            group['grpname'])
                        if err:
                            raise Exception(err)
                # print 'groups - ', groups
            if os.path.isfile('/tmp/upload_system_config/local_users'):
                users = None
                with open('/tmp/upload_system_config/local_users', 'r') as f:
                    users = json.load(f)
                if users:
                    default_group_name, err = config.get_users_default_group()
                    if err:
                        raise Exception(err)
                    default_gid, err = config.get_system_uid_gid(
                        default_group_name, 'group')
                    if err:
                        raise Exception(err)
                    for user in users:
                        # print 'creating user ', user
                        # print 'username is ', user['comment'][18:]
                        ret, err = local_users.create_local_user(
                            user['username'], user['comment'][18:], 'password',
                            default_gid)
                        if err:
                            raise Exception(err)
                        if 'other_groups' in user and user['other_groups']:
                            # print 'adding other groups'
                            for g in user['other_groups']:
                                ret, err = local_users.set_group_membership(
                                    g, [user['username']])
                                if err:
                                    raise Exception(err)
                # print 'users - ', users
            for key, value in upload_download_logs.iteritems():
                if key and os.path.isfile("/tmp/upload_system_config/" + key):
                    # print 'overwriting ', "/tmp/upload_system_config/%s"%key,
                    # 'to ', value
                    ret, err = _copy_file_and_overwrite(
                        "/tmp/upload_system_config/%s" % key, value)
                    if err:
                        raise Exception(err)
            for dir in upload_download_conf_dirs:
                if os.path.isdir("/tmp/upload_system_config/%s" % dir):
                    # print 'overwriting ', "/tmp/upload_system_config/%s"%dir,
                    # ' to ', '%s/%s'%(config_dir,dir)
                    ret, err = _copy_and_overwrite(
                        "/tmp/upload_system_config/%s" % dir,
                        '%s/%s' % (config_dir, dir))
                    if err:
                        raise Exception(err)
            services_restart_list = [
                'ntpd', 'smb', 'winbind', 'tgtd', 'nfs', 'vsftpd'
            ]
            for service in services_restart_list:
                ret, err = services_management.update_service_status(
                    service, 'restart')
                if err:
                    raise Exception(err)
            audit_str = 'Upload of an external system configuration completed successfully.'
            audit.audit('upload_configuration', audit_str, request)
            return django.http.HttpResponseRedirect(
                "/system/view_system_info?ack=config_uploaded")
        else:
            form = common_forms.FileUploadForm()
            return_dict["form"] = form
            return django.shortcuts.render_to_response(
                "upload_sys_info.html",
                return_dict,
                context_instance=django.template.context.RequestContext(
                    request))
    except Exception, e:
        audit_str = 'Upload of an external system configuration failed : %s.' % str(
            e)
        audit.audit('upload_configuration', audit_str, request)
        return_dict["base_template"] = 'system_base.html'
        return_dict['tab'] = 'system_info_tab'
        return_dict["page_title"] = 'Upload system configuration'
        return_dict["error"] = 'Error uploading system configuration'
        return_dict["error_details"] = str(e)
        return django.shortcuts.render_to_response(
            "logged_in_error.html",
            return_dict,
            context_instance=django.template.context.RequestContext(request))
def upload_sys_info(request):
    return_dict = {}
    try:
        config_dir, err = config.get_config_dir()
        if err:
            raise Exception(err)
        if request.method == "POST":
            status, err = _handle_uploaded_file(request.FILES['file_field'])
            if err:
                raise Exception(err)
            display_name, err = config.get_config_dir()
            if err:
                raise Exception(err)
            zip = zipfile.ZipFile('/tmp/upload.zip', 'r')
            data = zip.namelist()
            move = zip.extractall("/tmp/upload_system_config/")
            logs = {'smb_conf': '/etc/samba/smb.conf', 'ntp_conf': '/etc/ntp.conf', 'krb5_conf': '/etc/krb5.conf', 'nfs': '/etc/exports',
                    'ftp': '/etc/vsftpd/vsftpd.conf'}
            if os.path.isfile('/tmp/upload_system_config/local_groups'):
                groups = None
                with open('/tmp/upload_system_config/local_groups', 'r') as f:
                    groups = json.load(f)
                if groups:
                    for group in groups:
                        # print 'creating group ', group
                        ret, err = local_users.create_local_group(
                            group['grpname'])
                        if err:
                            raise Exception(err)
                # print 'groups - ', groups
            if os.path.isfile('/tmp/upload_system_config/local_users'):
                users = None
                with open('/tmp/upload_system_config/local_users', 'r') as f:
                    users = json.load(f)
                if users:
                    default_group_name, err = config.get_users_default_group()
                    if err:
                        raise Exception(err)
                    default_gid, err = config.get_system_uid_gid(
                        default_group_name, 'group')
                    if err:
                        raise Exception(err)
                    for user in users:
                        # print 'creating user ', user
                        # print 'username is ', user['comment'][18:]
                        ret, err = local_users.create_local_user(
                            user['username'], user['comment'][18:], 'password', default_gid)
                        if err:
                            raise Exception(err)
                        if 'other_groups' in user and user['other_groups']:
                            # print 'adding other groups'
                            for g in user['other_groups']:
                                ret, err = local_users.set_group_membership(
                                    g, [user['username']])
                                if err:
                                    raise Exception(err)
                # print 'users - ', users
            for key, value in upload_download_logs.iteritems():
                if key and os.path.isfile("/tmp/upload_system_config/" + key):
                    # print 'overwriting ', "/tmp/upload_system_config/%s"%key,
                    # 'to ', value
                    ret, err = _copy_file_and_overwrite(
                        "/tmp/upload_system_config/%s" % key, value)
                    if err:
                        raise Exception(err)
            for dir in upload_download_conf_dirs:
                if os.path.isdir("/tmp/upload_system_config/%s" % dir):
                    # print 'overwriting ', "/tmp/upload_system_config/%s"%dir,
                    # ' to ', '%s/%s'%(config_dir,dir)
                    ret, err = _copy_and_overwrite(
                        "/tmp/upload_system_config/%s" % dir, '%s/%s' % (config_dir, dir))
                    if err:
                        raise Exception(err)
            services_restart_list = ['ntpd', 'smb',
                                     'winbind', 'tgtd', 'nfs', 'vsftpd']
            for service in services_restart_list:
                ret, err = services_management.update_service_status(
                    service, 'restart')
                if err:
                    raise Exception(err)
            audit_str = 'Upload of an external system configuration completed successfully.'
            audit.audit('upload_configuration',
                        audit_str, request)
            return django.http.HttpResponseRedirect("/system/view_system_info?ack=config_uploaded")
        else:
            form = common_forms.FileUploadForm()
            return_dict["form"] = form
            return django.shortcuts.render_to_response("upload_sys_info.html", return_dict, context_instance=django.template.context.RequestContext(request))
    except Exception, e:
        audit_str = 'Upload of an external system configuration failed : %s.' % str(
            e)
        audit.audit('upload_configuration',
                    audit_str, request)
        return_dict["base_template"] = 'system_base.html'
        return_dict['tab'] = 'system_info_tab'
        return_dict["page_title"] = 'Upload system configuration'
        return_dict["error"] = 'Error uploading system configuration'
        return_dict["error_details"] = str(e)
        return django.shortcuts.render_to_response("logged_in_error.html", return_dict, context_instance=django.template.context.RequestContext(request))