-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathuser.sh
More file actions
192 lines (163 loc) · 3.83 KB
/
Copy pathuser.sh
File metadata and controls
192 lines (163 loc) · 3.83 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/bin/bash
# Copyright 2018 Juliano Santos [SHAMAN]
#
# This file is part of bashsrc.
#
# bashsrc 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.
#
# bashsrc 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 bashsrc. If not, see <http://www.gnu.org/licenses/>.
[ -v __USER_SH__ ] && return 0
readonly __USER_SH__=1
source builtin.sh
# .FUNCTION user.getuid -> [uint]|[bool]
#
# Retorna o id do usuário atual.
#
function user.getuid()
{
getopt.parse 0 "$@"
echo $UID
return $?
}
# .FUNCTION user.geteuid -> [uint]|[bool]
#
# Retorna o id efetivo do usuário atual.
#
function user.geteuid()
{
getopt.parse 0 "$@"
echo $EUID
return $?
}
# .FUNCTION user.getpwall -> [uint]|[bool]
#
# Retorna todos os usuários do sistema.
#
function user.getpwall
{
getopt.parse 0 "$@"
local user
while IFS=':' read user _; do
echo $user
done < /etc/passwd
return $?
}
# .FUNCTION user.getpwnam <name[str]> <pwd[map]> -> [bool]
#
# Obtem informações do usuário.
#
# == EXEMPLO ==
#
# source user.sh
#
# # Mapa
# declare -A info=()
#
# user.getpwname 'colord' info
#
# echo ${info[pw_uid]}
# echo ${info[pw_name]}
# echo ${info[pw_dir]}
# echo ${info[pw_gecos]}
#
# == SAÍDA ==
#
# 113
# colord
# /var/lib/colord
# colord colour management daemon,,,
#
function user.getpwnam()
{
getopt.parse 2 "name:str:$1" "pwd:map:$2" "${@:3}"
local __name__ __passwd__ __uid__ __gid__
local __gecos__ __dir__ __shell__
local -n __ref__=$2
__ref__=() || return 1
while IFS=':' read __name__ \
__passwd__ \
__uid__ \
__gid__ \
__gecos__ \
__dir__ \
__shell__; do
[[ $1 == $__name__ ]] && break
done < /etc/passwd
(($?)) && { error.error "'$1' usuário não encontrado"; return $?; }
__ref__[pw_name]=$__name__
__ref__[pw_passwd]=$__passwd__
__ref__[pw_uid]=$__uid__
__ref__[pw_gid]=$__gid__
__ref__[pw_gecos]=$__gecos__
__ref__[pw_dir]=$__dir__
__ref__[pw_shell]=$__shell__
return $?
}
# .FUNCTION user.getpwuid <uid[uint]> <pwd[map]> -> [bool]
#
# Obtém informações do uid especificado.
#
# Inicializa o mapa 'S' com as chaves:
#
# S[pw_name]
# S[pw_passwd]
# S[pw_uid]
# S[pw_gid]
# S[pw_gecos]
# S[pw_dir]
# S[pw_shell]
#
function user.getpwuid()
{
getopt.parse 2 "uid:uint:$1" "pwd:map:$2" "${@:3}"
local __name__ __passwd__ __uid__ __gid__
local __gecos__ __dir__ __shell__
local -n __ref__=$2
__ref__=() || return 1
while IFS=':' read __name__ \
__passwd__ \
__uid__ \
__gid__ \
__gecos__ \
__dir__ \
__shell__; do
[[ $1 == $__uid__ ]] && break
done < /etc/passwd
(($?)) && { error.error "'$1' id não encontrado"; return $?; }
__ref__[pw_name]=$__name__
__ref__[pw_passwd]=$__passwd__
__ref__[pw_uid]=$__uid__
__ref__[pw_gid]=$__gid__
__ref__[pw_gecos]=$__gecos__
__ref__[pw_dir]=$__dir__
__ref__[pw_shell]=$__shell__
return $?
}
# .MAP pwd
#
# Chaves:
#
# pw_name /* Nome do usuário do sistema */
# pw_passwd /* Senha criptografada ou asteriscos. */
# pw_uid /* Identificação númerica do usuário */
# pw_gid /* Identificação númerica do grupo primário */
# pw_gecos /* Informações do usuário (opcional) */
# pw_dir /* Diretório do usuário ($HOME). */
# pw_shell /* Interpretador de comandos */
#
# Funções (somente leitura)
readonly -f user.getuid \
user.geteuid \
user.getpwall \
user.getpwnam \
user.getpwuid
# /* __USER_SH__ */