fs_config: introduce passwd generator

Introduce a generator that outputs passwd files per man(5) passwd.

Succinctly, the output is a colon delimited string containing the following
fields:

  * login name
  * encrypted password (optional)
  * uid (int)
  * gid (int)
  * User name or comment field
  * home directory
  * interpreter (optional)

Multiple colon delimited lines may exist, but will not be separated
across lines.

When run, produces:

foo::2900:2900::/:/system/bin/sh
foo_bar::2901:2901::/:/system/bin/sh
custom_oem1::2902:2902::/:/system/bin/sh

Note that this generator allows for 0 or more config.fs files. This allows for:
  * Unconditional inclusion of /system/etc/passwd in the generated image
  * A blank passwd file if no config.fs files are specified.

This ensures that when OEMs add config.fs files, there is no additional steps
for proper functionality (simpler for OEMs).

The one draw back is the additional inode consumption on system for a possible
blank file.

Test: That it produces a valid passwd file.
Change-Id: I19691c8260f02147ed861f8a319aeab3f5b1738e
Signed-off-by: William Roberts <william.c.roberts@intel.com>
This commit is contained in:
William Roberts 2016-04-25 15:00:34 -07:00 committed by Dan Albert
parent 8f42ce71ac
commit 316f9462af
2 changed files with 92 additions and 1 deletions

View file

@ -105,7 +105,6 @@ LOCAL_GENERATED_SOURCES := $(oem) $(gen)
my_fs_config_h := $(gen)
my_gen_oem_aid := $(oem)
system_android_filesystem_config :=
gen :=
oem :=
endif
@ -151,6 +150,25 @@ LOCAL_EXPORT_C_INCLUDE_DIRS := $(dir $(my_gen_oem_aid))
LOCAL_EXPORT_C_INCLUDE_DEPS := $(my_gen_oem_aid)
include $(BUILD_STATIC_LIBRARY)
##################################
# Generate the system/etc/passwd text file for the target
# This file may be empty if no AIDs are defined in
# TARGET_FS_CONFIG_GEN files.
include $(CLEAR_VARS)
LOCAL_MODULE := passwd
LOCAL_MODULE_CLASS := ETC
include $(BUILD_SYSTEM)/base_rules.mk
$(LOCAL_BUILT_MODULE): PRIVATE_LOCAL_PATH := $(LOCAL_PATH)
$(LOCAL_BUILT_MODULE): PRIVATE_TARGET_FS_CONFIG_GEN := $(TARGET_FS_CONFIG_GEN)
$(LOCAL_BUILT_MODULE): PRIVATE_ANDROID_FS_HDR := $(system_android_filesystem_config)
$(LOCAL_BUILT_MODULE): $(LOCAL_PATH)/fs_config_generator.py $(TARGET_FS_CONFIG_GEN) $(system_android_filesystem_config)
@mkdir -p $(dir $@)
$(hide) $< passwd --aid-header=$(PRIVATE_ANDROID_FS_HDR) $(PRIVATE_TARGET_FS_CONFIG_GEN) > $@
system_android_filesystem_config :=
endif
ANDROID_FS_CONFIG_H :=

View file

@ -1153,6 +1153,79 @@ class OEMAidGen(BaseGenerator):
print OEMAidGen._GENERIC_DEFINE % (aid.identifier, aid.value)
@generator('passwd')
class PasswdGen(BaseGenerator):
"""Generates the /etc/passwd file per man (5) passwd."""
_GENERATED = ('#\n# THIS IS AN AUTOGENERATED FILE! DO NOT MODIFY!\n#')
_FILE_COMMENT = '# Defined in file: \"%s\"'
def __init__(self):
self._old_file = None
def add_opts(self, opt_group):
opt_group.add_argument(
'fsconfig', nargs='+', help='The list of fsconfig files to parse.')
opt_group.add_argument(
'--aid-header',
required=True,
help='An android_filesystem_config.h file'
'to parse AIDs and OEM Ranges from')
def __call__(self, args):
hdr_parser = AIDHeaderParser(args['aid_header'])
parser = FSConfigFileParser(args['fsconfig'], hdr_parser.oem_ranges)
aids = parser.aids
# nothing to do if no aids defined
if len(aids) == 0:
return
print PasswdGen._GENERATED
for aid in aids:
self._print_aid_passwd_line(aid)
def _print_aid_passwd_line(self, aid):
"""
Prints the aid to stdout in the passwd format.
Internal use only.
Colon delimited:
login name, friendly name
encrypted password (optional)
uid (int)
gid (int)
User name or comment field
home directory
interpreter (optional)
Args:
aid (AID): The aid to print.
"""
if self._old_file != aid.found:
self._old_file = aid.found
print PasswdGen._FILE_COMMENT % aid.found
logon = aid.friendly
uid = aid.normalized_value
if ':' in uid:
sys.exit('Cannot specify delimiter character ":" in uid: "%s"' %
uid)
if ':' in logon:
sys.exit('Cannot specify delimiter character ":" in logon: "%s"' %
logon)
print "%s::%s:%s::/:/system/bin/sh" % (logon, uid, uid)
def main():
"""Main entry point for execution."""