mkusers 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 have 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 named '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' for '%s'\n" ${gid} "${username}"
  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 '%d' for '%s' is already used by group '%s'\n" \
  116. ${gid} "${username}" "${_group}"
  117. fi
  118. # check the group does not already exists with another gid
  119. # Need to split the check in two, otherwise '[' complains it
  120. # is missing arguments when _gid is empty
  121. if [ -n "${_gid}" ] && [ ${_gid} -ne ${gid} ]; then
  122. fail "group '%s' for '%s' already exists with gid '%d' (wants '%d')\n" \
  123. "${group}" "${username}" ${_gid} ${gid}
  124. fi
  125. # check the user does not already exists with another gid
  126. # Need to split the check in two, otherwise '[' complains it
  127. # is missing arguments when _ugid is empty
  128. if [ -n "${_ugid}" ] && [ ${_ugid} -ne ${gid} ]; then
  129. fail "user '%s' already exists with gid '%d' (wants '%d')\n" \
  130. "${username}" ${_ugid} ${gid}
  131. fi
  132. fi
  133. if [ ${uid} -lt -1 -o ${uid} -eq 0 ]; then
  134. fail "invalid uid '%d' for '%s'\n" ${uid} "${username}"
  135. elif [ ${uid} -ne -1 ]; then
  136. # check the uid is not already used for another user
  137. if [ -n "${_username}" -a "${_username}" != "${username}" ]; then
  138. fail "uid '%d' for '%s' already used by user '%s'\n" \
  139. ${uid} "${username}" "${_username}"
  140. fi
  141. # check the user does not already exists with another uid
  142. # Need to split the check in two, otherwise '[' complains it
  143. # is missing arguments when _uid is empty
  144. if [ -n "${_uid}" ] && [ ${_uid} -ne ${uid} ]; then
  145. fail "user '%s' already exists with uid '%d' (wants '%d')\n" \
  146. "${username}" ${_uid} ${uid}
  147. fi
  148. fi
  149. # check the user does not already exist in another group
  150. if [ -n "${_ugroup}" -a "${_ugroup}" != "${group}" ]; then
  151. fail "user '%s' already exists with group '%s' (wants '%s')\n" \
  152. "${username}" "${_ugroup}" "${group}"
  153. fi
  154. return 0
  155. }
  156. #----------------------------------------------------------------------------
  157. # Generate a unique GID for given group. If the group already exists,
  158. # then simply report its current GID. Otherwise, generate the lowest GID
  159. # that is:
  160. # - not 0
  161. # - comprised in [MIN_GID..MAX_GID]
  162. # - not already used by a group
  163. generate_gid() {
  164. local group="${1}"
  165. local gid
  166. gid="$( get_gid "${group}" )"
  167. if [ -z "${gid}" ]; then
  168. for(( gid=MIN_GID; gid<=MAX_GID; gid++ )); do
  169. if [ -z "$( get_group "${gid}" )" ]; then
  170. break
  171. fi
  172. done
  173. if [ ${gid} -gt ${MAX_GID} ]; then
  174. fail "can not allocate a GID for group '%s'\n" "${group}"
  175. fi
  176. fi
  177. printf "%d\n" "${gid}"
  178. }
  179. #----------------------------------------------------------------------------
  180. # Add a group; if it does already exist, remove it first
  181. add_one_group() {
  182. local group="${1}"
  183. local gid="${2}"
  184. local _f
  185. # Generate a new GID if needed
  186. if [ ${gid} -eq -1 ]; then
  187. gid="$( generate_gid "${group}" )"
  188. fi
  189. # Remove any previous instance of this group, and re-add the new one
  190. sed -i --follow-symlinks -e '/^'"${group}"':.*/d;' "${GROUP}"
  191. printf "%s:x:%d:\n" "${group}" "${gid}" >>"${GROUP}"
  192. # Ditto for /etc/gshadow if it exists
  193. if [ -f "${GSHADOW}" ]; then
  194. sed -i --follow-symlinks -e '/^'"${group}"':.*/d;' "${GSHADOW}"
  195. printf "%s:*::\n" "${group}" >>"${GSHADOW}"
  196. fi
  197. }
  198. #----------------------------------------------------------------------------
  199. # Generate a unique UID for given username. If the username already exists,
  200. # then simply report its current UID. Otherwise, generate the lowest UID
  201. # that is:
  202. # - not 0
  203. # - comprised in [MIN_UID..MAX_UID]
  204. # - not already used by a user
  205. generate_uid() {
  206. local username="${1}"
  207. local uid
  208. uid="$( get_uid "${username}" )"
  209. if [ -z "${uid}" ]; then
  210. for(( uid=MIN_UID; uid<=MAX_UID; uid++ )); do
  211. if [ -z "$( get_username "${uid}" )" ]; then
  212. break
  213. fi
  214. done
  215. if [ ${uid} -gt ${MAX_UID} ]; then
  216. fail "can not allocate a UID for user '%s'\n" "${username}"
  217. fi
  218. fi
  219. printf "%d\n" "${uid}"
  220. }
  221. #----------------------------------------------------------------------------
  222. # Add given user to given group, if not already the case
  223. add_user_to_group() {
  224. local username="${1}"
  225. local group="${2}"
  226. local _f
  227. for _f in "${GROUP}" "${GSHADOW}"; do
  228. [ -f "${_f}" ] || continue
  229. sed -r -i --follow-symlinks \
  230. -e 's/^('"${group}"':.*:)(([^:]+,)?)'"${username}"'(,[^:]+*)?$/\1\2\4/;' \
  231. -e 's/^('"${group}"':.*)$/\1,'"${username}"'/;' \
  232. -e 's/,+/,/' \
  233. -e 's/:,/:/' \
  234. "${_f}"
  235. done
  236. }
  237. #----------------------------------------------------------------------------
  238. # Encode a password
  239. encode_password() {
  240. local passwd="${1}"
  241. mkpasswd -m "${PASSWD_METHOD}" "${passwd}"
  242. }
  243. #----------------------------------------------------------------------------
  244. # Add a user; if it does already exist, remove it first
  245. add_one_user() {
  246. local username="${1}"
  247. local uid="${2}"
  248. local group="${3}"
  249. local gid="${4}"
  250. local passwd="${5}"
  251. local home="${6}"
  252. local shell="${7}"
  253. local groups="${8}"
  254. local comment="${9}"
  255. local _f _group _home _shell _gid _passwd
  256. # First, sanity-check the user
  257. check_user_validity "${username}" "${uid}" "${group}" "${gid}"
  258. # Generate a new UID if needed
  259. if [ ${uid} -eq -1 ]; then
  260. uid="$( generate_uid "${username}" )"
  261. fi
  262. # Remove any previous instance of this user
  263. for _f in "${PASSWD}" "${SHADOW}"; do
  264. sed -r -i --follow-symlinks -e '/^'"${username}"':.*/d;' "${_f}"
  265. done
  266. _gid="$( get_gid "${group}" )"
  267. _shell="${shell}"
  268. if [ "${shell}" = "-" ]; then
  269. _shell="/bin/false"
  270. fi
  271. case "${home}" in
  272. -) _home="/";;
  273. /) fail "home can not explicitly be '/'\n";;
  274. /*) _home="${home}";;
  275. *) fail "home must be an absolute path\n";;
  276. esac
  277. case "${passwd}" in
  278. -)
  279. _passwd=""
  280. ;;
  281. !=*)
  282. _passwd='!'"$( encode_password "${passwd#!=}" )"
  283. ;;
  284. =*)
  285. _passwd="$( encode_password "${passwd#=}" )"
  286. ;;
  287. *)
  288. _passwd="${passwd}"
  289. ;;
  290. esac
  291. printf "%s:x:%d:%d:%s:%s:%s\n" \
  292. "${username}" "${uid}" "${_gid}" \
  293. "${comment}" "${_home}" "${_shell}" \
  294. >>"${PASSWD}"
  295. printf "%s:%s:::::::\n" \
  296. "${username}" "${_passwd}" \
  297. >>"${SHADOW}"
  298. # Add the user to its additional groups
  299. if [ "${groups}" != "-" ]; then
  300. for _group in ${groups//,/ }; do
  301. add_user_to_group "${username}" "${_group}"
  302. done
  303. fi
  304. # If the user has a home, chown it
  305. # (Note: stdout goes to the fakeroot-script)
  306. if [ "${home}" != "-" ]; then
  307. mkdir -p "${TARGET_DIR}/${home}"
  308. printf "chown -h -R %d:%d '%s'\n" "${uid}" "${_gid}" "${TARGET_DIR}/${home}"
  309. fi
  310. }
  311. #----------------------------------------------------------------------------
  312. main() {
  313. local username uid group gid passwd home shell groups comment
  314. local line
  315. local -a ENTRIES
  316. # Some sanity checks
  317. if [ ${MIN_UID} -le 0 ]; then
  318. fail "MIN_UID must be >0 (currently %d)\n" ${MIN_UID}
  319. fi
  320. if [ ${MIN_GID} -le 0 ]; then
  321. fail "MIN_GID must be >0 (currently %d)\n" ${MIN_GID}
  322. fi
  323. # Read in all the file in memory, exclude empty lines and comments
  324. while read line; do
  325. ENTRIES+=( "${line}" )
  326. done < <( sed -r -e 's/#.*//; /^[[:space:]]*$/d;' "${USERS_TABLE}" )
  327. # We first create groups whose gid is not -1, and then we create groups
  328. # whose gid is -1 (automatic), so that, if a group is defined both with
  329. # a specified gid and an automatic gid, we ensure the specified gid is
  330. # used, rather than a different automatic gid is computed.
  331. # First, create all the main groups which gid is *not* automatic
  332. for line in "${ENTRIES[@]}"; do
  333. read username uid group gid passwd home shell groups comment <<<"${line}"
  334. [ ${gid} -ge 0 ] || continue # Automatic gid
  335. add_one_group "${group}" "${gid}"
  336. done
  337. # Then, create all the main groups which gid *is* automatic
  338. for line in "${ENTRIES[@]}"; do
  339. read username uid group gid passwd home shell groups comment <<<"${line}"
  340. [ ${gid} -eq -1 ] || continue # Non-automatic gid
  341. add_one_group "${group}" "${gid}"
  342. done
  343. # Then, create all the additional groups
  344. # If any additional group is already a main group, we should use
  345. # the gid of that main group; otherwise, we can use any gid
  346. for line in "${ENTRIES[@]}"; do
  347. read username uid group gid passwd home shell groups comment <<<"${line}"
  348. if [ "${groups}" != "-" ]; then
  349. for g in ${groups//,/ }; do
  350. add_one_group "${g}" -1
  351. done
  352. fi
  353. done
  354. # When adding users, we do as for groups, in case two packages create
  355. # the same user, one with an automatic uid, the other with a specified
  356. # uid, to ensure the specified uid is used, rather than an incompatible
  357. # uid be generated.
  358. # Now, add users whose uid is *not* automatic
  359. for line in "${ENTRIES[@]}"; do
  360. read username uid group gid passwd home shell groups comment <<<"${line}"
  361. [ "${username}" != "-" ] || continue # Magic string to skip user creation
  362. [ ${uid} -ge 0 ] || continue # Automatic uid
  363. add_one_user "${username}" "${uid}" "${group}" "${gid}" "${passwd}" \
  364. "${home}" "${shell}" "${groups}" "${comment}"
  365. done
  366. # Finally, add users whose uid *is* automatic
  367. for line in "${ENTRIES[@]}"; do
  368. read username uid group gid passwd home shell groups comment <<<"${line}"
  369. [ "${username}" != "-" ] || continue # Magic string to skip user creation
  370. [ ${uid} -eq -1 ] || continue # Non-automatic uid
  371. add_one_user "${username}" "${uid}" "${group}" "${gid}" "${passwd}" \
  372. "${home}" "${shell}" "${groups}" "${comment}"
  373. done
  374. }
  375. #----------------------------------------------------------------------------
  376. main "${@}"