Added seed code for caas-registry.
[ta/caas-registry.git] / docker-build / registry / security-utils / set-nologin-shell-to-system-users.sh
1 #!/bin/sh
2 # Copyright 2019 Nokia
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 . $(dirname "$0")/utils.sh
17
18 is_nologin_shell () {
19     shell=$1
20
21     set -- "/sbin/nologin" "/bin/sync" "/sbin/halt" "/sbin/shutdown"
22     for no_login_shell
23     do
24         if [ "$no_login_shell" = "$shell" ]
25         then
26             return 1;
27         fi
28     done
29     return 0;
30 }
31
32 set_nologin_shell () {
33     account=$1
34
35     name=$(echo "$account" | cut -d: -f1)
36     uid=$(echo "$account" | cut -d: -f3)
37     gid=$(echo "$account" | cut -d: -f4)
38     gecos=$(echo "$account" | cut -d: -f5)
39     home_dir=$(echo "$account" | cut -d: -f6)
40
41     del_user "$name" > /dev/null 2&>1
42     group_name=$(get_group_name "$gid")
43     if [ -z $group_name ]
44     then
45         group_command=""
46     else
47         group_command="-G $group_name"
48     fi
49     adduser -D -h "$home_dir" -g "$gecos" -s /sbin/nologin $group_command -u "$uid" "$name"
50
51 }
52
53 main () {
54     while read -r account
55     do
56         name=$(echo "$account" | cut -d: -f1)
57         if [ "$name" = "root" ]
58         then
59             continue;
60         fi
61
62         shell=$(echo "$account" | cut -d: -f7)
63         if is_nologin_shell "$shell"
64         then
65             set_nologin_shell "$account"
66         fi
67     done < /etc/passwd
68
69     if [[ `ls -ld /root | awk '{print $3"\n"$4}' | grep -v root` ]]
70     then
71         chown root:root /root
72     fi
73
74 }
75
76 main