sys-process/glances: 3.1.4.1-r1 amd64 stable, bug #720368
[gentoo.git] / eclass / user-info.eclass
1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: user-info.eclass
5 # @MAINTAINER:
6 # base-system@gentoo.org (Linux)
7 # Michał Górny <mgorny@gentoo.org> (NetBSD)
8 # @BLURB: Read-only access to user and group information
9
10 if [[ -z ${_USER_INFO_ECLASS} ]]; then
11 _USER_INFO_ECLASS=1
12
13 # @FUNCTION: egetent
14 # @USAGE: <database> <key>
15 # @DESCRIPTION:
16 # Small wrapper for getent (Linux), nidump (< Mac OS X 10.5),
17 # dscl (Mac OS X 10.5), and pw (FreeBSD) used in enewuser()/enewgroup().
18 #
19 # Supported databases: group passwd
20 egetent() {
21         local db=$1 key=$2
22
23         [[ $# -ge 3 ]] && die "usage: egetent <database> <key>"
24
25         case ${db} in
26         passwd|group) ;;
27         *) die "sorry, database '${db}' not yet supported; file a bug" ;;
28         esac
29
30         case ${CHOST} in
31         *-freebsd*|*-dragonfly*)
32                 case ${db} in
33                 passwd) db="user" ;;
34                 *) ;;
35                 esac
36
37                 # lookup by uid/gid
38                 local opts
39                 if [[ ${key} == [[:digit:]]* ]] ; then
40                         [[ ${db} == "user" ]] && opts="-u" || opts="-g"
41                 fi
42
43                 pw show ${db} ${opts} "${key}" -q
44                 ;;
45         *-openbsd*)
46                 grep "${key}:\*:" /etc/${db}
47                 ;;
48         *)
49                 # ignore nscd output if we're not running as root
50                 type -p nscd >/dev/null && nscd -i "${db}" 2>/dev/null
51                 getent "${db}" "${key}"
52                 ;;
53         esac
54 }
55
56 # @FUNCTION: egetusername
57 # @USAGE: <uid>
58 # @DESCRIPTION:
59 # Gets the username for given UID.
60 egetusername() {
61         [[ $# -eq 1 ]] || die "usage: egetusername <uid>"
62
63         egetent passwd "$1" | cut -d: -f1
64 }
65
66 # @FUNCTION: egetgroupname
67 # @USAGE: <gid>
68 # @DESCRIPTION:
69 # Gets the group name for given GID.
70 egetgroupname() {
71         [[ $# -eq 1 ]] || die "usage: egetgroupname <gid>"
72
73         egetent group "$1" | cut -d: -f1
74 }
75
76 # @FUNCTION: egethome
77 # @USAGE: <user>
78 # @DESCRIPTION:
79 # Gets the home directory for the specified user.
80 egethome() {
81         local pos
82
83         [[ $# -eq 1 ]] || die "usage: egethome <user>"
84
85         case ${CHOST} in
86         *-freebsd*|*-dragonfly*)
87                 pos=9
88                 ;;
89         *)      # Linux, NetBSD, OpenBSD, etc...
90                 pos=6
91                 ;;
92         esac
93
94         egetent passwd "$1" | cut -d: -f${pos}
95 }
96
97 # @FUNCTION: egetshell
98 # @USAGE: <user>
99 # @DESCRIPTION:
100 # Gets the shell for the specified user.
101 egetshell() {
102         local pos
103
104         [[ $# -eq 1 ]] || die "usage: egetshell <user>"
105
106         case ${CHOST} in
107         *-freebsd*|*-dragonfly*)
108                 pos=10
109                 ;;
110         *)      # Linux, NetBSD, OpenBSD, etc...
111                 pos=7
112                 ;;
113         esac
114
115         egetent passwd "$1" | cut -d: -f${pos}
116 }
117
118 # @FUNCTION: egetcomment
119 # @USAGE: <user>
120 # @DESCRIPTION:
121 # Gets the comment field for the specified user.
122 egetcomment() {
123         local pos
124
125         [[ $# -eq 1 ]] || die "usage: egetcomment <user>"
126
127         case ${CHOST} in
128         *-freebsd*|*-dragonfly*)
129                 pos=8
130                 ;;
131         *)      # Linux, NetBSD, OpenBSD, etc...
132                 pos=5
133                 ;;
134         esac
135
136         egetent passwd "$1" | cut -d: -f${pos}
137 }
138
139 # @FUNCTION: egetgroups
140 # @USAGE: <user>
141 # @DESCRIPTION:
142 # Gets all the groups user belongs to.  The primary group is returned
143 # first, then all supplementary groups.  Groups are ','-separated.
144 egetgroups() {
145         [[ $# -eq 1 ]] || die "usage: egetgroups <user>"
146
147         local egroups_arr
148         read -r -a egroups_arr < <(id -G -n "$1")
149
150         local g groups=${egroups_arr[0]}
151         # sort supplementary groups to make comparison possible
152         while read -r g; do
153                 [[ -n ${g} ]] && groups+=",${g}"
154         done < <(printf '%s\n' "${egroups_arr[@]:1}" | sort)
155         echo "${groups}"
156 }
157
158 fi