소스 검색

Improve user removals

Bob Mottram 8 년 전
부모
커밋
8b47da2be5
1개의 변경된 파일34개의 추가작업 그리고 27개의 파일을 삭제
  1. 34
    27
      src/freedombone-rmuser

+ 34
- 27
src/freedombone-rmuser 파일 보기

@@ -48,65 +48,72 @@ do
48 48
     source $f
49 49
 done
50 50
 
51
-MY_USERNAME=$1
51
+read_config_param MY_USERNAME
52 52
 
53
-if [ ! $MY_USERNAME ]; then
53
+REMOVE_USERNAME=$1
54
+
55
+if [ ! $REMOVE_USERNAME ]; then
54 56
     echo $'Please specify a username to remove'
55 57
     exit 1
56 58
 fi
57 59
 
58
-if [[ $MY_USERNAME == 'git' || $MY_USERNAME == 'mirrors' ]]; then
59
-    echo $'Cannot remove reserved users'
60
+if [[ "$REMOVE_USERNAME" == "$MY_USERNAME" ]]; then
61
+    echo $'You cannot remove the administrator user'
60 62
     exit 2
61 63
 fi
62 64
 
63
-if [ ! -d /home/$MY_USERNAME ]; then
64
-    echo $"Home directory does not exist for $MY_USERNAME"
65
+if [[ $(is_valid_user "$REMOVE_USERNAME") == "0" ]]; then
66
+    echo $'Cannot remove reserved users'
65 67
     exit 3
66 68
 fi
67 69
 
70
+if [ ! -d /home/$REMOVE_USERNAME ]; then
71
+    echo $"Home directory does not exist for $REMOVE_USERNAME"
72
+    exit 4
73
+fi
74
+
68 75
 if [ ! -f $COMPLETION_FILE ]; then
69 76
     echo $"$COMPLETION_FILE not found"
70
-    exit 4
77
+    exit 5
71 78
 fi
72 79
 
73 80
 if ! grep -q "Admin user" $COMPLETION_FILE; then
74 81
     echo $"No admin user specified in $COMPLETION_FILE"
75
-    exit 5
82
+    exit 6
76 83
 fi
77 84
 
78 85
 ADMIN_USERNAME=$(get_completion_param "Admin user")
79 86
 if [ ! $ADMIN_USERNAME ]; then
80 87
     echo $"No admin username specified in $COMPLETION_FILE"
81
-    exit 6
88
+    exit 7
82 89
 fi
83 90
 
84
-if [[ $MY_USERNAME == $ADMIN_USERNAME ]]; then
91
+if [[ $REMOVE_USERNAME == $ADMIN_USERNAME ]]; then
85 92
     echo $"The administrator user cannot be removed"
86
-    exit 7
93
+    exit 8
87 94
 fi
88 95
 
89 96
 echo $'>>> REMOVE USER <<<'
90
-read -p $"Do you really wish to remove the user '$MY_USERNAME' (y/n) ?" yn
97
+read -p $"Do you really wish to remove the user '$REMOVE_USERNAME' (y/n) ?" yn
91 98
 if [[ $yn != 'y' && $yn != 'Y' && $yn != 'yes' && $yn != 'Yes' && $yn != 'YES' ]]; then
92
-    echo $"User $MY_USERNAME was not removed"
93
-    exit 8
99
+    echo $"User $REMOVE_USERNAME was not removed"
100
+    exit 9
94 101
 fi
95 102
 
96 103
 if [ -f /etc/nginx/.htpasswd ]; then
97
-    if grep "${MY_USERNAME}:" /etc/nginx/.htpasswd; then
98
-        htpasswd -D /etc/nginx/.htpasswd $MY_USERNAME
104
+    if grep "${REMOVE_USERNAME}:" /etc/nginx/.htpasswd; then
105
+        htpasswd -D /etc/nginx/.htpasswd $REMOVE_USERNAME
99 106
     fi
100 107
 fi
101 108
 
102 109
 # remove gpg keys
103
-if [ -d /home/$MY_USERNAME/.gnupg ]; then
104
-    shred -zu /home/$MY_USERNAME/.gnupg/*
110
+if [ -d /home/$REMOVE_USERNAME/.gnupg ]; then
111
+    shred -zu /home/$REMOVE_USERNAME/.gnupg/*
105 112
 fi
106 113
 
107 114
 # remove ssh keys
108
-if [ -d /home/$MY_USERNAME/.ssh ]; then
109
-    shred -zu /home/$MY_USERNAME/.ssh/*
115
+if [ -d /home/$REMOVE_USERNAME/.ssh ]; then
116
+    shred -zu /home/$REMOVE_USERNAME/.ssh/*
110 117
 fi
111 118
 
112 119
 echo $'Detecting installed apps...'
@@ -117,19 +124,19 @@ do
117 124
     if [[ $(function_exists remove_user_${app_name}) == "1" ]]; then
118 125
         echo $"Removing user from ${app_name}"
119 126
         app_load_variables ${app_name}
120
-        remove_user_${app_name} "$MY_USERNAME"
121
-        if grep -q "${app_name}_${MY_USERNAME}" $APP_USERS_FILE; then
122
-            sed -i "/${app_name}_${MY_USERNAME}/d" $APP_USERS_FILE
127
+        remove_user_${app_name} "$REMOVE_USERNAME"
128
+        if grep -q "${app_name}_${REMOVE_USERNAME}" $APP_USERS_FILE; then
129
+            sed -i "/${app_name}_${REMOVE_USERNAME}/d" $APP_USERS_FILE
123 130
         fi
124 131
     fi
125 132
 done
126 133
 
127
-userdel -r $MY_USERNAME
134
+userdel -r $REMOVE_USERNAME
128 135
 
129
-if [ -d /home/$MY_USERNAME ]; then
130
-    rm -rf /home/$MY_USERNAME
136
+if [ -d /home/$REMOVE_USERNAME ]; then
137
+    rm -rf /home/$REMOVE_USERNAME
131 138
 fi
132 139
 
133
-echo $"User $MY_USERNAME was removed"
140
+echo $"User $REMOVE_USERNAME was removed"
134 141
 
135 142
 exit 0