mkusers 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. #!/usr/bin/env bash
  2. set -e
  3. myname="${0##*/}"
  4. #----------------------------------------------------------------------------
  5. # Configurable items
  6. MIN_UID=1000
  7. MAX_UID=1999
  8. MIN_GID=1000
  9. MAX_GID=1999
  10. # No more is configurable below this point
  11. #----------------------------------------------------------------------------
  12. #----------------------------------------------------------------------------
  13. error() {
  14. local fmt="${1}"
  15. shift
  16. printf "%s: " "${myname}" >&2
  17. printf "${fmt}" "${@}" >&2
  18. }
  19. fail() {
  20. error "$@"
  21. exit 1
  22. }
  23. #----------------------------------------------------------------------------
  24. if [ ${#} -ne 2 ]; then
  25. fail "usage: %s USERS_TABLE TARGET_DIR\n"
  26. fi
  27. USERS_TABLE="${1}"
  28. TARGET_DIR="${2}"
  29. shift 2
  30. PASSWD="${TARGET_DIR}/etc/passwd"
  31. SHADOW="${TARGET_DIR}/etc/shadow"
  32. GROUP="${TARGET_DIR}/etc/group"
  33. # /etc/gshadow is not part of the standard skeleton, so not everybody
  34. # will have it, but some may hav it, and its content must be in sync
  35. # with /etc/group, so any use of gshadow must be conditional.
  36. GSHADOW="${TARGET_DIR}/etc/gshadow"
  37. # We can't simply source ${BR2_CONFIG} as it may contains constructs
  38. # such as:
  39. # BR2_DEFCONFIG="$(CONFIG_DIR)/defconfig"
  40. # which when sourced from a shell script will eventually try to execute
  41. # a command name 'CONFIG_DIR', which is plain wrong for virtually every
  42. # systems out there.
  43. # So, we have to scan that file instead. Sigh... :-(
  44. PASSWD_METHOD="$( sed -r -e '/^BR2_TARGET_GENERIC_PASSWD_METHOD="(.*)"$/!d;' \
  45. -e 's//\1/;' \
  46. "${BR2_CONFIG}" \
  47. )"
  48. #----------------------------------------------------------------------------
  49. get_uid() {
  50. local username="${1}"
  51. awk -F: -v username="${username}" \
  52. '$1 == username { printf( "%d\n", $3 ); }' "${PASSWD}"
  53. }
  54. #----------------------------------------------------------------------------
  55. get_ugid() {
  56. local username="${1}"
  57. awk -F: -v username="${username}" \
  58. '$1 == username { printf( "%d\n", $4 ); }' "${PASSWD}"
  59. }
  60. #----------------------------------------------------------------------------
  61. get_gid() {
  62. local group="${1}"
  63. awk -F: -v group="${group}" \
  64. '$1 == group { printf( "%d\n", $3 ); }' "${GROUP}"
  65. }
  66. #----------------------------------------------------------------------------
  67. get_username() {
  68. local uid="${1}"
  69. awk -F: -v uid="${uid}" \
  70. '$3 == uid { printf( "%s\n", $1 ); }' "${PASSWD}"
  71. }
  72. #----------------------------------------------------------------------------
  73. get_group() {
  74. local gid="${1}"
  75. awk -F: -v gid="${gid}" \
  76. '$3 == gid { printf( "%s\n", $1 ); }' "${GROUP}"
  77. }
  78. #----------------------------------------------------------------------------
  79. get_ugroup() {
  80. local username="${1}"
  81. local ugid
  82. ugid="$( get_ugid "${username}" )"
  83. if [ -n "${ugid}" ]; then
  84. get_group "${ugid}"
  85. fi
  86. }
  87. #----------------------------------------------------------------------------
  88. # Sanity-check the new user/group:
  89. # - check the gid is not already used for another group
  90. # - check the group does not already exist with another gid
  91. # - check the user does not already exist with another gid
  92. # - check the uid is not already used for another user
  93. # - check the user does not already exist with another uid
  94. # - check the user does not already exist in another group
  95. check_user_validity() {
  96. local username="${1}"
  97. local uid="${2}"
  98. local group="${3}"
  99. local gid="${4}"
  100. local _uid _ugid _gid _username _group _ugroup
  101. _group="$( get_group "${gid}" )"
  102. _gid="$( get_gid "${group}" )"
  103. _ugid="$( get_ugid "${username}" )"
  104. _username="$( get_username "${uid}" )"
  105. _uid="$( get_uid "${username}" )"
  106. _ugroup="$( get_ugroup "${username}" )"
  107. if [ "${username}" = "root" ]; then
  108. fail "invalid username '%s\n'" "${username}"
  109. fi
  110. if [ ${gid} -lt -1 -o ${gid} -eq 0 ]; then
  111. fail "invalid gid '%d'\n" ${gid}
  112. elif [ ${gid} -ne -1 ]; then
  113. # check the gid is not already used for another group
  114. if [ -n "${_group}" -a "${_group}" != "${group}" ]; then
  115. fail "gid is already used by group '${_group}'\n"
  116. fi
  117. # check the group does not already exists with another gid
  118. # Need to split the check in two, otherwise '[' complains it
  119. # is missing arguments when _gid is empty
  120. if [ -n "${_gid}" ] && [ ${_gid} -ne ${gid} ]; then
  121. fail "group already exists with gid '${_gid}'\n"
  122. fi
  123. # check the user does not already exists with another gid
  124. # Need to split the check in two, otherwise '[' complains it
  125. # is missing arguments when _ugid is empty
  126. if [ -n "${_ugid}" ] && [ ${_ugid} -ne ${gid} ]; then
  127. fail "user already exists with gid '${_ugid}'\n"
  128. fi
  129. fi
  130. if [ ${uid} -lt -1 -o ${uid} -eq 0 ]; then
  131. fail "invalid uid '%d'\n" ${uid}
  132. elif [ ${uid} -ne -1 ]; then
  133. # check the uid is not already used for another user
  134. if [ -n "${_username}" -a "${_username}" != "${username}" ]; then
  135. fail "uid is already used by user '${_username}'\n"
  136. fi
  137. # check the user does not already exists with another uid
  138. # Need to split the check in two, otherwise '[' complains it
  139. # is missing arguments when _uid is empty
  140. if [ -n "${_uid}" ] && [ ${_uid} -ne ${uid} ]; then
  141. fail "user already exists with uid '${_uid}'\n"
  142. fi
  143. fi
  144. # check the user does not already exist in another group
  145. if [ -n "${_ugroup}" -a "${_ugroup}" != "${group}" ]; then
  146. fail "user already exists with group '${_ugroup}'\n"
  147. fi
  148. return 0
  149. }
  150. #----------------------------------------------------------------------------
  151. # Generate a unique GID for given group. If the group already exists,
  152. # then simply report its current GID. Otherwise, generate the lowest GID
  153. # that is:
  154. # - not 0
  155. # - comprised in [MIN_GID..MAX_GID]
  156. # - not already used by a group
  157. generate_gid() {
  158. local group="${1}"
  159. local gid
  160. gid="$( get_gid "${group}" )"
  161. if [ -z "${gid}" ]; then
  162. for(( gid=MIN_GID; gid<=MAX_GID; gid++ )); do
  163. if [ -z "$( get_group "${gid}" )" ]; then
  164. break
  165. fi
  166. done
  167. if [ ${gid} -gt ${MAX_GID} ]; then
  168. fail "can not allocate a GID for group '%s'\n" "${group}"
  169. fi
  170. fi
  171. printf "%d\n" "${gid}"
  172. }
  173. #----------------------------------------------------------------------------
  174. # Add a group; if it does already exist, remove it first
  175. add_one_group() {
  176. local group="${1}"
  177. local gid="${2}"
  178. local _f
  179. # Generate a new GID if needed
  180. if [ ${gid} -eq -1 ]; then
  181. gid="$( generate_gid "${group}" )"
  182. fi
  183. # Remove any previous instance of this group, and re-add the new one
  184. sed -i -e '/^'"${group}"':.*/d;' "${GROUP}"
  185. printf "%s:x:%d:\n" "${group}" "${gid}" >>"${GROUP}"
  186. # Ditto for /etc/gshadow if it exists
  187. if [ -f "${GSHADOW}" ]; then
  188. sed -i -e '/^'"${group}"':.*/d;' "${GSHADOW}"
  189. printf "%s:*::\n" "${group}" >>"${GSHADOW}"
  190. fi
  191. }
  192. #----------------------------------------------------------------------------
  193. # Generate a unique UID for given username. If the username already exists,
  194. # then simply report its current UID. Otherwise, generate the lowest UID
  195. # that is:
  196. # - not 0
  197. # - comprised in [MIN_UID..MAX_UID]
  198. # - not already used by a user
  199. generate_uid() {
  200. local username="${1}"
  201. local uid
  202. uid="$( get_uid "${username}" )"
  203. if [ -z "${uid}" ]; then
  204. for(( uid=MIN_UID; uid<=MAX_UID; uid++ )); do
  205. if [ -z "$( get_username "${uid}" )" ]; then
  206. break
  207. fi
  208. done
  209. if [ ${uid} -gt ${MAX_UID} ]; then
  210. fail "can not allocate a UID for user '%s'\n" "${username}"
  211. fi
  212. fi
  213. printf "%d\n" "${uid}"
  214. }
  215. #----------------------------------------------------------------------------
  216. # Add given user to given group, if not already the case
  217. add_user_to_group() {
  218. local username="${1}"
  219. local group="${2}"
  220. local _f
  221. for _f in "${GROUP}" "${GSHADOW}"; do
  222. [ -f "${_f}" ] || continue
  223. sed -r -i -e 's/^('"${group}"':.*:)(([^:]+,)?)'"${username}"'(,[^:]+*)?$/\1\2\4/;' \
  224. -e 's/^('"${group}"':.*)$/\1,'"${username}"'/;' \
  225. -e 's/,+/,/' \
  226. -e 's/:,/:/' \
  227. "${_f}"
  228. done
  229. }
  230. #----------------------------------------------------------------------------
  231. # Encode a password
  232. encode_password() {
  233. local passwd="${1}"
  234. mkpasswd -m "${PASSWD_METHOD}" "${passwd}"
  235. }
  236. #----------------------------------------------------------------------------
  237. # Add a user; if it does already exist, remove it first
  238. add_one_user() {
  239. local username="${1}"
  240. local uid="${2}"
  241. local group="${3}"
  242. local gid="${4}"
  243. local passwd="${5}"
  244. local home="${6}"
  245. local shell="${7}"
  246. local groups="${8}"
  247. local comment="${9}"
  248. local _f _group _home _shell _gid _passwd
  249. # First, sanity-check the user
  250. check_user_validity "${username}" "${uid}" "${group}" "${gid}"
  251. # Generate a new UID if needed
  252. if [ ${uid} -eq -1 ]; then
  253. uid="$( generate_uid "${username}" )"
  254. fi
  255. # Remove any previous instance of this user
  256. for _f in "${PASSWD}" "${SHADOW}"; do
  257. sed -r -i -e '/^'"${username}"':.*/d;' "${_f}"
  258. done
  259. _gid="$( get_gid "${group}" )"
  260. _shell="${shell}"
  261. if [ "${shell}" = "-" ]; then
  262. _shell="/bin/false"
  263. fi
  264. case "${home}" in
  265. -) _home="/";;
  266. /) fail "home can not explicitly be '/'\n";;
  267. /*) _home="${home}";;
  268. *) fail "home must be an absolute path\n";;
  269. esac
  270. case "${passwd}" in
  271. !=*)
  272. _passwd='!'"$( encode_password "${passwd#!=}" )"
  273. ;;
  274. =*)
  275. _passwd="$( encode_password "${passwd#=}" )"
  276. ;;
  277. *)
  278. _passwd="${passwd}"
  279. ;;
  280. esac
  281. printf "%s:x:%d:%d:%s:%s:%s\n" \
  282. "${username}" "${uid}" "${_gid}" \
  283. "${comment}" "${_home}" "${_shell}" \
  284. >>"${PASSWD}"
  285. printf "%s:%s:::::::\n" \
  286. "${username}" "${_passwd}" \
  287. >>"${SHADOW}"
  288. # Add the user to its additional groups
  289. if [ "${groups}" != "-" ]; then
  290. for _group in ${groups//,/ }; do
  291. add_user_to_group "${username}" "${_group}"
  292. done
  293. fi
  294. # If the user has a home, chown it
  295. # (Note: stdout goes to the fakeroot-script)
  296. if [ "${home}" != "-" ]; then
  297. mkdir -p "${TARGET_DIR}/${home}"
  298. printf "chown -h -R %d:%d '%s'\n" "${uid}" "${_gid}" "${TARGET_DIR}/${home}"
  299. fi
  300. }
  301. #----------------------------------------------------------------------------
  302. main() {
  303. local username uid group gid passwd home shell groups comment
  304. # Some sanity checks
  305. if [ ${MIN_UID} -le 0 ]; then
  306. fail "MIN_UID must be >0 (currently %d)\n" ${MIN_UID}
  307. fi
  308. if [ ${MIN_GID} -le 0 ]; then
  309. fail "MIN_GID must be >0 (currently %d)\n" ${MIN_GID}
  310. fi
  311. # We first create groups whose gid is not -1, and then we create groups
  312. # whose gid is -1 (automatic), so that, if a group is defined both with
  313. # a specified gid and an automatic gid, we ensure the specified gid is
  314. # used, rather than a different automatic gid is computed.
  315. # First, create all the main groups which gid is *not* automatic
  316. while read username uid group gid passwd home shell groups comment; do
  317. [ -n "${username}" ] || continue # Package with no user
  318. [ ${gid} -ge 0 ] || continue # Automatic gid
  319. add_one_group "${group}" "${gid}"
  320. done <"${USERS_TABLE}"
  321. # Then, create all the main groups which gid *is* automatic
  322. while read username uid group gid passwd home shell groups comment; do
  323. [ -n "${username}" ] || continue # Package with no user
  324. [ ${gid} -eq -1 ] || continue # Non-automatic gid
  325. add_one_group "${group}" "${gid}"
  326. done <"${USERS_TABLE}"
  327. # Then, create all the additional groups
  328. # If any additional group is already a main group, we should use
  329. # the gid of that main group; otherwise, we can use any gid
  330. while read username uid group gid passwd home shell groups comment; do
  331. [ -n "${username}" ] || continue # Package with no user
  332. if [ "${groups}" != "-" ]; then
  333. for g in ${groups//,/ }; do
  334. add_one_group "${g}" -1
  335. done
  336. fi
  337. done <"${USERS_TABLE}"
  338. # When adding users, we do as for groups, in case two packages create
  339. # the same user, one with an automatic uid, the other with a specified
  340. # uid, to ensure the specified uid is used, rather than an incompatible
  341. # uid be generated.
  342. # Now, add users whose uid is *not* automatic
  343. while read username uid group gid passwd home shell groups comment; do
  344. [ -n "${username}" ] || continue # Package with no user
  345. [ "${username}" != "-" ] || continue # Magic string to skip user creation
  346. [ ${uid} -ge 0 ] || continue # Automatic uid
  347. add_one_user "${username}" "${uid}" "${group}" "${gid}" "${passwd}" \
  348. "${home}" "${shell}" "${groups}" "${comment}"
  349. done <"${USERS_TABLE}"
  350. # Finally, add users whose uid *is* automatic
  351. while read username uid group gid passwd home shell groups comment; do
  352. [ -n "${username}" ] || continue # Package with no user
  353. [ "${username}" != "-" ] || continue # Magic string to skip user creation
  354. [ ${uid} -eq -1 ] || continue # Non-automatic uid
  355. add_one_user "${username}" "${uid}" "${group}" "${gid}" "${passwd}" \
  356. "${home}" "${shell}" "${groups}" "${comment}"
  357. done <"${USERS_TABLE}"
  358. }
  359. #----------------------------------------------------------------------------
  360. main "${@}"