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