Skip to contentSkip to conversation

Pull request for branch yem-package-create-user

GIT PULL New Yann E. MORIN 2 participants 8 messages Feb 5, 2013 – Feb 12, 2013 VFS

lore
Share & tools
lore
#0 lore raw

Hello All!

Here is a series that allows packages to create users.

Packages that install daemons may well want to run those daemons as non-root users to avoid security issues. Currently, there are two users of choice to run 'generic' daemons: root or daemon (although there are a few dedicated users to run a few services: mail, sshd, ftp...).

This series builds upon both the package infrastrucutre to define the user(s) a package may want to create, and the filesystem infrastructure to actually generate these users, and chown their ${HOME}s.

Documentation is updated accordingly.

As a proof of concept, the package tvheadend has been updated to use a dedicated user to run its daemon as (call me stubborn! ;-] ).

Changes v4 -> v5:
- rebased ontop master after Developer's Day comments and upstreaming

Changes v3 -> v4:
- use the configured password encryption scheme
- some tweaks and typo-fixes to the documentation

Changes v2 -> v3:
- clarify password prefixes (Samuel)
- move makeuser syntax doc to its own file (Samuel)
- use awk instead of sed to parse /etc/passwd et al. (Cam, Thomas)
- sanitise use of grep (Cam)
- enhancements and fixes to makuser syntax doc (Cam)

Changes v1 -> v2:
- drop the gshadow patch (Thomas, Peter)
- tvheadend user is now part of the video secondary group

The following changes since commit b93bc6ebdcbae89547dc89dbce4701ea8037e02b:

samba: security bump to version 3.6.12 (2013-02-05 12:23:01 +0100)

are available in the git repository at:
git://gitorious.org/buildroot/buildroot.git yem-package-create-user

Yann E. MORIN (2):
packages: add ability for packages to create users package/tvheadend: use a non-root user to run the daemon

 docs/manual/adding-packages-generic.txt |   16 ++-
 docs/manual/appendix.txt                |    1 +
 docs/manual/makeusers-syntax.txt        |   87 +++++++
 fs/common.mk                            |    3 +
 package/pkg-generic.mk                  |    1 +
 package/tvheadend/etc.default.tvheadend |    5 +-
 package/tvheadend/tvheadend.mk          |   10 +-
 support/scripts/mkusers                 |  371 +++++++++++++++++++++++++++++++
 8 files changed, 487 insertions(+), 7 deletions(-)
 create mode 100644 docs/manual/makeusers-syntax.txt
 create mode 100755 support/scripts/mkusers

Regards,
Yann E. MORIN

Signature · 6 lines
.-----------------.--------------------.------------------.--------------------.
|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
| +33 223 225 172 `------------.-------:  X  AGAINST      |  \e/  There is no  |
| http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
'------------------------------^-------^------------------^--------------------'
#1 lore raw

PATCH 1/2 packages: add ability for packages to create users

Packages that install daemons may need those daemons to run as a non-root, or an otherwise non-system (eg. 'daemon'), user.

Add infrastructure for packages to create users, by declaring the FOO_USERS variable that contain a makedev-syntax-like description of the user(s) to add.

Cc Samuel Martin <[email protected]>
Cc Cam Hutchison <[email protected]>
Cc Thomas Petazzoni <[email protected]>
---
 docs/manual/adding-packages-generic.txt |   16 ++-
 docs/manual/appendix.txt                |    1 +
 docs/manual/makeusers-syntax.txt        |   87 +++++++
 fs/common.mk                            |    3 +
 package/pkg-generic.mk                  |    1 +
 support/scripts/mkusers                 |  371 +++++++++++++++++++++++++++++++
 6 files changed, 477 insertions(+), 2 deletions(-)
 create mode 100644 docs/manual/makeusers-syntax.txt
 create mode 100755 support/scripts/mkusers
docs/manual/adding-packages-generic.txt +14 -2
index 41a94d7..aa938b6 100644
@@ -51,7 +51,11 @@ system is based on hand-written Makefiles or shell scripts.
 35:	/bin/foo  f  4755  0  0	 -  -  -  -  -
 36: endef
 37:
-38: $(eval $(generic-package))
+38: define LIBFOO_USERS
+39:	foo -1 libfoo -1 * - - - LibFoo daemon
+40: endef
+41:
+42: $(eval $(generic-package))
 --------------------------------
 
 The Makefile begins on line 6 to 10 with metadata information: the
@@ -134,7 +138,10 @@ On line 29..31, we define a device-node file used by this package
 On line 33..35, we define the permissions to set to specific files
 installed by this package (+LIBFOO_PERMISSIONS+).
 
-Finally, on line 37, we call the +generic-package+ function, which
+On lines 38..40, we define a user that is used by this package (eg.
+to run a daemon as non-root) (+LIBFOO_USERS+).
+
+Finally, on line 42, we call the +generic-package+ function, which
 generates, according to the variables defined previously, all the
 Makefile code necessary to make your package working.
 
@@ -299,6 +306,11 @@ information is (assuming the package name is +libfoo+) :
   You can find some documentation for this syntax in the xref:makedev-syntax[].
   This variable is optional.
 
+* +LIBFOO_USERS+ lists the users to create for this package, if it installs
+  a program you want to run as a specific user (eg. as a daemon, or as a
+  cron-job). The syntax is similar in spirit to the makedevs one, and is
+  described in the xref:makeuser-syntax[]. This variable is optional.
+
 * +LIBFOO_LICENSE+ defines the license (or licenses) under which the package
   is released.
   This name will appear in the manifest file produced by +make legal-info+.
docs/manual/appendix.txt +1 -0
index 6f1e9f3..63b172b 100644
@@ -4,6 +4,7 @@ Appendix
 ========
 
 include::makedev-syntax.txt[]
+include::makeusers-syntax.txt[]
 
 [[package-list]]
 Available packages
docs/manual/makeusers-syntax.txt new file +87 -0

87 lines not on this page · raw message

fs/common.mk +3 -0
index 8b5b2f2..a8279b1 100644
@@ -35,6 +35,7 @@ FAKEROOT_SCRIPT = $(BUILD_DIR)/_fakeroot.fs
 FULL_DEVICE_TABLE = $(BUILD_DIR)/_device_table.txt
 ROOTFS_DEVICE_TABLES = $(call qstrip,$(BR2_ROOTFS_DEVICE_TABLE)) \
 	$(call qstrip,$(BR2_ROOTFS_STATIC_DEVICE_TABLE))
+USERS_TABLE = $(BUILD_DIR)/_users_table.txt
 
 define ROOTFS_TARGET_INTERNAL
 
@@ -55,6 +56,8 @@ endif
 	printf '$$(subst $$(sep),\n,$$(PACKAGES_PERMISSIONS_TABLE))' >> $$(FULL_DEVICE_TABLE)
 	echo "$$(HOST_DIR)/usr/bin/makedevs -d $$(FULL_DEVICE_TABLE) $$(TARGET_DIR)" >> $$(FAKEROOT_SCRIPT)
 endif
+	printf '$(subst $(sep),\n,$(PACKAGES_USERS))' > $(USERS_TABLE)
+	$(TOPDIR)/support/scripts/mkusers $(USERS_TABLE) $(TARGET_DIR) >> $(FAKEROOT_SCRIPT)
 	echo "$$(ROOTFS_$(2)_CMD)" >> $$(FAKEROOT_SCRIPT)
 	chmod a+x $$(FAKEROOT_SCRIPT)
 	$$(HOST_DIR)/usr/bin/fakeroot -- $$(FAKEROOT_SCRIPT)
package/pkg-generic.mk +1 -0
index 19a115e..e095100 100644
@@ -524,6 +524,7 @@ ifeq ($$($$($(2)_KCONFIG_VAR)),y)
 TARGETS += $(1)
 PACKAGES_PERMISSIONS_TABLE += $$($(2)_PERMISSIONS)$$(sep)
 PACKAGES_DEVICES_TABLE += $$($(2)_DEVICES)$$(sep)
+PACKAGES_USERS += $$($(2)_USERS)$$(sep)
 
 ifeq ($$($(2)_SITE_METHOD),svn)
 DL_TOOLS_DEPENDENCIES += svn
support/scripts/mkusers new file +371 -0

371 lines not on this page · raw message

Signature · 1 line
1.7.2.5
#2 lore raw

PATCH 1/2 packages: add ability for packages to create users

Although I have a shitload of comments below, there's nothing critical so:

On 05/02/13 15:54, Yann E. MORIN wrote:
[snip]

support/scripts/mkusers line 1

Quoted 7 lines
support/scripts/mkusers new file +1 -0
new file mode 100755
index 0000000..d98714c
@@ -0,0 +1,371 @@
+#!/bin/bash

Although we already do depend on bash, I would prefer not to add more
dependencies. The only bashism I could find is the 'local' declaration, which unfortunately is really required where it is used in this script. So never mind that.

Quoted 18 lines
support/scripts/mkusers new file +18 -0
+set -e
+myname="${0##*/}"
+
+#----------------------------------------------------------------------------
+# Configurable items
+MIN_UID=1000
+MAX_UID=1999
+MIN_GID=1000
+MAX_GID=1999
+# No more is configurable below this point
+#----------------------------------------------------------------------------
+
+#----------------------------------------------------------------------------
+error() {
+    local fmt="${1}"
+    shift
+
+    printf "%s: " "${myname}" >&2

support/scripts/mkusers line 29

Quoted 10 lines
support/scripts/mkusers new file +10 -0
+    printf "${fmt}" "${@}" >&2
+}
+fail() {
+    error "$@"
+    exit 1
+}
+
+#----------------------------------------------------------------------------
+if [ ${#} -ne 2 ]; then
+    fail "usage: %s USERS_TBLE TARGET_DIR\n"

USERS_TABLE

support/scripts/mkusers line 37

Quoted 8 lines
support/scripts/mkusers new file +8 -0
+fi
+USERS_TABLE="${1}"
+TARGET_DIR="${2}"
+shift 2
+PASSWD="${TARGET_DIR}/etc/passwd"
+SHADOW="${TARGET_DIR}/etc/shadow"
+GROUP="${TARGET_DIR}/etc/group"
+# /etc/gsahdow is not part of the standard skeleton, so not everybody

gshadow

support/scripts/mkusers line 44

Quoted 7 lines
support/scripts/mkusers new file +7 -0
+# will have it, but some may hav it, and its content must be in sync
+# with /etc/group, so any use of gshadow must be conditional.
+GSHADOW="${TARGET_DIR}/etc/gshadow"
+PASSWD_METHOD="$( sed -r -e '/^BR2_TARGET_GENERIC_PASSWD_METHOD="(.)*"$/!d' \
+                         -e 's//\1/;'                                       \
+                         "${BUILDROOT_CONFIG}"                              \
+                )"

I'm personally more in favour of sourcing ${BUILDROOT_CONFIG} and using
"${BR2_TARGET_GENERIC_PASSWD_METHOD}" directly.

support/scripts/mkusers line 50

Quoted 6 lines
support/scripts/mkusers new file +6 -0
+
+#----------------------------------------------------------------------------
+get_uid() {
+    local username="${1}"
+
+    awk -F: '$1=="'"${username}"'" { printf( "%d\n", $3 ); }' "${PASSWD}"

I personally find it easier to read an awk script that is written like
this:

	awk -F: -v username="${1}" \
		'$1 == username { printf( "%d\n", $3 ); }'

(i.e. pass variables as awk variables rather than expanding them in the awk script).

But obviously that's just personal opinion.

Quoted 5 lines
support/scripts/mkusers new file +5 -0
+}
+
+#----------------------------------------------------------------------------
+get_ugid() {
+    local username="${1}"

support/scripts/mkusers line 65

Quoted 10 lines
support/scripts/mkusers new file +10 -0
+
+    awk -F: '$1=="'"${username}"'" { printf( "%d\n", $4 ); }' "${PASSWD}"
+}
+
+#----------------------------------------------------------------------------
+get_gid() {
+    local group="${1}"
+
+    awk -F: '$1=="'"${group}"'" { printf( "%d\n", $3 ); }' "${GROUP}"
+}

I would define a single get_id for passwd and group, where the file is
passed as a parameter. That allows to factor the generate_uid/gid, below, as well.

Quoted 43 lines
support/scripts/mkusers new file +43 -0
+
+#----------------------------------------------------------------------------
+get_username() {
+    local uid="${1}"
+
+    awk -F: '$3=="'"${uid}"'" { printf( "%s\n", $1 ); }' "${PASSWD}"
+}
+
+#----------------------------------------------------------------------------
+get_group() {
+    local gid="${1}"
+
+    awk -F: '$3=="'"${gid}"'" { printf( "%s\n", $1 ); }' "${GROUP}"
+}
+
+#----------------------------------------------------------------------------
+get_ugroup() {
+    local username="${1}"
+    local ugid
+
+    ugid="$( get_ugid "${username}" )"
+    if [ -n "${ugid}" ]; then
+        get_group "${ugid}"
+    fi
+}
+
+#----------------------------------------------------------------------------
+# Sanity-check the new user/group:
+#   - check the gid is not already used for another group
+#   - check the group does not already exist with another gid
+#   - check the user does not already exist with another gid
+#   - check the uid is not already used for another user
+#   - check the user does not already exist with another uid
+#   - check the user does not already exist in another group
+check_user_validity() {
+    local username="${1}"
+    local uid="${2}"
+    local group="${3}"
+    local gid="${4}"
+    local _uid _ugid _gid _username _group _ugroup
+
+    _group="$( get_group "${gid}" )"
+    _gid="$( get_gid "${group}" )"

support/scripts/mkusers line 118

Quoted 10 lines
support/scripts/mkusers new file +10 -0
+    _ugid="$( get_ugid "${username}" )"
+    _username="$( get_username "${uid}" )"
+    _uid="$( get_uid "${username}" )"
+    _ugroup="$( get_ugroup "${username}" )"
+
+    if [ "${username}" = "root" ]; then
+        fail "invalid username '%s\n'" "${username}"
+    fi
+
+    if [ ${gid} -lt -1 -o ${gid} -eq 0 ]; then

If ${gid} is not an integer, this will report:
[: foo: integer expression expected
in addition to the error below. Unfortunately there is no way to check for integer, but you could redirect stderr to /dev/null.

Quoted 21 lines
support/scripts/mkusers new file +21 -0
+        fail "invalid gid '%d'\n" ${gid}
+    elif [ ${gid} -ne -1 ]; then
+        # check the gid is not already used for another group
+        if [ -n "${_group}" -a "${_group}" != "${group}" ]; then
+            fail "gid is already used by group '${_group}'\n"
+        fi
+
+        # check the group does not already exists with another gid
+        if [ -n "${_gid}" -a ${_gid} -ne ${gid} ]; then
+            fail "group already exists with gid '${_gid}'\n"
+        fi
+
+        # check the user does not already exists with another gid
+        if [ -n "${_ugid}" -a ${_ugid} -ne ${gid} ]; then
+            fail "user already exists with gid '${_ugid}'\n"
+        fi
+    fi
+
+    if [ ${uid} -lt -1 -o ${uid} -eq 0 ]; then
+        fail "invalid uid '%d'\n" ${uid}
+    elif [ ${uid} -ne -1 ]; then

support/scripts/mkusers line 149

Quoted 10 lines
support/scripts/mkusers new file +10 -0
+        # check the uid is not already used for another user
+        if [ -n "${_username}" -a "${_username}" != "${username}" ]; then
+            fail "uid is already used by user '${_username}'\n"
+        fi
+
+        # check the user does not already exists with another uid
+        if [ -n "${_uid}" -a ${_uid} -ne ${uid} ]; then
+            fail "user already exists with uid '${_uid}'\n"
+        fi
+    fi

If the user already exists, I would check that _all_ fields are exactly
the same. Otherwise, it depends on the order of evaluation of the .mk files what will end up in the passwd/group file.

Quoted 7 lines
support/scripts/mkusers new file +7 -0
+
+    # check the user does not already exist in another group
+    if [ -n "${_ugroup}" -a "${_ugroup}" != "${group}" ]; then
+        fail "user already exists with group '${_ugroup}'\n"
+    fi
+
+    return 0

support/scripts/mkusers line 166

Quoted 10 lines
support/scripts/mkusers new file +10 -0
+}
+
+#----------------------------------------------------------------------------
+# Generate a unique GID for given group. If the group already exists,
+# then simply report its current GID. Otherwise, generate the lowest GID
+# that is:
+#   - not 0
+#   - comprised in [MIN_GID..MAX_GID]
+#   - not already used by a group
+generate_gid() {

So you could factor generate_gid/uid into a single generate_id which
you call like:

generate_id ${group} ${GROUP} ${MIN_GID} ${MAX_GID}

Quoted 21 lines
support/scripts/mkusers new file +21 -0
+    local group="${1}"
+    local gid
+
+    gid="$( get_gid "${group}" )"
+    if [ -z "${gid}" ]; then
+        for(( gid=MIN_GID; gid<=MAX_GID; gid++ )); do
+            if [ -z "$( get_group "${gid}" )" ]; then
+                break
+            fi
+        done
+        if [ ${gid} -gt ${MAX_GID} ]; then
+            fail "can not allocate a GID for group '%s'\n" "${group}"
+        fi
+    fi
+    printf "%d\n" "${gid}"
+}
+
+#----------------------------------------------------------------------------
+# Add a group; if it does already exist, remove it first
+add_one_group() {
+    local group="${1}"

support/scripts/mkusers line 197

Quoted 10 lines
support/scripts/mkusers new file +10 -0
+    local gid="${2}"
+    local _f
+
+    # Generate a new GID if needed
+    if [ ${gid} -eq -1 ]; then
+        gid="$( generate_gid "${group}" )"
+    fi
+
+    # Remove any previous instance of this group
+    for _f in "${GROUP}" "${GSHADOW}"; do

Since ${GROUP} always exists, I think having a loop here is harder to
read that just repeating the sed call.

support/scripts/mkusers line 199

Quoted 2 lines
support/scripts/mkusers new file +2 -0
+        [ -f "${_f}" ] || continue
+        sed -r -i -e '/^'"${group}"':.*/d;' "${_f}"

-r is redundant (it's a plain regex).

Quoting could be simpler:

	sed -i -e "/^${group}:.*/d;" "${GROUP}"

support/scripts/mkusers line 203

Quoted 4 lines
support/scripts/mkusers new file +4 -0
+    done
+
+    printf "%s:x:%d:\n" "${group}" "${gid}" >>"${GROUP}"
+    if [ -f "${GSHADOW}" ]; then

Instead of having the condition twice, just put the sed statement for
GSHADOW here.

Quoted 42 lines
support/scripts/mkusers new file +42 -0
+        printf "%s:*::\n" "${group}" >>"${GSHADOW}"
+    fi
+}
+
+#----------------------------------------------------------------------------
+# Generate a unique UID for given username. If the username already exists,
+# then simply report its current UID. Otherwise, generate the lowest UID
+# that is:
+#   - not 0
+#   - comprised in [MIN_UID..MAX_UID]
+#   - not already used by a user
+generate_uid() {
+    local username="${1}"
+    local uid
+
+    uid="$( get_uid "${username}" )"
+    if [ -z "${uid}" ]; then
+        for(( uid=MIN_UID; uid<=MAX_UID; uid++ )); do
+            if [ -z "$( get_username "${uid}" )" ]; then
+                break
+            fi
+        done
+        if [ ${uid} -gt ${MAX_UID} ]; then
+            fail "can not allocate a UID for user '%s'\n" "${username}"
+        fi
+    fi
+    printf "%d\n" "${uid}"
+}
+
+#----------------------------------------------------------------------------
+# Add given user to given group, if not already the case
+add_user_to_group() {
+    local username="${1}"
+    local group="${2}"
+    local _f
+
+    for _f in "${GROUP}" "${GSHADOW}"; do
+        [ -f "${_f}" ] || continue
+        sed -r -i -e 's/^('"${group}"':.*:)(([^:]+,)?)'"${username}"'(,[^:]+*)?$/\1\2\4/;'  \
+                  -e 's/^('"${group}"':.*)$/\1,'"${username}"'/;'                           \
+                  -e 's/,+/,/'                                                              \
+                  -e 's/:,/:/'                                                              \

support/scripts/mkusers line 255

Quoted 10 lines
support/scripts/mkusers new file +10 -0
+                  "${_f}"
+    done
+}
+
+#----------------------------------------------------------------------------
+# Encode a password
+encode_password() {
+    local passwd="${1}"
+
+    mkpasswd -m "${BR2_TARGET_GENERIC_PASSWD_METHOD}" "${passwd}"

Err, you defined it as PASSWD_METHOD above... But if you source
.config as I suggested, this is OK :-)

Quoted 5 lines
support/scripts/mkusers new file +5 -0
+}
+
+#----------------------------------------------------------------------------
+# Add a user; if it does already exist, remove it first
+add_one_user() {

support/scripts/mkusers line 270

Quoted 10 lines
support/scripts/mkusers new file +10 -0
+    local username="${1}"
+    local uid="${2}"
+    local group="${3}"
+    local gid="${4}"
+    local passwd="${5}"
+    local home="${6}"
+    local shell="${7}"
+    local groups="${8}"
+    local comment="${9}"
+    local nb_days="$((($(date +%s)+(24*60*60-1))/(24*60*60)))"

Hm, I don't like this. If I re-run the same configuration after a year,
it gives me a different shadow.

I think the nb_days field in shadow should be left empty. The one in
the skeleton is arbitrarily set to Dec. 8 1999, it has been like that since the first commit in 2001, and it has completely no meaning.

Quoted 39 lines
support/scripts/mkusers new file +39 -0
+    local _f _group _home _shell _gid _passwd
+
+    # First, sanity-check the user
+    check_user_validity "${username}" "${uid}" "${group}" "${gid}"
+
+    # Generate a new UID if needed
+    if [ ${uid} -eq -1 ]; then
+        uid="$( generate_uid "${username}" )"
+    fi
+
+    # Remove any previous instance of this user
+    for _f in "${PASSWD}" "${SHADOW}"; do
+        sed -r -i -e '/^'"${username}"':.*/d;' "${_f}"
+    done
+
+    _gid="$( get_gid "${group}" )"
+    _shell="${shell}"
+    if [ "${shell}" = "-" ]; then
+        _shell="/bin/false"
+    fi
+    case "${home}" in
+        -)  _home="/";;
+        /)  fail "home can not explicitly be '/'\n";;
+        /*) _home="${home}";;
+        *)  fail "home must be an absolute path\n";;
+    esac
+    case "${passwd}" in
+        !=*)
+            _passwd='!'"$( encode_passwd "${passwd#!=}" )"
+            ;;
+        =*)
+            _passwd="$( encode_passwd "${passwd#=}" )"
+            ;;
+        *)
+            _passwd="${passwd}"
+            ;;
+    esac
+
+    printf "%s:x:%d:%d:%s:%s:%s\n"              \

support/scripts/mkusers line 319

Quoted 10 lines
support/scripts/mkusers new file +10 -0
+           "${username}" "${uid}" "${_gid}"     \
+           "${comment}" "${_home}" "${_shell}"  \
+           >>"${PASSWD}"
+    printf "%s:%s:%d:0:99999:7:::\n"                \
+           "${username}" "${_passwd}" "${nb_days}"  \
+           >>"${SHADOW}"
+
+    # Add the user to its additional groups
+    if [ "${groups}" != "-" ]; then
+        for _group in ${groups//,/ }; do

Oh, here's another bashism. But also one which is much more difficult
to write in Posix sh.

Quoted 18 lines
support/scripts/mkusers new file +18 -0
+            add_user_to_group "${username}" "${_group}"
+        done
+    fi
+
+    # If the user has a home, chown it
+    # (Note: stdout goes to the fakeroot-script)
+    if [ "${home}" != "-" ]; then
+        mkdir -p "${TARGET_DIR}/${home}"
+        printf "chown -R %d:%d '%s'\n" "${uid}" "${_gid}" "${TARGET_DIR}/${home}"
+    fi
+}
+
+#----------------------------------------------------------------------------
+main() {
+    local username uid group gid passwd home shell groups comment
+
+    # Some sanity checks
+    if [ ${MIN_UID} -le 0 ]; then

support/scripts/mkusers line 347

Quoted 10 lines
support/scripts/mkusers new file +10 -0
+        fail "MIN_UID must be >0 (currently %d)\n" ${MIN_UID}
+    fi
+    if [ ${MIN_GID} -le 0 ]; then
+        fail "MIN_GID must be >0 (currently %d)\n" ${MIN_GID}
+    fi
+
+    # First, create all the main groups
+    while read username uid group gid passwd home shell groups comment; do
+        [ -n "${username}" ] || continue    # Package with no user
+        add_one_group "${group}" "${gid}"

In the first pass, I would only add groups where gid != -1. Then, if
there is a line which sets gid and another one which doesn't, it still works. Of course that means you have to repeat the add_one_group for gid == -1 in the second pass.

Quoted 9 lines
support/scripts/mkusers new file +9 -0
+    done <"${USERS_TABLE}"
+
+    # Then, create all the additional groups
+    # If any additional group is already a main group, we should use
+    # the gid of that main group; otherwise, we can use any gid
+    while read username uid group gid passwd home shell groups comment; do
+        [ -n "${username}" ] || continue    # Package with no user
+        if [ "${groups}" != "-" ]; then
+            for g in ${groups//,/ }; do

support/scripts/mkusers line 366

Quoted 10 lines
support/scripts/mkusers new file +10 -0
+                add_one_group "${g}" -1
+            done
+        fi
+    done <"${USERS_TABLE}"
+
+    # Finally, add users
+    while read username uid group gid passwd home shell groups comment; do
+        [ -n "${username}" ] || continue    # Package with no user
+        add_one_user "${username}" "${uid}" "${group}" "${gid}" "${passwd}" \
+                     "${home}" "${shell}" "${groups}" "${comment}"

If you reverse-sort the USERS_TABLE in the makefile before the script
is called, then you also make it possible for one package setting uid to -1 and another giving a specific uid.

Regards,
Arnout

Quoted 6 lines
support/scripts/mkusers new file +5 -0
+    done <"${USERS_TABLE}"
+}
+
+#----------------------------------------------------------------------------
+main "${@}"
Signature · 6 lines
Arnout Vandecappelle                          arnout at mind be
Senior Embedded Software Architect            +32-16-286500
Essensium/Mind                                http://www.mind.be
G.Geenslaan 9, 3001 Leuven, Belgium           BE 872 984 063 RPR Leuven
LinkedIn profile: http://www.linkedin.com/in/arnoutvandecappelle
GPG fingerprint:  7CB5 E4CC 6C2E EFD4 6E3D A754 F963 ECAB 2450 2F1F
#3 lore raw

PATCH 1/2 packages: add ability for packages to create users

Quoted 493 lines · On Wednesday 06 February 2013 Arnout Vandecappelle wrote:

Although I have a shitload of comments below, there's nothing critical so:

On 05/02/13 15:54, Yann E. MORIN wrote:
[snip]

support/scripts/mkusers line 1

Quoted 7 lines
support/scripts/mkusers new file +1 -0
new file mode 100755
index 0000000..d98714c
@@ -0,0 +1,371 @@
+#!/bin/bash

Although we already do depend on bash, I would prefer not to add more
dependencies. The only bashism I could find is the 'local' declaration, which unfortunately is really required where it is used in this script. So never mind that.

Quoted 18 lines
support/scripts/mkusers new file +18 -0
+set -e
+myname="${0##*/}"
+
+#----------------------------------------------------------------------------
+# Configurable items
+MIN_UID=1000
+MAX_UID=1999
+MIN_GID=1000
+MAX_GID=1999
+# No more is configurable below this point
+#----------------------------------------------------------------------------
+
+#----------------------------------------------------------------------------
+error() {
+    local fmt="${1}"
+    shift
+
+    printf "%s: " "${myname}" >&2

support/scripts/mkusers line 29

Quoted 10 lines
support/scripts/mkusers new file +10 -0
+    printf "${fmt}" "${@}" >&2
+}
+fail() {
+    error "$@"
+    exit 1
+}
+
+#----------------------------------------------------------------------------
+if [ ${#} -ne 2 ]; then
+    fail "usage: %s USERS_TBLE TARGET_DIR\n"

USERS_TABLE

support/scripts/mkusers line 37

Quoted 8 lines
support/scripts/mkusers new file +8 -0
+fi
+USERS_TABLE="${1}"
+TARGET_DIR="${2}"
+shift 2
+PASSWD="${TARGET_DIR}/etc/passwd"
+SHADOW="${TARGET_DIR}/etc/shadow"
+GROUP="${TARGET_DIR}/etc/group"
+# /etc/gsahdow is not part of the standard skeleton, so not everybody

gshadow

support/scripts/mkusers line 44

Quoted 7 lines
support/scripts/mkusers new file +7 -0
+# will have it, but some may hav it, and its content must be in sync
+# with /etc/group, so any use of gshadow must be conditional.
+GSHADOW="${TARGET_DIR}/etc/gshadow"
+PASSWD_METHOD="$( sed -r -e '/^BR2_TARGET_GENERIC_PASSWD_METHOD="(.)*"$/!d' \
+                         -e 's//\1/;'                                       \
+                         "${BUILDROOT_CONFIG}"                              \
+                )"

I'm personally more in favour of sourcing ${BUILDROOT_CONFIG} and using
"${BR2_TARGET_GENERIC_PASSWD_METHOD}" directly.

support/scripts/mkusers line 50

Quoted 6 lines
support/scripts/mkusers new file +6 -0
+
+#----------------------------------------------------------------------------
+get_uid() {
+    local username="${1}"
+
+    awk -F: '$1=="'"${username}"'" { printf( "%d\n", $3 ); }' "${PASSWD}"

I personally find it easier to read an awk script that is written like
this:

	awk -F: -v username="${1}" \
		'$1 == username { printf( "%d\n", $3 ); }'

(i.e. pass variables as awk variables rather than expanding them in the awk script).

But obviously that's just personal opinion.

Quoted 5 lines
support/scripts/mkusers new file +5 -0
+}
+
+#----------------------------------------------------------------------------
+get_ugid() {
+    local username="${1}"

support/scripts/mkusers line 65

Quoted 10 lines
support/scripts/mkusers new file +10 -0
+
+    awk -F: '$1=="'"${username}"'" { printf( "%d\n", $4 ); }' "${PASSWD}"
+}
+
+#----------------------------------------------------------------------------
+get_gid() {
+    local group="${1}"
+
+    awk -F: '$1=="'"${group}"'" { printf( "%d\n", $3 ); }' "${GROUP}"
+}

I would define a single get_id for passwd and group, where the file is
passed as a parameter. That allows to factor the generate_uid/gid, below, as well.

Quoted 43 lines
support/scripts/mkusers new file +43 -0
+
+#----------------------------------------------------------------------------
+get_username() {
+    local uid="${1}"
+
+    awk -F: '$3=="'"${uid}"'" { printf( "%s\n", $1 ); }' "${PASSWD}"
+}
+
+#----------------------------------------------------------------------------
+get_group() {
+    local gid="${1}"
+
+    awk -F: '$3=="'"${gid}"'" { printf( "%s\n", $1 ); }' "${GROUP}"
+}
+
+#----------------------------------------------------------------------------
+get_ugroup() {
+    local username="${1}"
+    local ugid
+
+    ugid="$( get_ugid "${username}" )"
+    if [ -n "${ugid}" ]; then
+        get_group "${ugid}"
+    fi
+}
+
+#----------------------------------------------------------------------------
+# Sanity-check the new user/group:
+#   - check the gid is not already used for another group
+#   - check the group does not already exist with another gid
+#   - check the user does not already exist with another gid
+#   - check the uid is not already used for another user
+#   - check the user does not already exist with another uid
+#   - check the user does not already exist in another group
+check_user_validity() {
+    local username="${1}"
+    local uid="${2}"
+    local group="${3}"
+    local gid="${4}"
+    local _uid _ugid _gid _username _group _ugroup
+
+    _group="$( get_group "${gid}" )"
+    _gid="$( get_gid "${group}" )"

support/scripts/mkusers line 118

Quoted 10 lines
support/scripts/mkusers new file +10 -0
+    _ugid="$( get_ugid "${username}" )"
+    _username="$( get_username "${uid}" )"
+    _uid="$( get_uid "${username}" )"
+    _ugroup="$( get_ugroup "${username}" )"
+
+    if [ "${username}" = "root" ]; then
+        fail "invalid username '%s\n'" "${username}"
+    fi
+
+    if [ ${gid} -lt -1 -o ${gid} -eq 0 ]; then

If ${gid} is not an integer, this will report:
[: foo: integer expression expected
in addition to the error below. Unfortunately there is no way to check for integer, but you could redirect stderr to /dev/null.

Quoted 21 lines
support/scripts/mkusers new file +21 -0
+        fail "invalid gid '%d'\n" ${gid}
+    elif [ ${gid} -ne -1 ]; then
+        # check the gid is not already used for another group
+        if [ -n "${_group}" -a "${_group}" != "${group}" ]; then
+            fail "gid is already used by group '${_group}'\n"
+        fi
+
+        # check the group does not already exists with another gid
+        if [ -n "${_gid}" -a ${_gid} -ne ${gid} ]; then
+            fail "group already exists with gid '${_gid}'\n"
+        fi
+
+        # check the user does not already exists with another gid
+        if [ -n "${_ugid}" -a ${_ugid} -ne ${gid} ]; then
+            fail "user already exists with gid '${_ugid}'\n"
+        fi
+    fi
+
+    if [ ${uid} -lt -1 -o ${uid} -eq 0 ]; then
+        fail "invalid uid '%d'\n" ${uid}
+    elif [ ${uid} -ne -1 ]; then

support/scripts/mkusers line 149

Quoted 10 lines
support/scripts/mkusers new file +10 -0
+        # check the uid is not already used for another user
+        if [ -n "${_username}" -a "${_username}" != "${username}" ]; then
+            fail "uid is already used by user '${_username}'\n"
+        fi
+
+        # check the user does not already exists with another uid
+        if [ -n "${_uid}" -a ${_uid} -ne ${uid} ]; then
+            fail "user already exists with uid '${_uid}'\n"
+        fi
+    fi

If the user already exists, I would check that _all_ fields are exactly
the same. Otherwise, it depends on the order of evaluation of the .mk files what will end up in the passwd/group file.

Quoted 7 lines
support/scripts/mkusers new file +7 -0
+
+    # check the user does not already exist in another group
+    if [ -n "${_ugroup}" -a "${_ugroup}" != "${group}" ]; then
+        fail "user already exists with group '${_ugroup}'\n"
+    fi
+
+    return 0

support/scripts/mkusers line 166

Quoted 10 lines
support/scripts/mkusers new file +10 -0
+}
+
+#----------------------------------------------------------------------------
+# Generate a unique GID for given group. If the group already exists,
+# then simply report its current GID. Otherwise, generate the lowest GID
+# that is:
+#   - not 0
+#   - comprised in [MIN_GID..MAX_GID]
+#   - not already used by a group
+generate_gid() {

So you could factor generate_gid/uid into a single generate_id which
you call like:

generate_id ${group} ${GROUP} ${MIN_GID} ${MAX_GID}

Quoted 21 lines
support/scripts/mkusers new file +21 -0
+    local group="${1}"
+    local gid
+
+    gid="$( get_gid "${group}" )"
+    if [ -z "${gid}" ]; then
+        for(( gid=MIN_GID; gid<=MAX_GID; gid++ )); do
+            if [ -z "$( get_group "${gid}" )" ]; then
+                break
+            fi
+        done
+        if [ ${gid} -gt ${MAX_GID} ]; then
+            fail "can not allocate a GID for group '%s'\n" "${group}"
+        fi
+    fi
+    printf "%d\n" "${gid}"
+}
+
+#----------------------------------------------------------------------------
+# Add a group; if it does already exist, remove it first
+add_one_group() {
+    local group="${1}"

support/scripts/mkusers line 197

Quoted 10 lines
support/scripts/mkusers new file +10 -0
+    local gid="${2}"
+    local _f
+
+    # Generate a new GID if needed
+    if [ ${gid} -eq -1 ]; then
+        gid="$( generate_gid "${group}" )"
+    fi
+
+    # Remove any previous instance of this group
+    for _f in "${GROUP}" "${GSHADOW}"; do

Since ${GROUP} always exists, I think having a loop here is harder to
read that just repeating the sed call.

support/scripts/mkusers line 199

Quoted 2 lines
support/scripts/mkusers new file +2 -0
+        [ -f "${_f}" ] || continue
+        sed -r -i -e '/^'"${group}"':.*/d;' "${_f}"

-r is redundant (it's a plain regex).

Quoting could be simpler:

	sed -i -e "/^${group}:.*/d;" "${GROUP}"

support/scripts/mkusers line 203

Quoted 4 lines
support/scripts/mkusers new file +4 -0
+    done
+
+    printf "%s:x:%d:\n" "${group}" "${gid}" >>"${GROUP}"
+    if [ -f "${GSHADOW}" ]; then

Instead of having the condition twice, just put the sed statement for
GSHADOW here.

Quoted 42 lines
support/scripts/mkusers new file +42 -0
+        printf "%s:*::\n" "${group}" >>"${GSHADOW}"
+    fi
+}
+
+#----------------------------------------------------------------------------
+# Generate a unique UID for given username. If the username already exists,
+# then simply report its current UID. Otherwise, generate the lowest UID
+# that is:
+#   - not 0
+#   - comprised in [MIN_UID..MAX_UID]
+#   - not already used by a user
+generate_uid() {
+    local username="${1}"
+    local uid
+
+    uid="$( get_uid "${username}" )"
+    if [ -z "${uid}" ]; then
+        for(( uid=MIN_UID; uid<=MAX_UID; uid++ )); do
+            if [ -z "$( get_username "${uid}" )" ]; then
+                break
+            fi
+        done
+        if [ ${uid} -gt ${MAX_UID} ]; then
+            fail "can not allocate a UID for user '%s'\n" "${username}"
+        fi
+    fi
+    printf "%d\n" "${uid}"
+}
+
+#----------------------------------------------------------------------------
+# Add given user to given group, if not already the case
+add_user_to_group() {
+    local username="${1}"
+    local group="${2}"
+    local _f
+
+    for _f in "${GROUP}" "${GSHADOW}"; do
+        [ -f "${_f}" ] || continue
+        sed -r -i -e 's/^('"${group}"':.*:)(([^:]+,)?)'"${username}"'(,[^:]+*)?$/\1\2\4/;'  \
+                  -e 's/^('"${group}"':.*)$/\1,'"${username}"'/;'                           \
+                  -e 's/,+/,/'                                                              \
+                  -e 's/:,/:/'                                                              \

support/scripts/mkusers line 255

Quoted 10 lines
support/scripts/mkusers new file +10 -0
+                  "${_f}"
+    done
+}
+
+#----------------------------------------------------------------------------
+# Encode a password
+encode_password() {
+    local passwd="${1}"
+
+    mkpasswd -m "${BR2_TARGET_GENERIC_PASSWD_METHOD}" "${passwd}"

Err, you defined it as PASSWD_METHOD above... But if you source
.config as I suggested, this is OK :-)

Quoted 5 lines
support/scripts/mkusers new file +5 -0
+}
+
+#----------------------------------------------------------------------------
+# Add a user; if it does already exist, remove it first
+add_one_user() {

support/scripts/mkusers line 270

Quoted 10 lines
support/scripts/mkusers new file +10 -0
+    local username="${1}"
+    local uid="${2}"
+    local group="${3}"
+    local gid="${4}"
+    local passwd="${5}"
+    local home="${6}"
+    local shell="${7}"
+    local groups="${8}"
+    local comment="${9}"
+    local nb_days="$((($(date +%s)+(24*60*60-1))/(24*60*60)))"

Hm, I don't like this. If I re-run the same configuration after a year,
it gives me a different shadow.

I think the nb_days field in shadow should be left empty. The one in
the skeleton is arbitrarily set to Dec. 8 1999, it has been like that since the first commit in 2001, and it has completely no meaning.

Quoted 39 lines
support/scripts/mkusers new file +39 -0
+    local _f _group _home _shell _gid _passwd
+
+    # First, sanity-check the user
+    check_user_validity "${username}" "${uid}" "${group}" "${gid}"
+
+    # Generate a new UID if needed
+    if [ ${uid} -eq -1 ]; then
+        uid="$( generate_uid "${username}" )"
+    fi
+
+    # Remove any previous instance of this user
+    for _f in "${PASSWD}" "${SHADOW}"; do
+        sed -r -i -e '/^'"${username}"':.*/d;' "${_f}"
+    done
+
+    _gid="$( get_gid "${group}" )"
+    _shell="${shell}"
+    if [ "${shell}" = "-" ]; then
+        _shell="/bin/false"
+    fi
+    case "${home}" in
+        -)  _home="/";;
+        /)  fail "home can not explicitly be '/'\n";;
+        /*) _home="${home}";;
+        *)  fail "home must be an absolute path\n";;
+    esac
+    case "${passwd}" in
+        !=*)
+            _passwd='!'"$( encode_passwd "${passwd#!=}" )"
+            ;;
+        =*)
+            _passwd="$( encode_passwd "${passwd#=}" )"
+            ;;
+        *)
+            _passwd="${passwd}"
+            ;;
+    esac
+
+    printf "%s:x:%d:%d:%s:%s:%s\n"              \

support/scripts/mkusers line 319

Quoted 10 lines
support/scripts/mkusers new file +10 -0
+           "${username}" "${uid}" "${_gid}"     \
+           "${comment}" "${_home}" "${_shell}"  \
+           >>"${PASSWD}"
+    printf "%s:%s:%d:0:99999:7:::\n"                \
+           "${username}" "${_passwd}" "${nb_days}"  \
+           >>"${SHADOW}"
+
+    # Add the user to its additional groups
+    if [ "${groups}" != "-" ]; then
+        for _group in ${groups//,/ }; do

Oh, here's another bashism. But also one which is much more difficult
to write in Posix sh.

Quoted 18 lines
support/scripts/mkusers new file +18 -0
+            add_user_to_group "${username}" "${_group}"
+        done
+    fi
+
+    # If the user has a home, chown it
+    # (Note: stdout goes to the fakeroot-script)
+    if [ "${home}" != "-" ]; then
+        mkdir -p "${TARGET_DIR}/${home}"
+        printf "chown -R %d:%d '%s'\n" "${uid}" "${_gid}" "${TARGET_DIR}/${home}"
+    fi
+}
+
+#----------------------------------------------------------------------------
+main() {
+    local username uid group gid passwd home shell groups comment
+
+    # Some sanity checks
+    if [ ${MIN_UID} -le 0 ]; then

support/scripts/mkusers line 347

Quoted 10 lines
support/scripts/mkusers new file +10 -0
+        fail "MIN_UID must be >0 (currently %d)\n" ${MIN_UID}
+    fi
+    if [ ${MIN_GID} -le 0 ]; then
+        fail "MIN_GID must be >0 (currently %d)\n" ${MIN_GID}
+    fi
+
+    # First, create all the main groups
+    while read username uid group gid passwd home shell groups comment; do
+        [ -n "${username}" ] || continue    # Package with no user
+        add_one_group "${group}" "${gid}"

In the first pass, I would only add groups where gid != -1. Then, if
there is a line which sets gid and another one which doesn't, it still works. Of course that means you have to repeat the add_one_group for gid == -1 in the second pass.

Quoted 9 lines
support/scripts/mkusers new file +9 -0
+    done <"${USERS_TABLE}"
+
+    # Then, create all the additional groups
+    # If any additional group is already a main group, we should use
+    # the gid of that main group; otherwise, we can use any gid
+    while read username uid group gid passwd home shell groups comment; do
+        [ -n "${username}" ] || continue    # Package with no user
+        if [ "${groups}" != "-" ]; then
+            for g in ${groups//,/ }; do

support/scripts/mkusers line 366

Quoted 10 lines
support/scripts/mkusers new file +10 -0
+                add_one_group "${g}" -1
+            done
+        fi
+    done <"${USERS_TABLE}"
+
+    # Finally, add users
+    while read username uid group gid passwd home shell groups comment; do
+        [ -n "${username}" ] || continue    # Package with no user
+        add_one_user "${username}" "${uid}" "${group}" "${gid}" "${passwd}" \
+                     "${home}" "${shell}" "${groups}" "${comment}"

If you reverse-sort the USERS_TABLE in the makefile before the script
is called, then you also make it possible for one package setting uid to -1 and another giving a specific uid.

Regards,
Arnout

Quoted 6 lines
support/scripts/mkusers new file +5 -0
+    done <"${USERS_TABLE}"
+}
+
+#----------------------------------------------------------------------------
+main "${@}"
Signature · 10 lines
Arnout Vandecappelle                          arnout at mind be
Senior Embedded Software Architect            +32-16-286500
Essensium/Mind                                http://www.mind.be
G.Geenslaan 9, 3001 Leuven, Belgium           BE 872 984 063 RPR Leuven
LinkedIn profile: http://www.linkedin.com/in/arnoutvandecappelle
GPG fingerprint:  7CB5 E4CC 6C2E EFD4 6E3D A754 F963 ECAB 2450 2F1F
_______________________________________________
buildroot mailing list
buildroot at busybox.net
http://lists.busybox.net/mailman/listinfo/buildroot
Signature · 6 lines
.-----------------.--------------------.------------------.--------------------.
|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
| +33 223 225 172 `------------.-------:  X  AGAINST      |  \e/  There is no  |
| http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
'------------------------------^-------^------------------^--------------------'
#4 lore raw

PATCH 1/2 packages: add ability for packages to create users

In reply to Arnout Vandecappelle

Arnout, All,

[sorry about previous post, I inadvertently press Ctrl-Enter which sends the mail in kmail]

Quoted 2 lines · On Wednesday 06 February 2013 Arnout Vandecappelle wrote:

Although I have a shitload of comments below, there's nothing critical so:

Thanks!

Typos fixed. See my other comments below, too.

Quoted 6 lines · Yann E. MORIN

support/scripts/mkusers

Quoted 5 lines
support/scripts/mkusers new file +0 -0
new file mode 100755
index 0000000..d98714c

[--SNIP--]

Quoted 10 lines

support/scripts/mkusers line 44

Quoted 7 lines
support/scripts/mkusers new file +7 -0
+# will have it, but some may hav it, and its content must be in sync
+# with /etc/group, so any use of gshadow must be conditional.
+GSHADOW="${TARGET_DIR}/etc/gshadow"
+PASSWD_METHOD="$( sed -r -e '/^BR2_TARGET_GENERIC_PASSWD_METHOD="(.)*"$/!d' \
+                         -e 's//\1/;'                                       \
+                         "${BUILDROOT_CONFIG}"                              \
+                )"

I'm personally more in favour of sourcing ${BUILDROOT_CONFIG} and using
"${BR2_TARGET_GENERIC_PASSWD_METHOD}" directly.

I'll how much it simplifies the code.

Quoted 10 lines

support/scripts/mkusers line 50

Quoted 6 lines
support/scripts/mkusers new file +6 -0
+
+#----------------------------------------------------------------------------
+get_uid() {
+    local username="${1}"
+
+    awk -F: '$1=="'"${username}"'" { printf( "%d\n", $3 ); }' "${PASSWD}"

I personally find it easier to read an awk script that is written like
this:

Quoted 5 lines
	awk -F: -v username="${1}" \
		'$1 == username { printf( "%d\n", $3 ); }'

(i.e. pass variables as awk variables rather than expanding them in the awk script).

OK, seems reasonable.

Quoted 11 lines
Quoted 11 lines
support/scripts/mkusers new file +11 -0
+}
+
+#----------------------------------------------------------------------------
+get_ugid() {
+    local username="${1}"
+
+    awk -F: '$1=="'"${username}"'" { printf( "%d\n", $4 ); }' "${PASSWD}"
+}
+
+#----------------------------------------------------------------------------
+get_gid() {
Quoted 8 lines

support/scripts/mkusers line 65

Quoted 4 lines
support/scripts/mkusers new file +4 -0
+    local group="${1}"
+
+    awk -F: '$1=="'"${group}"'" { printf( "%d\n", $3 ); }' "${GROUP}"
+}

I would define a single get_id for passwd and group, where the file is
passed as a parameter. That allows to factor the generate_uid/gid, below, as well.

It's not the same field. While factorising is overall good, it simple enough like that.

Quoted 10 lines

support/scripts/mkusers line 118

Quoted 5 lines
support/scripts/mkusers new file +5 -0
+    if [ "${username}" = "root" ]; then
+        fail "invalid username '%s\n'" "${username}"
+    fi
+
+    if [ ${gid} -lt -1 -o ${gid} -eq 0 ]; then

If ${gid} is not an integer, this will report:
[: foo: integer expression expected
in addition to the error below. Unfortunately there is no way to check for integer, but you could redirect stderr to /dev/null.

Well, this is not user-selectable, and is in the code. If the code is not using an integer, this is a bug in our code.

Quoted 9 lines

support/scripts/mkusers line 149

Quoted 5 lines
support/scripts/mkusers new file +5 -0
+        # check the user does not already exists with another uid
+        if [ -n "${_uid}" -a ${_uid} -ne ${uid} ]; then
+            fail "user already exists with uid '${_uid}'\n"
+        fi
+    fi

If the user already exists, I would check that _all_ fields are exactly
the same. Otherwise, it depends on the order of evaluation of the .mk files what will end up in the passwd/group file.

OK, I'll queue that change for tomorow, when I'm not as sleepy as I'm now.

Quoted 12 lines

support/scripts/mkusers line 166

Quoted 8 lines
support/scripts/mkusers new file +8 -0
+#----------------------------------------------------------------------------
+# Generate a unique GID for given group. If the group already exists,
+# then simply report its current GID. Otherwise, generate the lowest GID
+# that is:
+#   - not 0
+#   - comprised in [MIN_GID..MAX_GID]
+#   - not already used by a group
+generate_gid() {

So you could factor generate_gid/uid into a single generate_id which
you call like:

Quoted 1 line

generate_id ${group} ${GROUP} ${MIN_GID} ${MAX_GID}

Hmm. No sure it would be that simple... :-/

Quoted 9 lines
Quoted 9 lines
support/scripts/mkusers new file +9 -0
+#----------------------------------------------------------------------------
+# Add a group; if it does already exist, remove it first
+add_one_group() {
+    local group="${1}"
+    local gid="${2}"
+    local _f
+
+    # Generate a new GID if needed
+    if [ ${gid} -eq -1 ]; then
Quoted 8 lines

support/scripts/mkusers line 197

Quoted 5 lines
support/scripts/mkusers new file +5 -0
+        gid="$( generate_gid "${group}" )"
+    fi
+
+    # Remove any previous instance of this group
+    for _f in "${GROUP}" "${GSHADOW}"; do

Since ${GROUP} always exists, I think having a loop here is harder to
read that just repeating the sed call.

OK.

Quoted 4 lines

support/scripts/mkusers line 199

Quoted 2 lines
support/scripts/mkusers new file +2 -0
+        [ -f "${_f}" ] || continue
+        sed -r -i -e '/^'"${group}"':.*/d;' "${_f}"

-r is redundant (it's a plain regex).

I never know what is a plain regexp or an extended regexp. So I always use -i. Will remove.

Quoted 3 lines

Quoting could be simpler:

	sed -i -e "/^${group}:.*/d;" "${GROUP}"

OK.

Quoted 7 lines

support/scripts/mkusers line 203

Quoted 4 lines
support/scripts/mkusers new file +4 -0
+    done
+
+    printf "%s:x:%d:\n" "${group}" "${gid}" >>"${GROUP}"
+    if [ -f "${GSHADOW}" ]; then

Instead of having the condition twice, just put the sed statement for
GSHADOW here.

OK. Makes much more sense that way. :-)

Quoted 9 lines

support/scripts/mkusers line 255

Quoted 6 lines
support/scripts/mkusers new file +6 -0
+#----------------------------------------------------------------------------
+# Encode a password
+encode_password() {
+    local passwd="${1}"
+
+    mkpasswd -m "${BR2_TARGET_GENERIC_PASSWD_METHOD}" "${passwd}"

Err, you defined it as PASSWD_METHOD above... But if you source
.config as I suggested, this is OK :-)

He! How is it even supposed to work at all? :-/
/me is puzzled...
Will fix.

Quoted 19 lines
Quoted 5 lines
support/scripts/mkusers new file +5 -0
+}
+
+#----------------------------------------------------------------------------
+# Add a user; if it does already exist, remove it first
+add_one_user() {

support/scripts/mkusers line 270

Quoted 10 lines
support/scripts/mkusers new file +10 -0
+    local username="${1}"
+    local uid="${2}"
+    local group="${3}"
+    local gid="${4}"
+    local passwd="${5}"
+    local home="${6}"
+    local shell="${7}"
+    local groups="${8}"
+    local comment="${9}"
+    local nb_days="$((($(date +%s)+(24*60*60-1))/(24*60*60)))"

Hm, I don't like this. If I re-run the same configuration after a year,
it gives me a different shadow.

Quoted 3 lines

I think the nb_days field in shadow should be left empty. The one in
the skeleton is arbitrarily set to Dec. 8 1999, it has been like that since the first commit in 2001, and it has completely no meaning.

OK, will go for an empty nb_day field ("empty field means that password aging features are disabled.").

Quoted 6 lines

support/scripts/mkusers line 319

Quoted 3 lines
support/scripts/mkusers new file +3 -0
+    # Add the user to its additional groups
+    if [ "${groups}" != "-" ]; then
+        for _group in ${groups//,/ }; do

Oh, here's another bashism. But also one which is much more difficult
to write in Posix sh.

It'd need womething along the lines of:

  for _group in $( printf "%s" "${groups}" |sed -r -e 's/,/ /g;' ); do

which is much more verbose... I'd like it to be kept a bash script.

Quoted 9 lines

support/scripts/mkusers line 347

Quoted 4 lines
support/scripts/mkusers new file +4 -0
+    # First, create all the main groups
+    while read username uid group gid passwd home shell groups comment; do
+        [ -n "${username}" ] || continue    # Package with no user
+        add_one_group "${group}" "${gid}"

In the first pass, I would only add groups where gid != -1. Then, if
there is a line which sets gid and another one which doesn't, it still works. Of course that means you have to repeat the add_one_group for gid == -1 in the second pass.

OK.

Quoted 15 lines
Quoted 15 lines
support/scripts/mkusers new file +15 -0
+    done <"${USERS_TABLE}"
+
+    # Then, create all the additional groups
+    # If any additional group is already a main group, we should use
+    # the gid of that main group; otherwise, we can use any gid
+    while read username uid group gid passwd home shell groups comment; do
+        [ -n "${username}" ] || continue    # Package with no user
+        if [ "${groups}" != "-" ]; then
+            for g in ${groups//,/ }; do
+                add_one_group "${g}" -1
+            done
+        fi
+    done <"${USERS_TABLE}"
+
+    # Finally, add users
Quoted 8 lines

support/scripts/mkusers line 366

Quoted 4 lines
support/scripts/mkusers new file +4 -0
+    while read username uid group gid passwd home shell groups comment; do
+        [ -n "${username}" ] || continue    # Package with no user
+        add_one_user "${username}" "${uid}" "${group}" "${gid}" "${passwd}" \
+                     "${home}" "${shell}" "${groups}" "${comment}"

If you reverse-sort the USERS_TABLE in the makefile before the script
is called, then you also make it possible for one package setting uid to -1 and another giving a specific uid.

OK, I'll check this.

Thank you!

Regards,
Yann E. MORIN.

Signature · 6 lines
.-----------------.--------------------.------------------.--------------------.
|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
| +33 223 225 172 `------------.-------:  X  AGAINST      |  \e/  There is no  |
| http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
'------------------------------^-------^------------------^--------------------'
#5 lore raw

PATCH 1/2 packages: add ability for packages to create users

In reply to Arnout Vandecappelle

Arnout, All,

Quoted 1 line · On Wednesday 06 February 2013 Arnout Vandecappelle wrote:

On 05/02/13 15:54, Yann E. MORIN wrote:

[--SNIP--]

Quoted 7 lines

support/scripts/mkusers line 44

Quoted 4 lines
support/scripts/mkusers new file +4 -0
+PASSWD_METHOD="$( sed -r -e '/^BR2_TARGET_GENERIC_PASSWD_METHOD="(.)*"$/!d' \
+                         -e 's//\1/;'                                       \
+                         "${BUILDROOT_CONFIG}"                              \
+                )"

I'm personally more in favour of sourcing ${BUILDROOT_CONFIG} and using
"${BR2_TARGET_GENERIC_PASSWD_METHOD}" directly.

We can't simply source .config from shell scripts. .config may contain constructs of the form:

    BR2_HOST_DIR="$(BASE_DIR)/host"

which would ultimately try to call a command named 'BASE_DIR' which is obviously wrong for virtually everybody on Earth (and probably other planets, too).

So, I kept a sed expression to extract the password encoding method form the .config.

Note: I applied most of your suggestions, and added your ACK, as per your initial mail. I hope it will be still valid once you see the nes script! ;-)

Regards,
Yann E. MORIN.

Signature · 6 lines
.-----------------.--------------------.------------------.--------------------.
|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
| +33 223 225 172 `------------.-------:  X  AGAINST      |  \e/  There is no  |
| http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
'------------------------------^-------^------------------^--------------------'
#6 lore raw

PATCH 1/2 packages: add ability for packages to create users

Quoted 13 lines · Yann E. MORIN

Arnout, All,

Quoted 1 line · On Wednesday 06 February 2013 Arnout Vandecappelle wrote:

On 05/02/13 15:54, Yann E. MORIN wrote:

[--SNIP--]

Quoted 7 lines

support/scripts/mkusers line 44

Quoted 4 lines
support/scripts/mkusers new file +4 -0
+PASSWD_METHOD="$( sed -r -e '/^BR2_TARGET_GENERIC_PASSWD_METHOD="(.)*"$/!d' \
+                         -e 's//\1/;'                                       \
+                         "${BUILDROOT_CONFIG}"                              \
+                )"

I'm personally more in favour of sourcing ${BUILDROOT_CONFIG} and using
"${BR2_TARGET_GENERIC_PASSWD_METHOD}" directly.

Quoted 7 lines

We can't simply source .config from shell scripts. .config may contain constructs of the form:

     BR2_HOST_DIR="$(BASE_DIR)/host"

which would ultimately try to call a command named 'BASE_DIR' which is obviously wrong for virtually everybody on Earth (and probably other planets, too).

Ah, yes, that's why I prefer to put ${...} references in .config
(${...} is the same as $(...) in GNU make).

But there are about 10 references to $(...) in Config.in defaults, so
changing that is a bit too heavy.

Quoted 5 lines

So, I kept a sed expression to extract the password encoding method form the .config.

Note: I applied most of your suggestions, and added your ACK, as per your initial mail. I hope it will be still valid once you see the nes script! ;-)

No problem.

Regards,
Arnout

Signature · 6 lines
Arnout Vandecappelle                          arnout at mind be
Senior Embedded Software Architect            +32-16-286500
Essensium/Mind                                http://www.mind.be
G.Geenslaan 9, 3001 Leuven, Belgium           BE 872 984 063 RPR Leuven
LinkedIn profile: http://www.linkedin.com/in/arnoutvandecappelle
GPG fingerprint:  7CB5 E4CC 6C2E EFD4 6E3D A754 F963 ECAB 2450 2F1F
#7 lore raw

PATCH 2/2 package/tvheadend: use a non-root user to run the daemon

In reply to Yann E. MORIN
---
 package/tvheadend/etc.default.tvheadend |    5 ++---
 package/tvheadend/tvheadend.mk          |   10 ++++++++--
 2 files changed, 10 insertions(+), 5 deletions(-)
package/tvheadend/etc.default.tvheadend +2 -3
index c769055..253f832 100644
@@ -1,6 +1,5 @@
-# Once we have a real user, we'll use it
-TVH_USER=root
-TVH_GROUP=root
+TVH_USER=tvheadend
+TVH_GROUP=tvheadend
 #TVH_ADAPTERS=
 #TVH_HTTP_PORT=9981
 #TVH_HTSP_PORT=9982
package/tvheadend/tvheadend.mk +8 -2
index 5100781..4a3f882 100644
@@ -26,9 +26,11 @@ TVHEADEND_DEPENDENCIES     += dvb-apps
 # To run tvheadend, we need:
 #  - a startup script, and its config file
 #  - a default DB with a tvheadend admin
+#  - a non-root user to run as
 define TVHEADEND_INSTALL_DB
-	$(INSTALL) -D package/tvheadend/accesscontrol.1     \
-	              $(TARGET_DIR)/root/.hts/tvheadend/accesscontrol/1
+	$(INSTALL) -D -m 0600 package/tvheadend/accesscontrol.1     \
+	              $(TARGET_DIR)/home/tvheadend/.hts/tvheadend/accesscontrol/1
+	chmod -R go-rwx $(TARGET_DIR)/home/tvheadend
 endef
 TVHEADEND_POST_INSTALL_TARGET_HOOKS  = TVHEADEND_INSTALL_DB
 
@@ -37,6 +39,10 @@ define TVHEADEND_INSTALL_INIT_SYSV
 	$(INSTALL) -D package/tvheadend/S99tvheadend          $(TARGET_DIR)/etc/init.d/S99tvheadend
 endef
 
+define TVHEADEND_USERS
+tvheadend -1 tvheadend -1 * /home/tvheadend - video TVHeadend daemon
+endef
+
 #----------------------------------------------------------------------------
 # tvheadend is not an autotools-based package, but it is possible to
 # call its ./configure script as if it were an autotools one.
Signature · 1 line
1.7.2.5