useradd_base.bbclass 5.3 KB

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