diff options
| author | Richard Purdie <richard.purdie@linuxfoundation.org> | 2017-05-12 12:05:41 +0200 |
|---|---|---|
| committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2017-05-16 14:08:28 +0100 |
| commit | a11e87f179409d7f8581db73f636190e428ae849 (patch) | |
| tree | ae1266ac8c6779f4b0977e2d4370f8344c9966d8 | |
| parent | e4e8435886653789c93c95b99080d6c07ef78cbb (diff) | |
| download | poky-a11e87f179409d7f8581db73f636190e428ae849.tar.gz | |
useradd: Create lib/oe/useradd function library
The code in useradd-staticids is generally useful for user addition
functionality but is not reusable in its current form. Refactor the
code into a set of library functions.
(From OE-Core rev: a638ef304b1f1acb4c88f4f90b1ef22526cb8d2f)
Signed-off-by: Peter Kjellerstedt <peter.kjellerstedt@axis.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
| -rw-r--r-- | meta/classes/useradd-staticids.bbclass | 72 | ||||
| -rw-r--r-- | meta/lib/oe/useradd.py | 68 |
2 files changed, 76 insertions, 64 deletions
diff --git a/meta/classes/useradd-staticids.bbclass b/meta/classes/useradd-staticids.bbclass index 2d282c0d71..ce4ac62ab5 100644 --- a/meta/classes/useradd-staticids.bbclass +++ b/meta/classes/useradd-staticids.bbclass | |||
| @@ -1,22 +1,10 @@ | |||
| 1 | # In order to support a deterministic set of 'dynamic' users/groups, | 1 | # In order to support a deterministic set of 'dynamic' users/groups, |
| 2 | # we need a function to reformat the params based on a static file | 2 | # we need a function to reformat the params based on a static file |
| 3 | def update_useradd_static_config(d): | 3 | def update_useradd_static_config(d): |
| 4 | import argparse | ||
| 5 | import itertools | 4 | import itertools |
| 6 | import re | 5 | import re |
| 7 | import errno | 6 | import errno |
| 8 | 7 | import oe.useradd | |
| 9 | class myArgumentParser( argparse.ArgumentParser ): | ||
| 10 | def _print_message(self, message, file=None): | ||
| 11 | bb.warn("%s - %s: %s" % (d.getVar('PN'), pkg, message)) | ||
| 12 | |||
| 13 | # This should never be called... | ||
| 14 | def exit(self, status=0, message=None): | ||
| 15 | message = message or ("%s - %s: useradd.bbclass: Argument parsing exited" % (d.getVar('PN'), pkg)) | ||
| 16 | error(message) | ||
| 17 | |||
| 18 | def error(self, message): | ||
| 19 | bb.fatal(message) | ||
| 20 | 8 | ||
| 21 | def list_extend(iterable, length, obj = None): | 9 | def list_extend(iterable, length, obj = None): |
| 22 | """Ensure that iterable is the specified length by extending with obj | 10 | """Ensure that iterable is the specified length by extending with obj |
| @@ -59,31 +47,7 @@ def update_useradd_static_config(d): | |||
| 59 | 47 | ||
| 60 | # We parse and rewrite the useradd components | 48 | # We parse and rewrite the useradd components |
| 61 | def rewrite_useradd(params, is_pkg): | 49 | def rewrite_useradd(params, is_pkg): |
| 62 | # The following comes from --help on useradd from shadow | 50 | parser = oe.useradd.build_useradd_parser() |
| 63 | parser = myArgumentParser(prog='useradd') | ||
| 64 | parser.add_argument("-b", "--base-dir", metavar="BASE_DIR", help="base directory for the home directory of the new account") | ||
| 65 | parser.add_argument("-c", "--comment", metavar="COMMENT", help="GECOS field of the new account") | ||
| 66 | parser.add_argument("-d", "--home-dir", metavar="HOME_DIR", help="home directory of the new account") | ||
| 67 | parser.add_argument("-D", "--defaults", help="print or change default useradd configuration", action="store_true") | ||
| 68 | parser.add_argument("-e", "--expiredate", metavar="EXPIRE_DATE", help="expiration date of the new account") | ||
| 69 | parser.add_argument("-f", "--inactive", metavar="INACTIVE", help="password inactivity period of the new account") | ||
| 70 | parser.add_argument("-g", "--gid", metavar="GROUP", help="name or ID of the primary group of the new account") | ||
| 71 | parser.add_argument("-G", "--groups", metavar="GROUPS", help="list of supplementary groups of the new account") | ||
| 72 | parser.add_argument("-k", "--skel", metavar="SKEL_DIR", help="use this alternative skeleton directory") | ||
| 73 | parser.add_argument("-K", "--key", metavar="KEY=VALUE", help="override /etc/login.defs defaults") | ||
| 74 | parser.add_argument("-l", "--no-log-init", help="do not add the user to the lastlog and faillog databases", action="store_true") | ||
| 75 | parser.add_argument("-m", "--create-home", help="create the user's home directory", action="store_const", const=True) | ||
| 76 | parser.add_argument("-M", "--no-create-home", dest="create_home", help="do not create the user's home directory", action="store_const", const=False) | ||
| 77 | parser.add_argument("-N", "--no-user-group", dest="user_group", help="do not create a group with the same name as the user", action="store_const", const=False) | ||
| 78 | parser.add_argument("-o", "--non-unique", help="allow to create users with duplicate (non-unique UID)", action="store_true") | ||
| 79 | parser.add_argument("-p", "--password", metavar="PASSWORD", help="encrypted password of the new account") | ||
| 80 | parser.add_argument("-P", "--clear-password", metavar="CLEAR_PASSWORD", help="use this clear password for the new account") | ||
| 81 | parser.add_argument("-R", "--root", metavar="CHROOT_DIR", help="directory to chroot into") | ||
| 82 | parser.add_argument("-r", "--system", help="create a system account", action="store_true") | ||
| 83 | parser.add_argument("-s", "--shell", metavar="SHELL", help="login shell of the new account") | ||
| 84 | parser.add_argument("-u", "--uid", metavar="UID", help="user ID of the new account") | ||
| 85 | parser.add_argument("-U", "--user-group", help="create a group with the same name as the user", action="store_const", const=True) | ||
| 86 | parser.add_argument("LOGIN", help="Login name of the new user") | ||
| 87 | 51 | ||
| 88 | # Return a list of configuration files based on either the default | 52 | # Return a list of configuration files based on either the default |
| 89 | # files/passwd or the contents of USERADD_UID_TABLES | 53 | # files/passwd or the contents of USERADD_UID_TABLES |
| @@ -100,12 +64,9 @@ def update_useradd_static_config(d): | |||
| 100 | 64 | ||
| 101 | newparams = [] | 65 | newparams = [] |
| 102 | users = None | 66 | users = None |
| 103 | for param in re.split('''[ \t]*;[ \t]*(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', params): | 67 | for param in oe.useradd.split_commands(params): |
| 104 | param = param.strip() | ||
| 105 | if not param: | ||
| 106 | continue | ||
| 107 | try: | 68 | try: |
| 108 | uaargs = parser.parse_args(re.split('''[ \t]+(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', param)) | 69 | uaargs = parser.parse_args(oe.useradd.split_args(param)) |
| 109 | except: | 70 | except: |
| 110 | bb.fatal("%s: Unable to parse arguments for USERADD_PARAM_%s: '%s'" % (d.getVar('PN'), pkg, param)) | 71 | bb.fatal("%s: Unable to parse arguments for USERADD_PARAM_%s: '%s'" % (d.getVar('PN'), pkg, param)) |
| 111 | 72 | ||
| @@ -213,17 +174,7 @@ def update_useradd_static_config(d): | |||
| 213 | 174 | ||
| 214 | # We parse and rewrite the groupadd components | 175 | # We parse and rewrite the groupadd components |
| 215 | def rewrite_groupadd(params, is_pkg): | 176 | def rewrite_groupadd(params, is_pkg): |
| 216 | # The following comes from --help on groupadd from shadow | 177 | parser = oe.useradd.build_groupadd_parser() |
| 217 | parser = myArgumentParser(prog='groupadd') | ||
| 218 | parser.add_argument("-f", "--force", help="exit successfully if the group already exists, and cancel -g if the GID is already used", action="store_true") | ||
| 219 | parser.add_argument("-g", "--gid", metavar="GID", help="use GID for the new group") | ||
| 220 | parser.add_argument("-K", "--key", metavar="KEY=VALUE", help="override /etc/login.defs defaults") | ||
| 221 | parser.add_argument("-o", "--non-unique", help="allow to create groups with duplicate (non-unique) GID", action="store_true") | ||
| 222 | parser.add_argument("-p", "--password", metavar="PASSWORD", help="use this encrypted password for the new group") | ||
| 223 | parser.add_argument("-P", "--clear-password", metavar="CLEAR_PASSWORD", help="use this clear password for the new group") | ||
| 224 | parser.add_argument("-R", "--root", metavar="CHROOT_DIR", help="directory to chroot into") | ||
| 225 | parser.add_argument("-r", "--system", help="create a system account", action="store_true") | ||
| 226 | parser.add_argument("GROUP", help="Group name of the new group") | ||
| 227 | 178 | ||
| 228 | # Return a list of configuration files based on either the default | 179 | # Return a list of configuration files based on either the default |
| 229 | # files/group or the contents of USERADD_GID_TABLES | 180 | # files/group or the contents of USERADD_GID_TABLES |
| @@ -240,13 +191,10 @@ def update_useradd_static_config(d): | |||
| 240 | 191 | ||
| 241 | newparams = [] | 192 | newparams = [] |
| 242 | groups = None | 193 | groups = None |
| 243 | for param in re.split('''[ \t]*;[ \t]*(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', params): | 194 | for param in oe.useradd.split_commands(params): |
| 244 | param = param.strip() | ||
| 245 | if not param: | ||
| 246 | continue | ||
| 247 | try: | 195 | try: |
| 248 | # If we're processing multiple lines, we could have left over values here... | 196 | # If we're processing multiple lines, we could have left over values here... |
| 249 | gaargs = parser.parse_args(re.split('''[ \t]+(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', param)) | 197 | gaargs = parser.parse_args(oe.useradd.split_args(param)) |
| 250 | except: | 198 | except: |
| 251 | bb.fatal("%s: Unable to parse arguments for GROUPADD_PARAM_%s: '%s'" % (d.getVar('PN'), pkg, param)) | 199 | bb.fatal("%s: Unable to parse arguments for GROUPADD_PARAM_%s: '%s'" % (d.getVar('PN'), pkg, param)) |
| 252 | 200 | ||
| @@ -331,11 +279,7 @@ def update_useradd_static_config(d): | |||
| 331 | 279 | ||
| 332 | #bb.warn("Before: 'EXTRA_USERS_PARAMS' - '%s'" % (d.getVar('EXTRA_USERS_PARAMS'))) | 280 | #bb.warn("Before: 'EXTRA_USERS_PARAMS' - '%s'" % (d.getVar('EXTRA_USERS_PARAMS'))) |
| 333 | new_extrausers = [] | 281 | new_extrausers = [] |
| 334 | for cmd in re.split('''[ \t]*;[ \t]*(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', extrausers): | 282 | for cmd in oe.useradd.split_commands(extrausers): |
| 335 | cmd = cmd.strip() | ||
| 336 | if not cmd: | ||
| 337 | continue | ||
| 338 | |||
| 339 | if re.match('''useradd (.*)''', cmd): | 283 | if re.match('''useradd (.*)''', cmd): |
| 340 | useradd_param = re.match('''useradd (.*)''', cmd).group(1) | 284 | useradd_param = re.match('''useradd (.*)''', cmd).group(1) |
| 341 | useradd_param = rewrite_useradd(useradd_param, False) | 285 | useradd_param = rewrite_useradd(useradd_param, False) |
diff --git a/meta/lib/oe/useradd.py b/meta/lib/oe/useradd.py new file mode 100644 index 0000000000..179ac76b5e --- /dev/null +++ b/meta/lib/oe/useradd.py | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | import argparse | ||
| 2 | import re | ||
| 3 | |||
| 4 | class myArgumentParser(argparse.ArgumentParser): | ||
| 5 | def _print_message(self, message, file=None): | ||
| 6 | bb.warn("%s - %s: %s" % (d.getVar('PN'), pkg, message)) | ||
| 7 | |||
| 8 | # This should never be called... | ||
| 9 | def exit(self, status=0, message=None): | ||
| 10 | message = message or ("%s - %s: useradd.bbclass: Argument parsing exited" % (d.getVar('PN'), pkg)) | ||
| 11 | error(message) | ||
| 12 | |||
| 13 | def error(self, message): | ||
| 14 | raise bb.build.FuncFailed(message) | ||
| 15 | |||
| 16 | def split_commands(params): | ||
| 17 | params = re.split('''[ \t]*;[ \t]*(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', params.strip()) | ||
| 18 | # Remove any empty items | ||
| 19 | return [x for x in params if x] | ||
| 20 | |||
| 21 | def split_args(params): | ||
| 22 | params = re.split('''[ \t]+(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', params.strip()) | ||
| 23 | # Remove any empty items | ||
| 24 | return [x for x in params if x] | ||
| 25 | |||
| 26 | def build_useradd_parser(): | ||
| 27 | # The following comes from --help on useradd from shadow | ||
| 28 | parser = myArgumentParser(prog='useradd') | ||
| 29 | parser.add_argument("-b", "--base-dir", metavar="BASE_DIR", help="base directory for the home directory of the new account") | ||
| 30 | parser.add_argument("-c", "--comment", metavar="COMMENT", help="GECOS field of the new account") | ||
| 31 | parser.add_argument("-d", "--home-dir", metavar="HOME_DIR", help="home directory of the new account") | ||
| 32 | parser.add_argument("-D", "--defaults", help="print or change default useradd configuration", action="store_true") | ||
| 33 | parser.add_argument("-e", "--expiredate", metavar="EXPIRE_DATE", help="expiration date of the new account") | ||
| 34 | parser.add_argument("-f", "--inactive", metavar="INACTIVE", help="password inactivity period of the new account") | ||
| 35 | parser.add_argument("-g", "--gid", metavar="GROUP", help="name or ID of the primary group of the new account") | ||
| 36 | parser.add_argument("-G", "--groups", metavar="GROUPS", help="list of supplementary groups of the new account") | ||
| 37 | parser.add_argument("-k", "--skel", metavar="SKEL_DIR", help="use this alternative skeleton directory") | ||
| 38 | parser.add_argument("-K", "--key", metavar="KEY=VALUE", help="override /etc/login.defs defaults") | ||
| 39 | parser.add_argument("-l", "--no-log-init", help="do not add the user to the lastlog and faillog databases", action="store_true") | ||
| 40 | parser.add_argument("-m", "--create-home", help="create the user's home directory", action="store_const", const=True) | ||
| 41 | parser.add_argument("-M", "--no-create-home", dest="create_home", help="do not create the user's home directory", action="store_const", const=False) | ||
| 42 | parser.add_argument("-N", "--no-user-group", dest="user_group", help="do not create a group with the same name as the user", action="store_const", const=False) | ||
| 43 | parser.add_argument("-o", "--non-unique", help="allow to create users with duplicate (non-unique UID)", action="store_true") | ||
| 44 | parser.add_argument("-p", "--password", metavar="PASSWORD", help="encrypted password of the new account") | ||
| 45 | parser.add_argument("-P", "--clear-password", metavar="CLEAR_PASSWORD", help="use this clear password for the new account") | ||
| 46 | parser.add_argument("-R", "--root", metavar="CHROOT_DIR", help="directory to chroot into") | ||
| 47 | parser.add_argument("-r", "--system", help="create a system account", action="store_true") | ||
| 48 | parser.add_argument("-s", "--shell", metavar="SHELL", help="login shell of the new account") | ||
| 49 | parser.add_argument("-u", "--uid", metavar="UID", help="user ID of the new account") | ||
| 50 | parser.add_argument("-U", "--user-group", help="create a group with the same name as the user", action="store_const", const=True) | ||
| 51 | parser.add_argument("LOGIN", help="Login name of the new user") | ||
| 52 | |||
| 53 | return parser | ||
| 54 | |||
| 55 | def build_groupadd_parser(): | ||
| 56 | # The following comes from --help on groupadd from shadow | ||
| 57 | parser = myArgumentParser(prog='groupadd') | ||
| 58 | parser.add_argument("-f", "--force", help="exit successfully if the group already exists, and cancel -g if the GID is already used", action="store_true") | ||
| 59 | parser.add_argument("-g", "--gid", metavar="GID", help="use GID for the new group") | ||
| 60 | parser.add_argument("-K", "--key", metavar="KEY=VALUE", help="override /etc/login.defs defaults") | ||
| 61 | parser.add_argument("-o", "--non-unique", help="allow to create groups with duplicate (non-unique) GID", action="store_true") | ||
| 62 | parser.add_argument("-p", "--password", metavar="PASSWORD", help="use this encrypted password for the new group") | ||
| 63 | parser.add_argument("-P", "--clear-password", metavar="CLEAR_PASSWORD", help="use this clear password for the new group") | ||
| 64 | parser.add_argument("-R", "--root", metavar="CHROOT_DIR", help="directory to chroot into") | ||
| 65 | parser.add_argument("-r", "--system", help="create a system account", action="store_true") | ||
| 66 | parser.add_argument("GROUP", help="Group name of the new group") | ||
| 67 | |||
| 68 | return parser | ||
