mkusers 14 KB

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