useradd_base.bbclass 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #
  2. # Copyright OpenEmbedded Contributors
  3. #
  4. # SPDX-License-Identifier: MIT
  5. #
  6. # This bbclass provides basic functionality for user/group settings.
  7. # This bbclass is intended to be inherited by useradd.bbclass and
  8. # extrausers.bbclass.
  9. # The following functions basically have similar logic.
  10. # *) Perform necessary checks before invoking the actual command
  11. # *) Invoke the actual command with flock
  12. # *) Error out if an error occurs.
  13. # Note that before invoking these functions, make sure the global variable
  14. # PSEUDO is set up correctly.
  15. perform_groupadd () {
  16. local rootdir="$1"
  17. local opts="$2"
  18. bbnote "${PN}: Performing groupadd with [$opts]"
  19. local groupname=`echo "$opts" | awk '{ print $NF }'`
  20. local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
  21. if test "x$group_exists" = "x"; then
  22. eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO groupadd \$opts\" || true
  23. group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
  24. if test "x$group_exists" = "x"; then
  25. bbfatal "${PN}: groupadd command did not succeed."
  26. fi
  27. else
  28. bbnote "${PN}: group $groupname already exists, not re-creating it"
  29. fi
  30. }
  31. perform_useradd () {
  32. local rootdir="$1"
  33. local opts="$2"
  34. bbnote "${PN}: Performing useradd with [$opts]"
  35. local username=`echo "$opts" | awk '{ print $NF }'`
  36. local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
  37. if test "x$user_exists" = "x"; then
  38. eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO useradd \$opts\" || true
  39. user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
  40. if test "x$user_exists" = "x"; then
  41. bbfatal "${PN}: useradd command did not succeed."
  42. fi
  43. else
  44. bbnote "${PN}: user $username already exists, not re-creating it"
  45. fi
  46. }
  47. perform_groupmems () {
  48. local rootdir="$1"
  49. local opts="$2"
  50. bbnote "${PN}: Performing groupmems with [$opts]"
  51. local groupname=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-g" || $i == "--group") print $(i+1) }'`
  52. local username=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-a" || $i == "--add") print $(i+1) }'`
  53. bbnote "${PN}: Running groupmems command with group $groupname and user $username"
  54. local mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*$" $rootdir/etc/group || true`"
  55. if test "x$mem_exists" = "x"; then
  56. eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO groupmems \$opts\" || true
  57. mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*$" $rootdir/etc/group || true`"
  58. if test "x$mem_exists" = "x"; then
  59. bbfatal "${PN}: groupmems command did not succeed."
  60. fi
  61. else
  62. bbnote "${PN}: group $groupname already contains $username, not re-adding it"
  63. fi
  64. }
  65. perform_groupdel () {
  66. local rootdir="$1"
  67. local opts="$2"
  68. bbnote "${PN}: Performing groupdel with [$opts]"
  69. local groupname=`echo "$opts" | awk '{ print $NF }'`
  70. local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
  71. if test "x$group_exists" != "x"; then
  72. local awk_input='BEGIN {FS=":"}; $1=="'$groupname'" { print $3 }'
  73. local groupid=`echo "$awk_input" | awk -f- $rootdir/etc/group`
  74. local awk_check_users='BEGIN {FS=":"}; $4=="'$groupid'" {print $1}'
  75. local other_users=`echo "$awk_check_users" | awk -f- $rootdir/etc/passwd`
  76. if test "x$other_users" = "x"; then
  77. eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO groupdel \$opts\" || true
  78. group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
  79. if test "x$group_exists" != "x"; then
  80. bbfatal "${PN}: groupdel command did not succeed."
  81. fi
  82. else
  83. bbnote "${PN}: '$groupname' is primary group for users '$other_users', not removing it"
  84. fi
  85. else
  86. bbnote "${PN}: group $groupname doesn't exist, not removing it"
  87. fi
  88. }
  89. perform_userdel () {
  90. local rootdir="$1"
  91. local opts="$2"
  92. bbnote "${PN}: Performing userdel with [$opts]"
  93. local username=`echo "$opts" | awk '{ print $NF }'`
  94. local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
  95. if test "x$user_exists" != "x"; then
  96. eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO userdel \$opts\" || true
  97. user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
  98. if test "x$user_exists" != "x"; then
  99. bbfatal "${PN}: userdel command did not succeed."
  100. fi
  101. else
  102. bbnote "${PN}: user $username doesn't exist, not removing it"
  103. fi
  104. }
  105. perform_groupmod () {
  106. # Other than the return value of groupmod, there's no simple way to judge whether the command
  107. # succeeds, so we disable -e option temporarily
  108. set +e
  109. local rootdir="$1"
  110. local opts="$2"
  111. bbnote "${PN}: Performing groupmod with [$opts]"
  112. local groupname=`echo "$opts" | awk '{ print $NF }'`
  113. local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
  114. if test "x$group_exists" != "x"; then
  115. eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO groupmod \$opts\"
  116. if test $? != 0; then
  117. bbwarn "${PN}: groupmod command did not succeed."
  118. fi
  119. else
  120. bbwarn "${PN}: group $groupname doesn't exist, unable to modify it"
  121. fi
  122. set -e
  123. }
  124. perform_usermod () {
  125. # Same reason with groupmod, temporarily disable -e option
  126. set +e
  127. local rootdir="$1"
  128. local opts="$2"
  129. bbnote "${PN}: Performing usermod with [$opts]"
  130. local username=`echo "$opts" | awk '{ print $NF }'`
  131. local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
  132. if test "x$user_exists" != "x"; then
  133. eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO usermod \$opts\"
  134. if test $? != 0; then
  135. bbfatal "${PN}: usermod command did not succeed."
  136. fi
  137. else
  138. bbwarn "${PN}: user $username doesn't exist, unable to modify it"
  139. fi
  140. set -e
  141. }
  142. perform_passwd_expire () {
  143. local rootdir="$1"
  144. local opts="$2"
  145. bbnote "${PN}: Performing equivalent of passwd --expire with [$opts]"
  146. # Directly set sp_lstchg to 0 without using the passwd command: Only root can do that
  147. local username=`echo "$opts" | awk '{ print $NF }'`
  148. local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
  149. if test "x$user_exists" != "x"; then
  150. eval flock -x $rootdir${sysconfdir} -c \"$PSEUDO sed --follow-symlinks -i \''s/^\('$username':[^:]*\):[^:]*:/\1:0:/'\' $rootdir/etc/shadow \" || true
  151. local passwd_lastchanged="`grep "^$username:" $rootdir/etc/shadow | cut -d: -f3`"
  152. if test "x$passwd_lastchanged" != "x0"; then
  153. bbfatal "${PN}: passwd --expire operation did not succeed."
  154. fi
  155. else
  156. bbnote "${PN}: user $username doesn't exist, not expiring its password"
  157. fi
  158. }