Esempio n. 1
0
def edit_hosts_file(path, new_hostname):
    try:
        hosts = read_file_contents(path)
        hosts += '\n' + hosts_mod_comment + '\n'
        hosts += '127.0.0.1\t{}\n'.format(new_hostname)
        write_file_contents(path, hosts)
    except:
        logger.error("exception while changing change {}".format(path))
Esempio n. 2
0
def edit_hosts_file(path, new_hostname):
    try:
        hosts = read_file_contents(path)
        hosts += '\n' + hosts_mod_comment + '\n'
        hosts += '127.0.0.1\t{}\n'.format(new_hostname)
        write_file_contents(path, hosts)
    except:
        logger.error("exception while changing change {}".format(path))
Esempio n. 3
0
def get_metadata_archive():
    '''
    It creates a file (ARCHIVE_NAME) with all the information
    Returns the file
    '''
    ensure_dir(TMP_DIR)
    file_list = [
        {'name': 'kanux_version.txt', 'contents': get_version()},
        {'name': 'kanux_stamp.txt', 'contents': get_stamp()},
        {'name': 'process.txt', 'contents': get_processes()},
        {'name': 'packages.txt', 'contents': get_packages()},
        {'name': 'dmesg.txt', 'contents': get_dmesg()},
        {'name': 'syslog.txt', 'contents': get_syslog()},
        {'name': 'cmdline.txt', 'contents': read_file_contents('/boot/cmdline.txt')},
        {'name': 'config.txt', 'contents': read_file_contents('/boot/config.txt')},
        {'name': 'wifi-info.txt', 'contents': get_wifi_info()},
        {'name': 'usbdevices.txt', 'contents': get_usb_devices()},

        # TODO: Remove raw logs when json ones become stable
        {'name': 'app-logs.txt', 'contents': get_app_logs_raw()},

        {'name': 'app-logs-json.txt', 'contents': get_app_logs_json()},
        {'name': 'hdmi-info.txt', 'contents': get_hdmi_info()},
        {'name': 'edid.dat', 'contents': get_edid()},
        {'name': 'screen-log.txt', 'contents': get_screen_log()},
        {'name': 'xorg-log.txt', 'contents': get_xorg_log()},
        {'name': 'cpu-info.txt', 'contents': get_cpu_info()},
        {'name': 'lsof.txt', 'contents': get_lsof()},
        {'name': 'content-objects.txt', 'contents': get_co_list()}
    ]
    # Include the screenshot if it exists
    if os.path.isfile(SCREENSHOT_PATH):
        file_list.append({
                         'name': SCREENSHOT_NAME,
                         'contents': read_file_contents(SCREENSHOT_PATH)
                         })
    # Collect all coredumps, for applications that terminated unexpectedly
    for f in os.listdir('/var/tmp/'):
        if f.startswith('core-'):
            file_list.append({
                'name': f,
                'contents': read_file_contents(os.path.join('/var/tmp', f))
            })
    # create files for each non empty metadata info
    for file in file_list:
        if file['contents']:
            write_file_contents(TMP_DIR + file['name'], file['contents'])
    # archive all the metadata files
    # need to change dir to avoid tar subdirectories
    current_directory = os.getcwd()
    os.chdir(TMP_DIR)
    run_cmd("tar -zcvf {} *".format(ARCHIVE_NAME))
    # open the file and return it
    archive = open(ARCHIVE_NAME, 'rb')
    # restore the current working directory
    os.chdir(current_directory)

    return archive
Esempio n. 4
0
def set_apt_proxy(enable, host=None, port=None, username=None, password=None):
    url_string = generate_proxy_url(host, port, username, password)

    if enable and host and port:
        cfg = 'Acquire::http::proxy "{url}/";'.format(url=url_string)
    else:
        cfg = ''

    write_file_contents(apt_cfg, cfg)
Esempio n. 5
0
def set_apt_proxy(enable, host=None, port=None, username=None, password=None):
    url_string = generate_proxy_url(host, port, username, password)

    if enable and host and port:
        cfg = 'Acquire::http::proxy "{url}/";'.format(url=url_string)
    else:
        cfg = ''

    write_file_contents(apt_cfg, cfg)
Esempio n. 6
0
def set_wget(enable, host=None, port=None, username=None, password=None):
    data = ('http_proxy=http://{host}:{port}/\n'
            'https_proxy=http://{host}:{port}/\n').format(host=host, port=port)

    if username and password:
        data += 'proxy_user={username}\n'.format(username=username)
        data += 'proxy_password={password}\n'.format(password=password)

    write_file_contents('/etc/wgetrc', data)
Esempio n. 7
0
def set_curl(enable, host=None, port=None, username=None, password=None):
    url_string = generate_proxy_url(host, port, username, password)
    data = 'proxy={url}'.format(url=url_string)

    files = [os.path.join(f, '.curlrc') for f in get_all_home_folders(root=True, skel=True)]
    for f in files:
        if not enable:
            delete_file(f)
        else:
            write_file_contents(f, data)
Esempio n. 8
0
def create_empty_hosts():
    import platform
    hostname = platform.node()
    new_hosts = '127.0.0.1   localhost\n127.0.1.1   {}\n'.format(hostname)

    logger.debug("writing new hosts file")
    write_file_contents(hosts_file, new_hosts)

    logger.debug("restoring original hosts permission")
    os.chmod(hosts_file, 0644)
Esempio n. 9
0
def create_empty_hosts():
    import platform
    hostname = platform.node()
    new_hosts = '127.0.0.1   localhost\n127.0.1.1   {}\n'.format(hostname)

    logger.debug('writing new hosts file')
    write_file_contents(hosts_file, new_hosts)

    logger.debug('restoring original hosts permission')
    os.chmod(hosts_file, 0644)
Esempio n. 10
0
def get_metadata_archive():
    '''
    It creates a file (ARCHIVE_NAME) with all the information
    Returns the file
    '''
    ensure_dir(TMP_DIR)
    file_list = [
        {'name': 'kanux_version.txt', 'contents': get_version()},
        {'name': 'process.txt', 'contents': get_processes()},
        {'name': 'packages.txt', 'contents': get_packages()},
        {'name': 'dmesg.txt', 'contents': get_dmesg()},
        {'name': 'syslog.txt', 'contents': get_syslog()},
        {'name': 'cmdline.txt', 'contents': read_file_contents('/boot/cmdline.txt')},
        {'name': 'config.txt', 'contents': read_file_contents('/boot/config.txt')},
        {'name': 'wifi-info.txt', 'contents': get_wifi_info()},
        {'name': 'usbdevices.txt', 'contents': get_usb_devices()},

        # TODO: Remove raw logs when json ones become stable
        {'name': 'app-logs.txt', 'contents': get_app_logs_raw()},

        {'name': 'app-logs-json.txt', 'contents': get_app_logs_json()},
        {'name': 'hdmi-info.txt', 'contents': get_hdmi_info()},
        {'name': 'xorg-log.txt', 'contents': get_xorg_log()},
        {'name': 'cpu-info.txt', 'contents': get_cpu_info()},
        {'name': 'lsof.txt', 'contents': get_lsof()},
        {'name': 'content-objects.txt', 'contents': get_co_list()}
    ]
    # Include the screenshot if it exists
    if os.path.isfile(SCREENSHOT_PATH):
        file_list.append({
                         'name': SCREENSHOT_NAME,
                         'contents': read_file_contents(SCREENSHOT_PATH)
                         })
    # create files for each non empty metadata info
    for file in file_list:
        if file['contents']:
            write_file_contents(TMP_DIR + file['name'], file['contents'])
    # Collect all coredumps, for applications that terminated unexpectedly
    for f in os.listdir('/var/tmp/'):
        if f.startswith('core-'):
            file_list.append({
                'name': f,
                'contents': read_file_contents(os.path.join('/var/tmp', f))
            })
    # archive all the metadata files
    # need to change dir to avoid tar subdirectories
    current_directory = os.getcwd()
    os.chdir(TMP_DIR)
    run_cmd("tar -zcvf {} *".format(ARCHIVE_NAME))
    # open the file and return it
    archive = open(ARCHIVE_NAME, 'rb')
    # restore the current working directory
    os.chdir(current_directory)

    return archive
Esempio n. 11
0
def set_wget(enable, host=None, port=None, username=None, password=None):
    data = (
        'http_proxy=http://{host}:{port}/\n'
        'https_proxy=http://{host}:{port}/\n'
    ).format(host=host, port=port)

    if username and password:
        data += 'proxy_user={username}\n'.format(username=username)
        data += 'proxy_password={password}\n'.format(password=password)

    write_file_contents('/etc/wgetrc', data)
Esempio n. 12
0
def set_curl(enable, host=None, port=None, username=None, password=None):
    url_string = generate_proxy_url(host, port, username, password)
    data = 'proxy={url}'.format(url=url_string)

    files = [
        os.path.join(f, '.curlrc')
        for f in get_all_home_folders(root=True, skel=True)
    ]
    for f in files:
        if not enable:
            delete_file(f)
        else:
            write_file_contents(f, data)
Esempio n. 13
0
def set_parental_enabled(setting, _password):
    logger.debug('set_parental_enabled: {}'.format(setting))

    # turning on
    if setting:
        logger.debug('enabling')

        logger.debug('setting password')
        write_file_contents(password_file, encrypt_password(_password))

        logger.debug('making the file root read-only')
        os.chmod(password_file, 0400)

        logger.debug('enabling parental controls')
        set_parental_level(get_parental_level())

        msg = N_("Parental lock enabled!")
        logger.debug(msg)

        return True, _(msg)

    # turning off
    else:
        # password matches
        if read_file_contents(password_file) == encrypt_password(_password):
            logger.debug('password accepted, disabling')

            logger.debug('clearing password')
            os.remove(password_file)

            logger.debug('disabling parental controls')
            set_parental_level(-1)

            msg = N_("Parental lock disabled!")
            logger.debug(msg)

            return True, _(msg)

        # password doesn't match
        else:
            msg = N_("Password doesn't match\nleaving parental lock enabled!")
            logger.debug(msg)

            return False, _(msg)
Esempio n. 14
0
def set_parental_enabled(setting, _password):
    logger.debug('set_parental_enabled: {}'.format(setting))

    # turning on
    if setting:
        logger.debug('enabling')

        logger.debug('setting password')
        write_file_contents(password_file, encrypt_password(_password))

        logger.debug('making the file root read-only')
        os.chmod(password_file, 0400)

        logger.debug('enabling parental controls')
        set_parental_level(get_parental_level())

        msg = "Parental lock enabled!"
        logger.debug(msg)

        return True, msg

    # turning off
    else:
        # password matches
        if read_file_contents(password_file) == encrypt_password(_password):
            logger.debug('password accepted, disabling')

            logger.debug('clearing password')
            os.remove(password_file)

            logger.debug('disabling parental controls')
            set_parental_level(-1)

            msg = "Parental lock disabled!"
            logger.debug(msg)

            return True, msg

        # password doesn't match
        else:
            msg = "Password doesn't match\nleaving parental lock enabled!"
            logger.debug(msg)

            return False, msg
Esempio n. 15
0
def set_hostname(new_hostname):
    if os.environ['LOGNAME'] != 'root':
        logger.error("Error: Settings must be executed with root privileges")

    # Check username chars
    new_hostname = re.sub('[^a-zA-Z0-9]', '', new_hostname).lower()

    if new_hostname == '':
        logger.error("no letters left in username after removing illegal ones")
        return

    if new_hostname == 'kano':
        logger.info(
            " not tryng to set hostname as it is the same as the default")
        return

    # check for missing hosts file
    if not os.path.exists(hosts_file):
        create_empty_hosts()

    # check if already done
    curr_hosts = read_file_contents_as_lines(hosts_file)
    if hosts_mod_comment in curr_hosts:
        logger.warn("/etc/hosts already modified, not changing")
        return

    # actually edit the hosts file
    edit_hosts_file(hosts_file, new_hostname)

    # edit the backup file.
    if os.path.exists(hosts_file_backup):
        edit_hosts_file(hosts_file_backup, new_hostname)

    try:
        write_file_contents('/etc/hostname', new_hostname + '\n')
    except:
        logger.error("exception while changing change /etc/hostname")
Esempio n. 16
0
def set_hostname(new_hostname):
    if os.environ['LOGNAME'] != 'root':
        logger.error("Error: Settings must be executed with root privileges")

    # Check username chars
    new_hostname = re.sub('[^a-zA-Z0-9]', '', new_hostname).lower()

    if new_hostname == '':
        logger.error('no letters left in username after removing illegal ones')
        return

    if new_hostname == 'kano':
        logger.info(' not tryng to set hostname as it is the same as the default')
        return

    # check for missing hosts file
    if not os.path.exists(hosts_file):
        create_empty_hosts()

    # check if already done
    curr_hosts = read_file_contents_as_lines(hosts_file)
    if hosts_mod_comment in curr_hosts:
        logger.warn('/etc/hosts already modified, not changing')
        return

    # actually edit the hosts file
    edit_hosts_file(hosts_file, new_hostname)

    # edit the backup file.
    if os.path.exists(hosts_file_backup):
        edit_hosts_file(hosts_file_backup, new_hostname)

    try:
        write_file_contents('/etc/hostname', new_hostname + '\n')
    except:
        logger.error("exception while changing change /etc/hostname")
Esempio n. 17
0
def get_metadata_archive(title='', desc=''):
    '''
    It creates a file (ARCHIVE_NAME) with all the information
    Returns the file
    '''
    ensure_dir(TMP_DIR)
    file_list = [
        {
            'name': 'metadata.json',
            'contents': json.dumps({
                'title': title,
                'description': desc
            })
        },
        {
            'name': 'kanux_version.txt',
            'contents': get_version()
        },
        {
            'name': 'kanux_stamp.txt',
            'contents': get_stamp()
        },
        {
            'name': 'process.txt',
            'contents': get_processes()
        },
        {
            'name': 'process-tree.txt',
            'contents': get_process_tree()
        },
        {
            'name': 'packages.txt',
            'contents': get_packages()
        },
        {
            'name': 'dmesg.txt',
            'contents': get_dmesg()
        },
        {
            'name': 'syslog.txt',
            'contents': get_syslog()
        },
        {
            'name': 'cmdline.txt',
            'contents': read_file_contents('/boot/cmdline.txt')
        },
        {
            'name': 'config.txt',
            'contents': read_file_contents('/boot/config.txt')
        },
        {
            'name': 'wifi-info.txt',
            'contents': get_wifi_info()
        },
        {
            'name': 'usbdevices.txt',
            'contents': get_usb_devices()
        },

        # TODO: Remove raw logs when json ones become stable
        {
            'name': 'app-logs.txt',
            'contents': get_app_logs_raw()
        },
        {
            'name': 'app-logs-json.txt',
            'contents': get_app_logs_json()
        },
        {
            'name': 'hdmi-info.txt',
            'contents': get_hdmi_info()
        },
        {
            'name': 'edid.dat',
            'contents': get_edid()
        },
        {
            'name': 'screen-log.txt',
            'contents': get_screen_log()
        },
        {
            'name': 'xorg-log.txt',
            'contents': get_xorg_log()
        },
        {
            'name': 'cpu-info.txt',
            'contents': get_cpu_info()
        },
        {
            'name': 'mem-stats.txt',
            'contents': get_mem_stats()
        },
        {
            'name': 'lsof.txt',
            'contents': get_lsof()
        },
        {
            'name': 'content-objects.txt',
            'contents': get_co_list()
        },
        {
            'name': 'disk-space.txt',
            'contents': get_disk_space()
        },
        {
            'name': 'lsblk.txt',
            'contents': get_lsblk()
        },
        {
            'name': 'sources-list.txt',
            'contents': get_sources_list()
        },
    ]
    file_list += get_install_logs()

    # Include the screenshot if it exists
    if os.path.isfile(SCREENSHOT_PATH):
        file_list.append({
            'name': SCREENSHOT_NAME,
            'contents': read_file_contents(SCREENSHOT_PATH)
        })
    # Collect all coredumps, for applications that terminated unexpectedly
    for f in os.listdir('/var/tmp/'):
        if f.startswith('core-'):
            file_list.append({
                'name':
                f,
                'contents':
                read_file_contents(os.path.join('/var/tmp', f))
            })
    # create files for each non empty metadata info
    for file in file_list:
        if file['contents']:
            write_file_contents(os.path.join(TMP_DIR, file['name']),
                                file['contents'])
    # archive all the metadata files
    import tarfile
    with tarfile.open(ARCHIVE_PATH, mode='w') as archive:
        for f in os.listdir(TMP_DIR):
            archive.add(os.path.join(TMP_DIR, f), arcname=f)

    # open the file and return it
    archive = open(ARCHIVE_PATH, 'rb')

    return archive
Esempio n. 18
0
def write_blacklisted_sites(blacklist):
    write_file_contents(blacklist_file, '\n'.join(blacklist))
Esempio n. 19
0
def write_whitelisted_sites(whitelist):
    write_file_contents(whitelist_file, '\n'.join(whitelist))
Esempio n. 20
0
def write_whitelisted_sites(whitelist):
    write_file_contents(whitelist_file, '\n'.join(whitelist))
Esempio n. 21
0
def set_dns(servers):
    server_str = '\n'.join(
        ['nameserver {}'.format(server) for server in servers])

    write_file_contents(DNS_FILE, server_str)
Esempio n. 22
0
 def beta_111_to_beta_120(self):
     purge("kano-unlocker")
     repo_url = "deb http://mirrordirector.raspbian.org/raspbian/ wheezy main contrib non-free rpi"
     write_file_contents('/etc/apt/sources.list', repo_url + '\n')
     run_cmd_log('apt-get -y clean')
     run_cmd_log('apt-get -y update')
Esempio n. 23
0
def get_metadata_archive(title='', desc=''):
    '''
    It creates a file (ARCHIVE_NAME) with all the information
    Returns the file
    '''
    ensure_dir(TMP_DIR)
    file_list = [
        {
            'name': 'metadata.json',
            'contents': json.dumps({'title': title, 'description': desc})
        },
        {'name': 'kanux_version.txt', 'contents': get_version()},
        {'name': 'kanux_stamp.txt', 'contents': get_stamp()},
        {'name': 'process.txt', 'contents': get_processes()},
        {'name': 'process-tree.txt', 'contents': get_process_tree()},
        {'name': 'packages.txt', 'contents': get_packages()},
        {'name': 'dmesg.txt', 'contents': get_dmesg()},
        {'name': 'syslog.txt', 'contents': get_syslog()},
        {
            'name': 'cmdline.txt',
            'contents': read_file_contents('/boot/cmdline.txt')
        },
        {
            'name': 'config.txt',
            'contents': read_file_contents('/boot/config.txt')
        },
        {'name': 'wifi-info.txt', 'contents': get_wifi_info()},
        {'name': 'usbdevices.txt', 'contents': get_usb_devices()},

        # TODO: Remove raw logs when json ones become stable
        {'name': 'app-logs.txt', 'contents': get_app_logs_raw()},

        {'name': 'app-logs-json.txt', 'contents': get_app_logs_json()},
        {'name': 'hdmi-info.txt', 'contents': get_hdmi_info()},
        {'name': 'edid.dat', 'contents': get_edid()},
        {'name': 'screen-log.txt', 'contents': get_screen_log()},
        {'name': 'xorg-log.txt', 'contents': get_xorg_log()},
        {'name': 'cpu-info.txt', 'contents': get_cpu_info()},
        {'name': 'mem-stats.txt', 'contents': get_mem_stats()},
        {'name': 'lsof.txt', 'contents': get_lsof()},
        {'name': 'content-objects.txt', 'contents': get_co_list()},
        {'name': 'disk-space.txt', 'contents': get_disk_space()},
        {'name': 'lsblk.txt', 'contents': get_lsblk()},
        {'name': 'sources-list.txt', 'contents': get_sources_list()},
    ]
    file_list += get_install_logs()

    # Include the screenshot if it exists
    if os.path.isfile(SCREENSHOT_PATH):
        file_list.append({
            'name': SCREENSHOT_NAME,
            'contents': read_file_contents(SCREENSHOT_PATH)
        })
    # Collect all coredumps, for applications that terminated unexpectedly
    for f in os.listdir('/var/tmp/'):
        if f.startswith('core-'):
            file_list.append({
                'name': f,
                'contents': read_file_contents(os.path.join('/var/tmp', f))
            })
    # create files for each non empty metadata info
    for file in file_list:
        if file['contents']:
            write_file_contents(
                os.path.join(TMP_DIR, file['name']), file['contents']
            )
    # archive all the metadata files
    import tarfile
    with tarfile.open(ARCHIVE_PATH, mode='w') as archive:
        for f in os.listdir(TMP_DIR):
            archive.add(os.path.join(TMP_DIR, f), arcname=f)

    # open the file and return it
    archive = open(ARCHIVE_PATH, 'rb')

    return archive
Esempio n. 24
0
def set_dns(servers):
    server_str = "\n".join(["nameserver {}".format(server) for server in servers])

    write_file_contents(DNS_FILE, server_str)
Esempio n. 25
0
def write_blacklisted_sites(blacklist):
    write_file_contents(blacklist_file, '\n'.join(blacklist))
Esempio n. 26
0
 def beta_111_to_beta_120(self):
     purge("kano-unlocker")
     repo_url = "deb http://mirrordirector.raspbian.org/raspbian/ wheezy main contrib non-free rpi"
     write_file_contents('/etc/apt/sources.list', repo_url + '\n')
     run_cmd_log('apt-get -y clean')
     run_cmd_log('apt-get -y update')
Esempio n. 27
0
def set_dns(servers):
    server_str = '\n'.join(
        ['nameserver {}'.format(server) for server in servers])

    write_file_contents(DNS_FILE, server_str)