File: user-state

package info (click to toggle)
snapd 2.71-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 79,536 kB
  • sloc: ansic: 16,114; sh: 16,105; python: 9,941; makefile: 1,890; exp: 190; awk: 40; xml: 22
file content (80 lines) | stat: -rwxr-xr-x 1,620 bytes parent folder | download | duplicates (3)
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
#!/bin/bash -e

show_help() {
    echo "usage: user-state remove-with-group <user>"
    echo "usage: user-state list-users"
    echo "usage: user-state list-groups"
    echo ""
    echo "The tool is used to manage users and groups."
}

remove_with_group() {
    USER=$1
    if [ -z "$USER" ]; then
        echo "user-state: user is a required parameter"
        exit 1
    fi

    if getent passwd "$USER"; then
        if [ -f /var/lib/extrausers/passwd ]; then
            userdel --extrausers --force --remove "$USER"
        else
            userdel --force --remove "$USER"
        fi
        if getent passwd "$USER"; then
            echo "user-state: user exists after removal"
            exit 1
        fi
    fi

    if getent group "$USER"; then
        if groupdel -h | grep -q force; then
            groupdel -f "$USER"
        else
            groupdel "$USER"
        fi
        if getent group "$USER"; then
            echo "user-state: group exists after removal"
            exit 1
        fi
    fi
}

list_users() {
    getent passwd | cut -d: -f1
}

list_groups () {
    getent group | cut -d: -f1
}

main() {
    if [ $# -eq 0 ]; then
        show_help
        exit 0
    fi

    local subcommand="$1"
    local action=

    case "$1" in
        -h|--help)
            show_help
            exit 0
            ;;
        *)
            action=$(echo "$subcommand" | tr '-' '_')
            shift
            ;;
    esac

    if [ -z "$(declare -f "$action")" ]; then
        echo "user-state: no such command: $subcommand"
        show_help
        exit 1
    fi

    "$action" "$@"
}

main "$@"