mkusers 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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' 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 -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 -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 -e 's/^('"${group}"':.*:)(([^:]+,)?)'"${username}"'(,[^:]+*)?$/\1\2\4/;' \
  230. -e 's/^('"${group}"':.*)$/\1,'"${username}"'/;' \
  231. -e 's/,+/,/' \
  232. -e 's/:,/:/' \
  233. "${_f}"
  234. done
  235. }
  236. #----------------------------------------------------------------------------
  237. # Encode a password
  238. encode_password() {
  239. local passwd="${1}"
  240. mkpasswd -m "${PASSWD_METHOD}" "${passwd}"
  241. }
  242. #----------------------------------------------------------------------------
  243. # Add a user; if it does already exist, remove it first
  244. add_one_user() {
  245. local username="${1}"
  246. local uid="${2}"
  247. local group="${3}"
  248. local gid="${4}"
  249. local passwd="${5}"
  250. local home="${6}"
  251. local shell="${7}"
  252. local groups="${8}"
  253. local comment="${9}"
  254. local _f _group _home _shell _gid _passwd
  255. # First, sanity-check the user
  256. check_user_validity "${username}" "${uid}" "${group}" "${gid}"
  257. # Generate a new UID if needed
  258. if [ ${uid} -eq -1 ]; then
  259. uid="$( generate_uid "${username}" )"
  260. fi
  261. # Remove any previous instance of this user
  262. for _f in "${PASSWD}" "${SHADOW}"; do
  263. sed -r -i -e '/^'"${username}"':.*/d;' "${_f}"
  264. done
  265. _gid="$( get_gid "${group}" )"
  266. _shell="${shell}"
  267. if [ "${shell}" = "-" ]; then
  268. _shell="/bin/false"
  269. fi
  270. case "${home}" in
  271. -) _home="/";;
  272. /) fail "home can not explicitly be '/'\n";;
  273. /*) _home="${home}";;
  274. *) fail "home must be an absolute path\n";;
  275. esac
  276. case "${passwd}" in
  277. -)
  278. _passwd=""
  279. ;;
  280. !=*)
  281. _passwd='!'"$( encode_password "${passwd#!=}" )"
  282. ;;
  283. =*)
  284. _passwd="$( encode_password "${passwd#=}" )"
  285. ;;
  286. *)
  287. _passwd="${passwd}"
  288. ;;
  289. esac
  290. printf "%s:x:%d:%d:%s:%s:%s\n" \
  291. "${username}" "${uid}" "${_gid}" \
  292. "${comment}" "${_home}" "${_shell}" \
  293. >>"${PASSWD}"
  294. printf "%s:%s:::::::\n" \
  295. "${username}" "${_passwd}" \
  296. >>"${SHADOW}"
  297. # Add the user to its additional groups
  298. if [ "${groups}" != "-" ]; then
  299. for _group in ${groups//,/ }; do
  300. add_user_to_group "${username}" "${_group}"
  301. done
  302. fi
  303. # If the user has a home, chown it
  304. # (Note: stdout goes to the fakeroot-script)
  305. if [ "${home}" != "-" ]; then
  306. mkdir -p "${TARGET_DIR}/${home}"
  307. printf "chown -h -R %d:%d '%s'\n" "${uid}" "${_gid}" "${TARGET_DIR}/${home}"
  308. fi
  309. }
  310. #----------------------------------------------------------------------------
  311. main() {
  312. local username uid group gid passwd home shell groups comment
  313. local line
  314. local -a LINES
  315. # Some sanity checks
  316. if [ ${MIN_UID} -le 0 ]; then
  317. fail "MIN_UID must be >0 (currently %d)\n" ${MIN_UID}
  318. fi
  319. if [ ${MIN_GID} -le 0 ]; then
  320. fail "MIN_GID must be >0 (currently %d)\n" ${MIN_GID}
  321. fi
  322. # Read in all the file in memory, exclude empty lines and comments
  323. while read line; do
  324. LINES+=( "${line}" )
  325. done < <( sed -r -e 's/#.*//; /^[[:space:]]*$/d;' "${USERS_TABLE}" )
  326. # We first create groups whose gid is not -1, and then we create groups
  327. # whose gid is -1 (automatic), so that, if a group is defined both with
  328. # a specified gid and an automatic gid, we ensure the specified gid is
  329. # used, rather than a different automatic gid is computed.
  330. # First, create all the main groups which gid is *not* automatic
  331. for line in "${LINES[@]}"; do
  332. read username uid group gid passwd home shell groups comment <<<"${line}"
  333. [ ${gid} -ge 0 ] || continue # Automatic gid
  334. add_one_group "${group}" "${gid}"
  335. done
  336. # Then, create all the main groups which gid *is* automatic
  337. for line in "${LINES[@]}"; do
  338. read username uid group gid passwd home shell groups comment <<<"${line}"
  339. [ ${gid} -eq -1 ] || continue # Non-automatic gid
  340. add_one_group "${group}" "${gid}"
  341. done
  342. # Then, create all the additional groups
  343. # If any additional group is already a main group, we should use
  344. # the gid of that main group; otherwise, we can use any gid
  345. for line in "${LINES[@]}"; do
  346. read username uid group gid passwd home shell groups comment <<<"${line}"
  347. if [ "${groups}" != "-" ]; then
  348. for g in ${groups//,/ }; do
  349. add_one_group "${g}" -1
  350. done
  351. fi
  352. done
  353. # When adding users, we do as for groups, in case two packages create
  354. # the same user, one with an automatic uid, the other with a specified
  355. # uid, to ensure the specified uid is used, rather than an incompatible
  356. # uid be generated.
  357. # Now, add users whose uid is *not* automatic
  358. for line in "${LINES[@]}"; do
  359. read username uid group gid passwd home shell groups comment <<<"${line}"
  360. [ "${username}" != "-" ] || continue # Magic string to skip user creation
  361. [ ${uid} -ge 0 ] || continue # Automatic uid
  362. add_one_user "${username}" "${uid}" "${group}" "${gid}" "${passwd}" \
  363. "${home}" "${shell}" "${groups}" "${comment}"
  364. done
  365. # Finally, add users whose uid *is* automatic
  366. for line in "${LINES[@]}"; do
  367. read username uid group gid passwd home shell groups comment <<<"${line}"
  368. [ "${username}" != "-" ] || continue # Magic string to skip user creation
  369. [ ${uid} -eq -1 ] || continue # Non-automatic uid
  370. add_one_user "${username}" "${uid}" "${group}" "${gid}" "${passwd}" \
  371. "${home}" "${shell}" "${groups}" "${comment}"
  372. done
  373. }
  374. #----------------------------------------------------------------------------
  375. main "${@}"