gencode-remote 5.02 KB
Newer Older
Darko Poljak's avatar
Darko Poljak committed
1
#!/bin/sh -e
Steven Armstrong's avatar
Steven Armstrong committed
2 3
#
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
4
# 2011 Nico Schottelius (nico-cdist at schottelius.org)
5
# 2013 Daniel Heule (hda at sfs.biz)
6
# 2018 Thomas Eckert (tom at it-eckert.de)
Steven Armstrong's avatar
Steven Armstrong committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#
# This file is part of cdist.
#
# cdist is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cdist 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#
#
# Manage users.
#
26
#set -x
Steven Armstrong's avatar
Steven Armstrong committed
27 28 29

name="$__object_id"

30 31
os="$(cat "$__global/explorer/os")"

32 33
state=$(cat "$__object/parameter/state")

34 35 36 37
# We need to shorten options for both usermod and useradd since on some
# systems (such as *BSD, Darwin) those commands do not handle GNU style long
# options.
shorten_property() {
38
    unset ret
Sébastien Gross's avatar
Sébastien Gross committed
39 40 41 42 43 44 45 46
    case "$1" in
	comment) ret="-c";;
	home) ret="-d";;
	gid) ret="-g";;
	groups) ret="-G";;
	password) ret="-p";;
	shell) ret="-s";;
	uid) ret="-u";;
47
    create-home) ret="-m";;
48
    system) ret="-r";;
Sébastien Gross's avatar
Sébastien Gross committed
49 50
    esac
    echo "$ret"
51 52
}

53 54 55
if [ "$state" = "present" ]; then
    cd "$__object/parameter"
    if grep -q "^${name}:" "$__object/explorer/passwd"; then
56
       for property in *; do
57 58
          new_value="$(cat "$property")"
          unset current_value
Steven Armstrong's avatar
Steven Armstrong committed
59

60
          file="$__object/explorer/passwd"
Steven Armstrong's avatar
Steven Armstrong committed
61

62 63
          case "$property" in
             gid)
64
                if echo "$new_value" | grep -q '^[0-9][0-9]*$'; then
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
                   field=4
                else
                   # We were passed a group name.  Compare the gid in
                   # the user's /etc/passwd entry with the gid of the
                   # group returned by the group explorer.
                   gid_from_group=$(awk -F: '{ print $3 }' "$__object/explorer/group")
                   gid_from_passwd=$(awk -F: '{ print $4 }' "$file")
                   if [ "$gid_from_group" != "$gid_from_passwd" ]; then
                      current_value="$gid_from_passwd"
                   else
                      current_value="$new_value"
                   fi
                fi
             ;;
             password)
                field=2
                file="$__object/explorer/shadow"
             ;;
             comment) field=5 ;;
             home)    field=6 ;;
             shell)   field=7 ;;
             uid)     field=3 ;;
             create-home) continue;; # Does not apply to user modification
             system) continue;; # Does not apply to user modification
             state) continue;; # Does not apply to user modification
             remove-home) continue;; # Does not apply to user modification
          esac
92

93 94 95 96 97 98
          # If we haven't already set $current_value above, pull it from the
          # appropriate file/field.
          if [ -z "$current_value" ]; then
             export field
             current_value="$(awk -F: '{ print $ENVIRON["field"] }' < "$file")"
          fi
99

100
          if [ "$new_value" != "$current_value" ]; then
Jonas Weber's avatar
Jonas Weber committed
101
              set -- "$@" "$(shorten_property "$property")" \'"$new_value"\'
102 103
          fi
       done
104

105
       if [ $# -gt 0 ]; then
106
          echo mod >> "$__messages_out"
107
          if [ "$os" = "freebsd" ]; then
108
             echo pw usermod "$@" -n "$name"
109 110 111 112 113 114 115
          else
             echo usermod "$@" "$name"
          fi
       else
          true
       fi
    else
116
        echo add >> "$__messages_out"
117
        for property in *; do
118 119 120 121
            [ "$property" = "state" ] && continue
            [ "$property" = "remove-home" ] && continue
            new_value="$(cat "$property")"
            if [ -z "$new_value" ];then       # Boolean values have no value
Jonas Weber's avatar
Jonas Weber committed
122
              set -- "$@" "$(shorten_property "$property")"
123
            else
Jonas Weber's avatar
Jonas Weber committed
124
              set -- "$@" "$(shorten_property "$property")" \'"$new_value"\'
125 126
            fi
        done
127

128
       if [ "$os" = "freebsd" ]; then
129
          echo pw useradd "$@" -n "$name"
130 131 132 133
       else
          echo useradd "$@" "$name"
       fi
    fi
134
elif [ "$state" = "absent" ]; then
135 136 137
    if grep -q "^${name}:" "$__object/explorer/passwd"; then
        #user exists, but state != present, so delete it
        if [ -f "$__object/parameter/remove-home" ]; then
138 139 140 141 142 143
            if [ "$os" = "freebsd" ]; then
                printf "pw userdel '%s' -r >/dev/null 2>&1\\n" "${name}"
            else
                printf "userdel -r '%s' >/dev/null 2>&1\\n" "${name}"
            fi
            echo "userdel -r" >> "$__messages_out"
144
        else
145 146 147 148 149 150
            if [ "$os" = "freebsd" ]; then
                printf "pw userdel '%s' >/dev/null 2>&1\\n" "${name}"
            else
                printf "userdel '%s' >/dev/null 2>&1\\n" "${name}"
            fi
            echo "userdel" >> "$__messages_out"
151 152
        fi
    fi
153 154
else
    echo "Invalid state $state" >&2
155
fi