summaryrefslogtreecommitdiffstats
path: root/src/account/test/methods.py
blob: 4043ddf833361ddbd09978707c1b4222f7e6d5f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# -*- encoding: utf-8 -*-
# Copyright (C) 2012-2014 Red Hat, Inc.  All rights reserved.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#
# Authors: Roman Rakus <rrakus@redhat.com>
#

import random
import string
import subprocess

def user_exists(username):
    """
    Return true/false if user does/does not exists
    """
    got = field_in_passwd(username, 0)
    return got == username

def group_exists(groupname):
    """
    Return true/false if user does/does not exists
    """
    got = field_in_group(groupname, 0)
    return got == groupname

def field_in_passwd(username, number):
    """
    Return numberth field in /etc/passwd for given username
    """
    for line in open("/etc/passwd").readlines():
        if line.startswith(username):
            return line.split(":")[number].strip()

def field_in_shadow(username, number):
    """
    Return numberth field in /etc/shadow for given username
    """
    for line in open("/etc/shadow").readlines():
        if line.startswith(username):
            return line.split(":")[number].strip()

def field_in_group(groupname, number):
    """
    Return numberth field in /etc/group for given groupname
    """
    for line in open("/etc/group").readlines():
        if line.startswith(groupname):
            return line.split(":")[number].strip()

def clean_account(user_name):
    """
    Force to delete testing account and remove home dir
    """
    if user_exists(user_name):
        subprocess.check_call(["userdel", "-fr", user_name])
    if group_exists(user_name):
        # groups should be expicitely deleted
        subprocess.check_call(["groupdel", user_name])

def add_user_to_group(user_name, group_name):
    """
    Will add user to group
    """
    subprocess.check_call(["usermod", "-a", "-G", group_name, user_name])

def create_account(user_name):
    """
    Force to create account; run clean_account before creation
    """
    if not user_exists(user_name):
        subprocess.check_call(["useradd", user_name])

def clean_group(group_name):
    """
    Force to delete testing group
    """
    if group_exists(group_name):
        subprocess.check_call(["groupdel", group_name])

def create_group(group_name):
    """
    Force to create group
    """
    if not group_exists(group_name):
        subprocess.check_call(["groupadd", group_name])

def random_string(strength=6, chars=None, prefix=""):
    """
    Generate a random string, e.g. usable as UID/GID

    strength is count of random characters in the final string.  chars
    is sequence of characters to choose from, and prefix can be provided
    to prepend it to final string.
    """
    if chars is None:
        chars = string.ascii_uppercase + string.digits
    salt = ''.join([random.choice(chars) for x in range(strength)])
    return prefix + salt

def random_shell():
    """
    Make up a funny shell
    """
    return random.choice([
        "/bin/ash",
        "/bin/cash",
        "/bin/dash",
        "/bin/hash",
        "/bin/nash",
        "/bin/mash",
        "/bin/sash",
        "/bin/stash",
        "/bin/splash",
        "/bin/wash",
    ])

def field_is_unique(fname, records):
    """
    True if the field in `records` has unique values.
    """
    seen = []
    for record in records:
        if record[fname] in seen:
            return False
        else:
            seen.append(record[fname])
    return True