pam.eclass 6.1 KB

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