1c342db35SBrad Bishop# 2c342db35SBrad Bishop# SPDX-License-Identifier: GPL-2.0-only 3c342db35SBrad Bishop# 4eb8dc403SDave Cobbleyimport argparse 5eb8dc403SDave Cobbleyimport re 6eb8dc403SDave Cobbley 7eb8dc403SDave Cobbleyclass myArgumentParser(argparse.ArgumentParser): 8eb8dc403SDave Cobbley def _print_message(self, message, file=None): 9eb8dc403SDave Cobbley bb.warn("%s - %s: %s" % (d.getVar('PN'), pkg, message)) 10eb8dc403SDave Cobbley 11eb8dc403SDave Cobbley # This should never be called... 12eb8dc403SDave Cobbley def exit(self, status=0, message=None): 13eb8dc403SDave Cobbley message = message or ("%s - %s: useradd.bbclass: Argument parsing exited" % (d.getVar('PN'), pkg)) 14eb8dc403SDave Cobbley error(message) 15eb8dc403SDave Cobbley 16eb8dc403SDave Cobbley def error(self, message): 17*08902b01SBrad Bishop bb.fatal(message) 18eb8dc403SDave Cobbley 19eb8dc403SDave Cobbleydef split_commands(params): 20eb8dc403SDave Cobbley params = re.split('''[ \t]*;[ \t]*(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', params.strip()) 21eb8dc403SDave Cobbley # Remove any empty items 22eb8dc403SDave Cobbley return [x for x in params if x] 23eb8dc403SDave Cobbley 24eb8dc403SDave Cobbleydef split_args(params): 25eb8dc403SDave Cobbley params = re.split('''[ \t]+(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', params.strip()) 26eb8dc403SDave Cobbley # Remove any empty items 27eb8dc403SDave Cobbley return [x for x in params if x] 28eb8dc403SDave Cobbley 29eb8dc403SDave Cobbleydef build_useradd_parser(): 30eb8dc403SDave Cobbley # The following comes from --help on useradd from shadow 31eb8dc403SDave Cobbley parser = myArgumentParser(prog='useradd') 32eb8dc403SDave Cobbley parser.add_argument("-b", "--base-dir", metavar="BASE_DIR", help="base directory for the home directory of the new account") 33eb8dc403SDave Cobbley parser.add_argument("-c", "--comment", metavar="COMMENT", help="GECOS field of the new account") 34eb8dc403SDave Cobbley parser.add_argument("-d", "--home-dir", metavar="HOME_DIR", help="home directory of the new account") 35eb8dc403SDave Cobbley parser.add_argument("-D", "--defaults", help="print or change default useradd configuration", action="store_true") 36eb8dc403SDave Cobbley parser.add_argument("-e", "--expiredate", metavar="EXPIRE_DATE", help="expiration date of the new account") 37eb8dc403SDave Cobbley parser.add_argument("-f", "--inactive", metavar="INACTIVE", help="password inactivity period of the new account") 38eb8dc403SDave Cobbley parser.add_argument("-g", "--gid", metavar="GROUP", help="name or ID of the primary group of the new account") 39eb8dc403SDave Cobbley parser.add_argument("-G", "--groups", metavar="GROUPS", help="list of supplementary groups of the new account") 40eb8dc403SDave Cobbley parser.add_argument("-k", "--skel", metavar="SKEL_DIR", help="use this alternative skeleton directory") 41eb8dc403SDave Cobbley parser.add_argument("-K", "--key", metavar="KEY=VALUE", help="override /etc/login.defs defaults") 42eb8dc403SDave Cobbley parser.add_argument("-l", "--no-log-init", help="do not add the user to the lastlog and faillog databases", action="store_true") 43eb8dc403SDave Cobbley parser.add_argument("-m", "--create-home", help="create the user's home directory", action="store_const", const=True) 44eb8dc403SDave Cobbley parser.add_argument("-M", "--no-create-home", dest="create_home", help="do not create the user's home directory", action="store_const", const=False) 45eb8dc403SDave Cobbley 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) 46eb8dc403SDave Cobbley parser.add_argument("-o", "--non-unique", help="allow to create users with duplicate (non-unique UID)", action="store_true") 47eb8dc403SDave Cobbley parser.add_argument("-p", "--password", metavar="PASSWORD", help="encrypted password of the new account") 48eb8dc403SDave Cobbley parser.add_argument("-P", "--clear-password", metavar="CLEAR_PASSWORD", help="use this clear password for the new account") 49eb8dc403SDave Cobbley parser.add_argument("-R", "--root", metavar="CHROOT_DIR", help="directory to chroot into") 50eb8dc403SDave Cobbley parser.add_argument("-r", "--system", help="create a system account", action="store_true") 51eb8dc403SDave Cobbley parser.add_argument("-s", "--shell", metavar="SHELL", help="login shell of the new account") 52eb8dc403SDave Cobbley parser.add_argument("-u", "--uid", metavar="UID", help="user ID of the new account") 53eb8dc403SDave Cobbley parser.add_argument("-U", "--user-group", help="create a group with the same name as the user", action="store_const", const=True) 54eb8dc403SDave Cobbley parser.add_argument("LOGIN", help="Login name of the new user") 55eb8dc403SDave Cobbley 56eb8dc403SDave Cobbley return parser 57eb8dc403SDave Cobbley 58eb8dc403SDave Cobbleydef build_groupadd_parser(): 59eb8dc403SDave Cobbley # The following comes from --help on groupadd from shadow 60eb8dc403SDave Cobbley parser = myArgumentParser(prog='groupadd') 61eb8dc403SDave Cobbley 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") 62eb8dc403SDave Cobbley parser.add_argument("-g", "--gid", metavar="GID", help="use GID for the new group") 63eb8dc403SDave Cobbley parser.add_argument("-K", "--key", metavar="KEY=VALUE", help="override /etc/login.defs defaults") 64eb8dc403SDave Cobbley parser.add_argument("-o", "--non-unique", help="allow to create groups with duplicate (non-unique) GID", action="store_true") 65eb8dc403SDave Cobbley parser.add_argument("-p", "--password", metavar="PASSWORD", help="use this encrypted password for the new group") 66eb8dc403SDave Cobbley parser.add_argument("-P", "--clear-password", metavar="CLEAR_PASSWORD", help="use this clear password for the new group") 67eb8dc403SDave Cobbley parser.add_argument("-R", "--root", metavar="CHROOT_DIR", help="directory to chroot into") 68eb8dc403SDave Cobbley parser.add_argument("-r", "--system", help="create a system account", action="store_true") 69eb8dc403SDave Cobbley parser.add_argument("GROUP", help="Group name of the new group") 70eb8dc403SDave Cobbley 71eb8dc403SDave Cobbley return parser 72