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