dev-qt/qtopengl: stable 5.14.2 for ppc, bug #719732
[gentoo.git] / eclass / pam.eclass
1 # Copyright 1999-2019 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: pam.eclass
5 # @MAINTAINER:
6 # Mikle Kolyada <zlogene@gentoo.org>
7 # @AUTHOR:
8 # Diego Pettenò <flameeyes@gentoo.org>
9 # @BLURB: Handles pam related tasks
10 # @DESCRIPTION:
11 # This eclass contains functions to install pamd configuration files and
12 # pam modules.
13
14 if [[ -z ${_PAM_ECLASS} ]]; then
15 _PAM_ECLASS=1
16
17 inherit flag-o-matic multilib
18
19 # @FUNCTION: dopamd
20 # @USAGE: <file> [more files]
21 # @DESCRIPTION:
22 # Install pam auth config file in /etc/pam.d
23 dopamd() {
24         [[ -z $1 ]] && die "dopamd requires at least one argument"
25
26         if has pam ${IUSE} && ! use pam; then
27                 return 0;
28         fi
29
30         ( # dont want to pollute calling env
31                 insinto /etc/pam.d
32                 insopts -m 0644
33                 doins "$@"
34         ) || die "failed to install $@"
35         cleanpamd "$@"
36 }
37
38 # @FUNCTION: newpamd
39 # @USAGE: <old name> <new name>
40 # @DESCRIPTION:
41 # Install pam file <old name> as <new name> in /etc/pam.d
42 newpamd() {
43         [[ $# -ne 2 ]] && die "newpamd requires two arguments"
44
45         if has pam ${IUSE} && ! use pam; then
46                 return 0;
47         fi
48
49         ( # dont want to pollute calling env
50                 insinto /etc/pam.d
51                 insopts -m 0644
52                 newins "$1" "$2"
53         ) || die "failed to install $1 as $2"
54         cleanpamd $2
55 }
56
57 # @FUNCTION: dopamsecurity
58 # @USAGE: <section> <file> [more files]
59 # @DESCRIPTION:
60 # Installs the config files in /etc/security/<section>/
61 dopamsecurity() {
62         [[ $# -lt 2 ]] && die "dopamsecurity requires at least two arguments"
63
64         if has pam ${IUSE} && ! use pam; then
65                 return 0
66         fi
67
68         ( # dont want to pollute calling env
69                 insinto /etc/security/$1
70                 insopts -m 0644
71                 doins "${@:2}"
72         ) || die "failed to install ${@:2}"
73 }
74
75 # @FUNCTION: newpamsecurity
76 # @USAGE: <section> <old name> <new name>
77 # @DESCRIPTION:
78 # Installs the config file <old name> as <new name> in /etc/security/<section>/
79 newpamsecurity() {
80         [[ $# -ne 3 ]] && die "newpamsecurity requires three arguments"
81
82         if has pam ${IUSE} && ! use pam; then
83                 return 0;
84         fi
85
86         ( # dont want to pollute calling env
87                 insinto /etc/security/$1
88                 insopts -m 0644
89                 newins "$2" "$3"
90         ) || die "failed to install $2 as $3"
91 }
92
93 # @FUNCTION: getpam_mod_dir
94 # @DESCRIPTION:
95 # Returns the pam modules' directory for current implementation
96 getpam_mod_dir() {
97         if has_version sys-libs/pam; then
98                 PAM_MOD_DIR=/$(get_libdir)/security
99         else
100                 # Unable to find PAM implementation... defaulting
101                 PAM_MOD_DIR=/$(get_libdir)/security
102         fi
103
104         echo ${PAM_MOD_DIR}
105 }
106
107 # @FUNCTION: pammod_hide_symbols
108 # @DESCRIPTION:
109 # Hide all non-PAM-used symbols from the module; this function creates a
110 # simple ld version script that hides all the symbols that are not
111 # necessary for PAM to load the module, then uses append-flags to make
112 # sure that it gets used.
113 pammod_hide_symbols() {
114         cat - > "${T}"/pam-eclass-pam_symbols.ver <<EOF
115 {
116         global: pam_sm_*;
117         local: *;
118 };
119 EOF
120
121         append-ldflags -Wl,--version-script="${T}"/pam-eclass-pam_symbols.ver
122 }
123
124 # @FUNCTION: dopammod
125 # @USAGE: <file> [more files]
126 # @DESCRIPTION:
127 # Install pam module file in the pam modules' dir for current implementation
128 dopammod() {
129         [[ -z $1 ]] && die "dopammod requires at least one argument"
130
131         if has pam ${IUSE} && ! use pam; then
132                 return 0;
133         fi
134
135         exeinto $(getpam_mod_dir)
136         doexe "$@" || die "failed to install $@"
137 }
138
139 # @FUNCTION: newpammod
140 # @USAGE: <old name> <new name>
141 # @DESCRIPTION:
142 # Install pam module file <old name> as <new name> in the pam
143 # modules' dir for current implementation
144 newpammod() {
145         [[ $# -ne 2 ]] && die "newpammod requires two arguements"
146
147         if has pam ${IUSE} && ! use pam; then
148                 return 0;
149         fi
150
151         exeinto $(getpam_mod_dir)
152         newexe "$1" "$2" || die "failed to install $1 as $2"
153 }
154
155 # @FUNCTION: pamd_mimic_system
156 # @USAGE: <pamd file> [auth levels]
157 # @DESCRIPTION:
158 # This function creates a pamd file which mimics system-auth file
159 # for the given levels in the /etc/pam.d directory.
160 pamd_mimic_system() {
161         [[ $# -lt 2 ]] && die "pamd_mimic_system requires at least two argments"
162         pamd_mimic system-auth "$@"
163 }
164
165 # @FUNCTION: pamd_mimic
166 # @USAGE: <stack> <pamd file> [auth levels]
167 # @DESCRIPTION:
168 # This function creates a pamd file which mimics the given stack
169 # for the given levels in the /etc/pam.d directory.
170 pamd_mimic() {
171         [[ $# -lt 3 ]] && die "pamd_mimic requires at least three argments"
172
173         if has pam ${IUSE} && ! use pam; then
174                 return 0;
175         fi
176
177         dodir /etc/pam.d
178         pamdfile=${D}/etc/pam.d/$2
179         echo -e "# File autogenerated by pamd_mimic in pam eclass\n\n" >> \
180                 $pamdfile
181
182         originalstack=$1
183         authlevels="auth account password session"
184
185         if has_version '<sys-libs/pam-0.78'; then
186                 mimic="\trequired\t\tpam_stack.so service=${originalstack}"
187         else
188                 mimic="\tinclude\t\t${originalstack}"
189         fi
190
191         shift; shift
192
193         while [[ -n $1 ]]; do
194                 has $1 ${authlevels} || die "unknown level type"
195
196                 echo -e "$1${mimic}" >> ${pamdfile}
197
198                 shift
199         done
200 }
201
202 # @FUNCTION: cleanpamd
203 # @USAGE: <pamd file>
204 # @DESCRIPTION:
205 # Cleans a pam.d file from modules that might not be present on the system
206 # where it's going to be installed
207 cleanpamd() {
208         while [[ -n $1 ]]; do
209                 if ! has_version sys-libs/pam; then
210                         sed -i -e '/pam_shells\|pam_console/s:^:#:' "${D}/etc/pam.d/$1"
211                 fi
212
213                 shift
214         done
215 }
216
217 # @FUNCTION: pam_epam_expand
218 # @USAGE: <pamd file>
219 # @DESCRIPTION:
220 # Steer clear, deprecated, don't use, bad experiment
221 pam_epam_expand() {
222         sed -n -e 's|#%EPAM-\([[:alpha:]-]\+\):\([-+<>=/.![:alnum:]]\+\)%#.*|\1 \2|p' \
223         "$@" | sort -u | while read condition parameter; do
224
225         disable="yes"
226
227         case "$condition" in
228                 If-Has)
229                 message="This can be used only if you have ${parameter} installed"
230                 has_version "$parameter" && disable="no"
231                 ;;
232                 Use-Flag)
233                 message="This can be used only if you enabled the ${parameter} USE flag"
234                 use "$parameter" && disable="no"
235                 ;;
236                 *)
237                 eerror "Unknown EPAM condition '${condition}' ('${parameter}')"
238                 die "Unknown EPAM condition '${condition}' ('${parameter}')"
239                 ;;
240         esac
241
242         if [ "${disable}" = "yes" ]; then
243                 sed -i -e "/#%EPAM-${condition}:${parameter/\//\\/}%#/d" "$@"
244         else
245                 sed -i -e "s|#%EPAM-${condition}:${parameter}%#||" "$@"
246         fi
247
248         done
249 }
250
251 # Think about it before uncommenting this one, for now run it by hand
252 # pam_pkg_preinst() {
253 #       eshopts_push -o noglob # so that bash doen't expand "*"
254 #
255 #       pam_epam_expand "${D}"/etc/pam.d/*
256 #
257 #       eshopts_pop # reset old shell opts
258 # }
259
260 fi