Example #1
0
def toggle_sftp_service(switch=True):
    """
    Toggles the SFTP service on/off by writing or not the
    `Subsystem sftp internal-sftp` (settings.SFTP_STR) declaration in SSHD_CONFIG.
    :param switch:
    :return:
    """
    fo, npath = mkstemp()
    written = False
    with open(SSHD_CONFIG) as sfo, open(npath, "w") as tfo:
        for line in sfo.readlines():
            if re.match(settings.SFTP_STR, line) is not None:
                if switch and not written:
                    tfo.write("{}\n".format(settings.SFTP_STR))
                    written = True
            elif re.match(settings.SSHD_HEADER, line) is not None:
                tfo.write(line)
                if switch and not written:
                    tfo.write("{}\n".format(settings.SFTP_STR))
                    written = True
            else:
                tfo.write(line)
    move(npath, SSHD_CONFIG)
    try:
        systemctl("sshd", "reload")
    except:
        return systemctl("sshd", "restart")
Example #2
0
def auto_update_status():
    enabled = False
    with open(YCFILE) as ifo:
        for line in ifo.readlines():
            if re.match("apply_updates = yes", line) is not None:
                enabled = True
                break
    if enabled:
        systemctl("yum-cron", "status")
    return enabled
Example #3
0
def auto_update_status():
    enabled = False
    with open(YCFILE) as ifo:
        for line in ifo.readlines():
            if (re.match('apply_updates = yes', line) is not None):
                enabled = True
                break
    if (enabled):
        systemctl('yum-cron', 'status')
    return enabled
Example #4
0
def auto_update_status():
    enabled = False
    with open(YCFILE) as ifo:
        for line in ifo.readlines():
            if (re.match('apply_updates = yes', line) is not None):
                enabled = True
                break
    if (enabled):
        systemctl('yum-cron', 'status')
    return enabled
Example #5
0
def auto_update(enable=True):
    # TODO: Add openSUSE zypper equivalent
    service = "yum-cron"
    fo, npath = mkstemp()
    updated = False
    with open(YCFILE) as ifo, open(npath, "w") as tfo:
        for line in ifo.readlines():
            if re.match("apply_updates = ", line) is not None:
                if enable:
                    tfo.write("apply_updates = yes\n")
                else:
                    tfo.write("apply_updates = no\n")
                updated = True
            else:
                tfo.write(line)
    if not updated:
        raise Exception("apply_updates directive missing in {}, assuming it "
                        "is corrupt. No change made.".format(YCFILE))
    shutil.move(npath, YCFILE)
    if enable:
        systemctl(service, "enable")
        systemctl(service, "start")
    else:
        systemctl(service, "stop")
        systemctl(service, "disable")
Example #6
0
def toggle_sftp_service(switch=True):
    fo, npath = mkstemp()
    written = False
    with open(SSHD_CONFIG) as sfo, open(npath, 'w') as tfo:
        for line in sfo.readlines():
            if (re.match(SFTP_REGEX, line) is not None):
                if (switch and not written):
                    tfo.write('%s\n' % SFTP_STR)
                    written = True
            elif (re.match(settings.SSHD_HEADER, line) is not None):
                if (switch and not written):
                    tfo.write('%s\n' % SFTP_STR)
                    written = True
                tfo.write(line)
            else:
                tfo.write(line)
    move(npath, SSHD_CONFIG)
    try:
        systemctl('sshd', 'reload')
    except:
        return systemctl('sshd', 'restart')
Example #7
0
def update_sftp_config(input_map):
    """
    input map is a dictionary of user,directory pairs
    """
    fo, npath = mkstemp()
    with open(SSHD_CONFIG) as sfo, open(npath, 'w') as tfo:
        for line in sfo.readlines():
            if (re.match(SSHD_HEADER, line) is None):
                tfo.write(line)
            else:
                break
        tfo.write('%s\n' % SSHD_HEADER)
        for user in input_map:
            tfo.write('Match User %s\n' % user)
            tfo.write('\tChrootDirectory %s\n' % input_map[user])

    move(npath, SSHD_CONFIG)
    try:
        systemctl('sshd', 'reload')
    except:
        return systemctl('sshd', 'restart')
Example #8
0
def update_sftp_config(input_map):
    """
    input map is a dictionary of user,directory pairs
    """
    fo, npath = mkstemp()
    with open(SSHD_CONFIG) as sfo, open(npath, "w") as tfo:
        for line in sfo.readlines():
            if re.match(SSHD_HEADER, line) is None:
                tfo.write(line)
            else:
                break
        tfo.write("%s\n" % SSHD_HEADER)
        for user in input_map:
            tfo.write("Match User %s\n" % user)
            tfo.write("\tChrootDirectory %s\n" % input_map[user])

    move(npath, SSHD_CONFIG)
    try:
        systemctl("sshd", "reload")
    except:
        return systemctl("sshd", "restart")
Example #9
0
def toggle_sftp_service(switch=True):
    fo, npath = mkstemp()
    written = False
    with open(SSHD_CONFIG) as sfo, open(npath, "w") as tfo:
        for line in sfo.readlines():
            if re.match(SFTP_REGEX, line) is not None:
                if switch and not written:
                    tfo.write("%s\n" % SFTP_STR)
                    written = True
            elif re.match(SSHD_HEADER, line) is not None:
                if switch and not written:
                    tfo.write("%s\n" % SFTP_STR)
                    written = True
                tfo.write(line)
            else:
                tfo.write(line)
    move(npath, SSHD_CONFIG)
    try:
        systemctl("sshd", "reload")
    except:
        return systemctl("sshd", "restart")
Example #10
0
def toggle_sftp_service(switch=True):
    # TODO add Subsystem sftp line below Rockstor header rather than above
    fo, npath = mkstemp()
    written = False
    with open(SSHD_CONFIG) as sfo, open(npath, 'w') as tfo:
        for line in sfo.readlines():
            if (re.match(SFTP_REGEX, line) is not None):
                if (switch and not written):
                    tfo.write('%s\n' % SFTP_STR)
                    written = True
            elif (re.match(settings.SSHD_HEADER, line) is not None):
                if (switch and not written):
                    tfo.write('%s\n' % SFTP_STR)
                    written = True
                tfo.write(line)
            else:
                tfo.write(line)
    move(npath, SSHD_CONFIG)
    try:
        systemctl('sshd', 'reload')
    except:
        return systemctl('sshd', 'restart')
Example #11
0
def update_sftp_config(input_map):
    """
    input map is a dictionary of user,directory pairs
    """
    fo, npath = mkstemp()
    userstr = 'AllowUsers root %s' % ' '.join(input_map.keys())
    with open(SSHD_CONFIG) as sfo, open(npath, 'w') as tfo:
        for line in sfo.readlines():
            if (re.match(settings.SSHD_HEADER, line) is None):
                tfo.write(line)
            else:
                break
        tfo.write('%s\n' % settings.SSHD_HEADER)
        tfo.write('%s\n' % userstr)
        for user in input_map:
            tfo.write('Match User %s\n' % user)
            tfo.write('\tChrootDirectory %s\n' % input_map[user])

    move(npath, SSHD_CONFIG)
    try:
        systemctl('sshd', 'reload')
    except:
        return systemctl('sshd', 'restart')
Example #12
0
def update_sftp_config(input_map):
    """
    Fetch sftp-related customization settings from database
    and writes them to SSHD_CONFIG.
    :param input_map: dictionary of user,directory pairs.
    :return:
    """
    fo, npath = mkstemp()
    userstr = "AllowUsers root {}".format(" ".join(input_map.keys()))
    with open(SSHD_CONFIG) as sfo, open(npath, "w") as tfo:
        for line in sfo.readlines():
            if re.match(settings.SSHD_HEADER, line) is None:
                tfo.write(line)
            else:
                break
        tfo.write("{}\n".format(settings.SSHD_HEADER))
        # Detect sftp service status and ensure we maintain it
        if is_sftp_running():
            tfo.write("{}\n".format(settings.SFTP_STR))
        tfo.write("{}\n".format(userstr))
        # Set options for each user according to openSUSE's defaults:
        # https://en.opensuse.org/SDB:SFTP_server_with_Chroot#Match_rule_block
        # TODO: implement webUI element to re-enable rsync over ssh by omitting
        #   the `ForceCommand internal sftp` line below.
        for user in input_map:
            tfo.write("Match User {}\n".format(user))
            tfo.write("\tForceCommand internal-sftp\n")
            tfo.write("\tChrootDirectory {}\n".format(input_map[user]))
            tfo.write("\tX11Forwarding no\n")
            tfo.write("\tAllowTcpForwarding no\n")

    move(npath, SSHD_CONFIG)
    try:
        systemctl("sshd", "reload")
    except:
        return systemctl("sshd", "restart")
Example #13
0
def update_sftp_config(input_map):
    """
    input map is a dictionary of user,directory pairs
    """
    fo, npath = mkstemp()
    with open(SSHD_CONFIG) as sfo, open(npath, 'w') as tfo:
        for line in sfo.readlines():
            if (re.match(SSHD_HEADER, line) is None):
                tfo.write(line)
            else:
                break
        tfo.write('%s\n' % SSHD_HEADER)
        for user in input_map:
            tfo.write('Match User %s\n' % user)
            tfo.write('\tChrootDirectory %s\n' % input_map[user])

    move(npath, SSHD_CONFIG)
    return systemctl('sshd', 'reload')
Example #14
0
def toggle_sftp_service(switch=True):
    fo, npath = mkstemp()
    written = False
    with open(SSHD_CONFIG) as sfo, open(npath, 'w') as tfo:
        for line in sfo.readlines():
            if (re.match(SFTP_STR, line) is not None):
                if (switch):
                    tfo.write('%s\n' % SFTP_STR)
                    written = True
            elif (re.match(SSHD_HEADER, line) is not None):
                if (switch and not written):
                    tfo.write('%s\n' % SFTP_STR)
                    written = True
                tfo.write(line)
            else:
                tfo.write(line)
    move(npath, SSHD_CONFIG)
    return systemctl('sshd', 'reload')
Example #15
0
def update_sftp_config(input_list):
    """
    input list is a list of dictionaries. sample dictionary:
    {'user': '******',
     'dir': '/mnt3/rocky',}

    """
    fo, npath = mkstemp()
    with open(SSHD_CONFIG) as sfo, open(npath, 'w') as tfo:
        for line in sfo.readlines():
            if (re.match('####BEGIN: Rockstor SFTP CONFIG####', line)
                    is not None):
                tfo.write(line)
                for entry in input_list:
                    tfo.write('Match User %s\n' % entry['user'])
                    tfo.write('\tChrootDirectory %s\n' % entry['dir'])
                tfo.write('####END: Rockstor SFTP CONFIG####\n')
                break
            else:
                tfo.write(line)
    move(npath, SSHD_CONFIG)
    return systemctl('sshd', 'reload')
Example #16
0
def update_sftp_config(input_list):
    """
    input list is a list of dictionaries. sample dictionary:
    {'user': '******',
     'dir': '/mnt3/rocky',}

    """
    fo, npath = mkstemp()
    with open(SSHD_CONFIG) as sfo, open(npath, 'w') as tfo:
        for line in sfo.readlines():
            if (re.match('####BEGIN: Rockstor SFTP CONFIG####', line) is not
                None):
                tfo.write(line)
                for entry in input_list:
                    tfo.write('Match User %s\n' % entry['user'])
                    tfo.write('\tChrootDirectory %s\n' % entry['dir'])
                tfo.write('####END: Rockstor SFTP CONFIG####\n')
                break
            else:
                tfo.write(line)
    move(npath, SSHD_CONFIG)
    return systemctl('sshd', 'reload')
Example #17
0
def auto_update(enable=True):
    service = 'yum-cron'
    fo, npath = mkstemp()
    updated = False
    with open(YCFILE) as ifo, open(npath, 'w') as tfo:
        for line in ifo.readlines():
            if (re.match('apply_updates = ', line) is not None):
                if (enable):
                    tfo.write('apply_updates = yes\n')
                else:
                    tfo.write('apply_updates = no\n')
                updated = True
            else:
                tfo.write(line)
    if (not updated):
        raise Exception('apply_updates directive missing in %s, assuming its '
                        'is corrupt. No change made.' % YCFILE)
    shutil.move(npath, YCFILE)
    if (enable):
        systemctl(service, 'enable')
        systemctl(service, 'start')
    else:
        systemctl(service, 'stop')
        systemctl(service, 'disable')
Example #18
0
def auto_update(enable=True):
    service = 'yum-cron'
    fo, npath = mkstemp()
    updated = False
    with open(YCFILE) as ifo, open(npath, 'w') as tfo:
        for line in ifo.readlines():
            if (re.match('apply_updates = ', line) is not None):
                if (enable):
                    tfo.write('apply_updates = yes\n')
                else:
                    tfo.write('apply_updates = no\n')
                updated = True
            else:
                tfo.write(line)
    if (not updated):
        raise Exception('apply_updates directive missing in %s, assuming its '
                        'is corrupt. No change made.' % YCFILE)
    shutil.move(npath, YCFILE)
    if (enable):
        systemctl(service, 'enable')
        systemctl(service, 'start')
    else:
        systemctl(service, 'stop')
        systemctl(service, 'disable')