Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 |
| 3 | def update_useradd_static_config(d): |
| 4 | import argparse |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 5 | import itertools |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 6 | import re |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 7 | import errno |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 8 | |
| 9 | class myArgumentParser( argparse.ArgumentParser ): |
| 10 | def _print_message(self, message, file=None): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 11 | bb.warn("%s - %s: %s" % (d.getVar('PN'), pkg, message)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 12 | |
| 13 | # This should never be called... |
| 14 | def exit(self, status=0, message=None): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 15 | message = message or ("%s - %s: useradd.bbclass: Argument parsing exited" % (d.getVar('PN'), pkg)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 16 | error(message) |
| 17 | |
| 18 | def error(self, message): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 19 | bb.fatal(message) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 20 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 21 | def list_extend(iterable, length, obj = None): |
| 22 | """Ensure that iterable is the specified length by extending with obj |
| 23 | and return it as a list""" |
| 24 | return list(itertools.islice(itertools.chain(iterable, itertools.repeat(obj)), length)) |
| 25 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 26 | def merge_files(file_list, exp_fields): |
| 27 | """Read each passwd/group file in file_list, split each line and create |
| 28 | a dictionary with the user/group names as keys and the split lines as |
| 29 | values. If the user/group name already exists in the dictionary, then |
| 30 | update any fields in the list with the values from the new list (if they |
| 31 | are set).""" |
| 32 | id_table = dict() |
| 33 | for conf in file_list.split(): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 34 | try: |
| 35 | with open(conf, "r") as f: |
| 36 | for line in f: |
| 37 | if line.startswith('#'): |
| 38 | continue |
| 39 | # Make sure there always are at least exp_fields |
| 40 | # elements in the field list. This allows for leaving |
| 41 | # out trailing colons in the files. |
| 42 | fields = list_extend(line.rstrip().split(":"), exp_fields) |
| 43 | if fields[0] not in id_table: |
| 44 | id_table[fields[0]] = fields |
| 45 | else: |
| 46 | id_table[fields[0]] = list(map(lambda x, y: x or y, fields, id_table[fields[0]])) |
| 47 | except IOError as e: |
| 48 | if e.errno == errno.ENOENT: |
| 49 | pass |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 50 | |
| 51 | return id_table |
| 52 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 53 | def handle_missing_id(id, type, pkg): |
| 54 | # For backwards compatibility we accept "1" in addition to "error" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 55 | if d.getVar('USERADD_ERROR_DYNAMIC') == 'error' or d.getVar('USERADD_ERROR_DYNAMIC') == '1': |
| 56 | raise NotImplementedError("%s - %s: %sname %s does not have a static ID defined. Skipping it." % (d.getVar('PN'), pkg, type, id)) |
| 57 | elif d.getVar('USERADD_ERROR_DYNAMIC') == 'warn': |
| 58 | bb.warn("%s - %s: %sname %s does not have a static ID defined." % (d.getVar('PN'), pkg, type, id)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 59 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 60 | # We parse and rewrite the useradd components |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 61 | def rewrite_useradd(params, is_pkg): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 62 | # The following comes from --help on useradd from shadow |
| 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") |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 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) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 80 | parser.add_argument("-P", "--clear-password", metavar="CLEAR_PASSWORD", help="use this clear password for the new account") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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") |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 85 | parser.add_argument("-U", "--user-group", help="create a group with the same name as the user", action="store_const", const=True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 86 | parser.add_argument("LOGIN", help="Login name of the new user") |
| 87 | |
| 88 | # Return a list of configuration files based on either the default |
| 89 | # files/passwd or the contents of USERADD_UID_TABLES |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 90 | # paths are resolved via BBPATH |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 91 | def get_passwd_list(d): |
| 92 | str = "" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 93 | bbpath = d.getVar('BBPATH') |
| 94 | passwd_tables = d.getVar('USERADD_UID_TABLES') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 95 | if not passwd_tables: |
| 96 | passwd_tables = 'files/passwd' |
| 97 | for conf_file in passwd_tables.split(): |
| 98 | str += " %s" % bb.utils.which(bbpath, conf_file) |
| 99 | return str |
| 100 | |
| 101 | newparams = [] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 102 | users = None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 103 | for param in re.split('''[ \t]*;[ \t]*(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', params): |
| 104 | param = param.strip() |
| 105 | if not param: |
| 106 | continue |
| 107 | try: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 108 | uaargs = parser.parse_args(re.split('''[ \t]+(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', param)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 109 | except: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 110 | bb.fatal("%s: Unable to parse arguments for USERADD_PARAM_%s: '%s'" % (d.getVar('PN'), pkg, param)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 111 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 112 | # Read all passwd files specified in USERADD_UID_TABLES or files/passwd |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 113 | # Use the standard passwd layout: |
| 114 | # username:password:user_id:group_id:comment:home_directory:login_shell |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 115 | # |
| 116 | # If a field is left blank, the original value will be used. The 'username' |
| 117 | # field is required. |
| 118 | # |
| 119 | # Note: we ignore the password field, as including even the hashed password |
| 120 | # in the useradd command may introduce a security hole. It's assumed that |
| 121 | # all new users get the default ('*' which prevents login) until the user is |
| 122 | # specifically configured by the system admin. |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 123 | if not users: |
| 124 | users = merge_files(get_passwd_list(d), 7) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 125 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 126 | if uaargs.LOGIN not in users: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 127 | handle_missing_id(uaargs.LOGIN, 'user', pkg) |
| 128 | newparams.append(param) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 129 | continue |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 130 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 131 | field = users[uaargs.LOGIN] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 132 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 133 | if uaargs.uid and field[2] and (uaargs.uid != field[2]): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 134 | bb.warn("%s: Changing username %s's uid from (%s) to (%s), verify configuration files!" % (d.getVar('PN'), uaargs.LOGIN, uaargs.uid, field[2])) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 135 | uaargs.uid = field[2] or uaargs.uid |
| 136 | |
| 137 | # Determine the possible groupname |
| 138 | # Unless the group name (or gid) is specified, we assume that the LOGIN is the groupname |
| 139 | # |
| 140 | # By default the system has creation of the matching groups enabled |
| 141 | # So if the implicit username-group creation is on, then the implicit groupname (LOGIN) |
| 142 | # is used, and we disable the user_group option. |
| 143 | # |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 144 | if uaargs.gid: |
| 145 | uaargs.groupname = uaargs.gid |
| 146 | elif uaargs.user_group is not False: |
| 147 | uaargs.groupname = uaargs.LOGIN |
| 148 | else: |
| 149 | uaargs.groupname = 'users' |
| 150 | uaargs.groupid = field[3] or uaargs.groupname |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 151 | |
| 152 | if uaargs.groupid and uaargs.gid != uaargs.groupid: |
| 153 | newgroup = None |
| 154 | if not uaargs.groupid.isdigit(): |
| 155 | # We don't have a group number, so we have to add a name |
| 156 | bb.debug(1, "Adding group %s!" % uaargs.groupid) |
| 157 | newgroup = "%s %s" % (' --system' if uaargs.system else '', uaargs.groupid) |
| 158 | elif uaargs.groupname and not uaargs.groupname.isdigit(): |
| 159 | # We have a group name and a group number to assign it to |
| 160 | bb.debug(1, "Adding group %s (gid %s)!" % (uaargs.groupname, uaargs.groupid)) |
| 161 | newgroup = "-g %s %s" % (uaargs.groupid, uaargs.groupname) |
| 162 | else: |
| 163 | # We want to add a group, but we don't know it's name... so we can't add the group... |
| 164 | # We have to assume the group has previously been added or we'll fail on the adduser... |
| 165 | # Note: specifying the actual gid is very rare in OE, usually the group name is specified. |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 166 | bb.warn("%s: Changing gid for login %s to %s, verify configuration files!" % (d.getVar('PN'), uaargs.LOGIN, uaargs.groupid)) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 167 | |
| 168 | uaargs.gid = uaargs.groupid |
| 169 | uaargs.user_group = None |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 170 | if newgroup and is_pkg: |
| 171 | groupadd = d.getVar("GROUPADD_PARAM_%s" % pkg) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 172 | if groupadd: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 173 | # Only add the group if not already specified |
| 174 | if not uaargs.groupname in groupadd: |
| 175 | d.setVar("GROUPADD_PARAM_%s" % pkg, "%s; %s" % (groupadd, newgroup)) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 176 | else: |
| 177 | d.setVar("GROUPADD_PARAM_%s" % pkg, newgroup) |
| 178 | |
| 179 | uaargs.comment = "'%s'" % field[4] if field[4] else uaargs.comment |
| 180 | uaargs.home_dir = field[5] or uaargs.home_dir |
| 181 | uaargs.shell = field[6] or uaargs.shell |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 182 | |
| 183 | # Should be an error if a specific option is set... |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 184 | if not uaargs.uid or not uaargs.uid.isdigit() or not uaargs.gid: |
| 185 | handle_missing_id(uaargs.LOGIN, 'user', pkg) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 186 | |
| 187 | # Reconstruct the args... |
| 188 | newparam = ['', ' --defaults'][uaargs.defaults] |
| 189 | newparam += ['', ' --base-dir %s' % uaargs.base_dir][uaargs.base_dir != None] |
| 190 | newparam += ['', ' --comment %s' % uaargs.comment][uaargs.comment != None] |
| 191 | newparam += ['', ' --home-dir %s' % uaargs.home_dir][uaargs.home_dir != None] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 192 | newparam += ['', ' --expiredate %s' % uaargs.expiredate][uaargs.expiredate != None] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 193 | newparam += ['', ' --inactive %s' % uaargs.inactive][uaargs.inactive != None] |
| 194 | newparam += ['', ' --gid %s' % uaargs.gid][uaargs.gid != None] |
| 195 | newparam += ['', ' --groups %s' % uaargs.groups][uaargs.groups != None] |
| 196 | newparam += ['', ' --skel %s' % uaargs.skel][uaargs.skel != None] |
| 197 | newparam += ['', ' --key %s' % uaargs.key][uaargs.key != None] |
| 198 | newparam += ['', ' --no-log-init'][uaargs.no_log_init] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 199 | newparam += ['', ' --create-home'][uaargs.create_home is True] |
| 200 | newparam += ['', ' --no-create-home'][uaargs.create_home is False] |
| 201 | newparam += ['', ' --no-user-group'][uaargs.user_group is False] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 202 | newparam += ['', ' --non-unique'][uaargs.non_unique] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 203 | if uaargs.password != None: |
| 204 | newparam += ['', ' --password %s' % uaargs.password][uaargs.password != None] |
| 205 | elif uaargs.clear_password: |
| 206 | newparam += ['', ' --clear-password %s' % uaargs.clear_password][uaargs.clear_password != None] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 207 | newparam += ['', ' --root %s' % uaargs.root][uaargs.root != None] |
| 208 | newparam += ['', ' --system'][uaargs.system] |
| 209 | newparam += ['', ' --shell %s' % uaargs.shell][uaargs.shell != None] |
| 210 | newparam += ['', ' --uid %s' % uaargs.uid][uaargs.uid != None] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 211 | newparam += ['', ' --user-group'][uaargs.user_group is True] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 212 | newparam += ' %s' % uaargs.LOGIN |
| 213 | |
| 214 | newparams.append(newparam) |
| 215 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 216 | return ";".join(newparams).strip() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 217 | |
| 218 | # We parse and rewrite the groupadd components |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 219 | def rewrite_groupadd(params, is_pkg): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 220 | # The following comes from --help on groupadd from shadow |
| 221 | parser = myArgumentParser(prog='groupadd') |
| 222 | 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") |
| 223 | parser.add_argument("-g", "--gid", metavar="GID", help="use GID for the new group") |
| 224 | parser.add_argument("-K", "--key", metavar="KEY=VALUE", help="override /etc/login.defs defaults") |
| 225 | parser.add_argument("-o", "--non-unique", help="allow to create groups with duplicate (non-unique) GID", action="store_true") |
| 226 | parser.add_argument("-p", "--password", metavar="PASSWORD", help="use this encrypted password for the new group") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 227 | parser.add_argument("-P", "--clear-password", metavar="CLEAR_PASSWORD", help="use this clear password for the new group") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 228 | parser.add_argument("-R", "--root", metavar="CHROOT_DIR", help="directory to chroot into") |
| 229 | parser.add_argument("-r", "--system", help="create a system account", action="store_true") |
| 230 | parser.add_argument("GROUP", help="Group name of the new group") |
| 231 | |
| 232 | # Return a list of configuration files based on either the default |
| 233 | # files/group or the contents of USERADD_GID_TABLES |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 234 | # paths are resolved via BBPATH |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 235 | def get_group_list(d): |
| 236 | str = "" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 237 | bbpath = d.getVar('BBPATH') |
| 238 | group_tables = d.getVar('USERADD_GID_TABLES') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 239 | if not group_tables: |
| 240 | group_tables = 'files/group' |
| 241 | for conf_file in group_tables.split(): |
| 242 | str += " %s" % bb.utils.which(bbpath, conf_file) |
| 243 | return str |
| 244 | |
| 245 | newparams = [] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 246 | groups = None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 247 | for param in re.split('''[ \t]*;[ \t]*(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', params): |
| 248 | param = param.strip() |
| 249 | if not param: |
| 250 | continue |
| 251 | try: |
| 252 | # If we're processing multiple lines, we could have left over values here... |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 253 | gaargs = parser.parse_args(re.split('''[ \t]+(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', param)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 254 | except: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 255 | bb.fatal("%s: Unable to parse arguments for GROUPADD_PARAM_%s: '%s'" % (d.getVar('PN'), pkg, param)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 256 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 257 | # Read all group files specified in USERADD_GID_TABLES or files/group |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 258 | # Use the standard group layout: |
| 259 | # groupname:password:group_id:group_members |
| 260 | # |
| 261 | # If a field is left blank, the original value will be used. The 'groupname' field |
| 262 | # is required. |
| 263 | # |
| 264 | # Note: similar to the passwd file, the 'password' filed is ignored |
| 265 | # Note: group_members is ignored, group members must be configured with the GROUPMEMS_PARAM |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 266 | if not groups: |
| 267 | groups = merge_files(get_group_list(d), 4) |
| 268 | |
| 269 | if gaargs.GROUP not in groups: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 270 | handle_missing_id(gaargs.GROUP, 'group', pkg) |
| 271 | newparams.append(param) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 272 | continue |
| 273 | |
| 274 | field = groups[gaargs.GROUP] |
| 275 | |
| 276 | if field[2]: |
| 277 | if gaargs.gid and (gaargs.gid != field[2]): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 278 | bb.warn("%s: Changing groupname %s's gid from (%s) to (%s), verify configuration files!" % (d.getVar('PN'), gaargs.GROUP, gaargs.gid, field[2])) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 279 | gaargs.gid = field[2] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 280 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 281 | if not gaargs.gid or not gaargs.gid.isdigit(): |
| 282 | handle_missing_id(gaargs.GROUP, 'group', pkg) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 283 | |
| 284 | # Reconstruct the args... |
| 285 | newparam = ['', ' --force'][gaargs.force] |
| 286 | newparam += ['', ' --gid %s' % gaargs.gid][gaargs.gid != None] |
| 287 | newparam += ['', ' --key %s' % gaargs.key][gaargs.key != None] |
| 288 | newparam += ['', ' --non-unique'][gaargs.non_unique] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 289 | if gaargs.password != None: |
| 290 | newparam += ['', ' --password %s' % gaargs.password][gaargs.password != None] |
| 291 | elif gaargs.clear_password: |
| 292 | newparam += ['', ' --clear-password %s' % gaargs.clear_password][gaargs.clear_password != None] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 293 | newparam += ['', ' --root %s' % gaargs.root][gaargs.root != None] |
| 294 | newparam += ['', ' --system'][gaargs.system] |
| 295 | newparam += ' %s' % gaargs.GROUP |
| 296 | |
| 297 | newparams.append(newparam) |
| 298 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 299 | return ";".join(newparams).strip() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 300 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 301 | # The parsing of the current recipe depends on the content of |
| 302 | # the files listed in USERADD_UID/GID_TABLES. We need to tell bitbake |
| 303 | # about that explicitly to trigger re-parsing and thus re-execution of |
| 304 | # this code when the files change. |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 305 | bbpath = d.getVar('BBPATH') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 306 | for varname, default in (('USERADD_UID_TABLES', 'files/passwd'), |
| 307 | ('USERADD_GID_TABLES', 'files/group')): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 308 | tables = d.getVar(varname) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 309 | if not tables: |
| 310 | tables = default |
| 311 | for conf_file in tables.split(): |
| 312 | bb.parse.mark_dependency(d, bb.utils.which(bbpath, conf_file)) |
| 313 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 314 | # Load and process the users and groups, rewriting the adduser/addgroup params |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 315 | useradd_packages = d.getVar('USERADD_PACKAGES') or "" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 316 | |
| 317 | for pkg in useradd_packages.split(): |
| 318 | # Groupmems doesn't have anything we might want to change, so simply validating |
| 319 | # is a bit of a waste -- only process useradd/groupadd |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 320 | useradd_param = d.getVar('USERADD_PARAM_%s' % pkg) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 321 | if useradd_param: |
| 322 | #bb.warn("Before: 'USERADD_PARAM_%s' - '%s'" % (pkg, useradd_param)) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 323 | d.setVar('USERADD_PARAM_%s' % pkg, rewrite_useradd(useradd_param, True)) |
| 324 | #bb.warn("After: 'USERADD_PARAM_%s' - '%s'" % (pkg, d.getVar('USERADD_PARAM_%s' % pkg))) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 325 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 326 | groupadd_param = d.getVar('GROUPADD_PARAM_%s' % pkg) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 327 | if groupadd_param: |
| 328 | #bb.warn("Before: 'GROUPADD_PARAM_%s' - '%s'" % (pkg, groupadd_param)) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 329 | d.setVar('GROUPADD_PARAM_%s' % pkg, rewrite_groupadd(groupadd_param, True)) |
| 330 | #bb.warn("After: 'GROUPADD_PARAM_%s' - '%s'" % (pkg, d.getVar('GROUPADD_PARAM_%s' % pkg))) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 331 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 332 | # Load and process extra users and groups, rewriting only adduser/addgroup params |
| 333 | pkg = d.getVar('PN') |
| 334 | extrausers = d.getVar('EXTRA_USERS_PARAMS') or "" |
| 335 | |
| 336 | #bb.warn("Before: 'EXTRA_USERS_PARAMS' - '%s'" % (d.getVar('EXTRA_USERS_PARAMS'))) |
| 337 | new_extrausers = [] |
| 338 | for cmd in re.split('''[ \t]*;[ \t]*(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', extrausers): |
| 339 | cmd = cmd.strip() |
| 340 | if not cmd: |
| 341 | continue |
| 342 | |
| 343 | if re.match('''useradd (.*)''', cmd): |
| 344 | useradd_param = re.match('''useradd (.*)''', cmd).group(1) |
| 345 | useradd_param = rewrite_useradd(useradd_param, False) |
| 346 | cmd = 'useradd %s' % useradd_param |
| 347 | elif re.match('''groupadd (.*)''', cmd): |
| 348 | groupadd_param = re.match('''groupadd (.*)''', cmd).group(1) |
| 349 | groupadd_param = rewrite_groupadd(groupadd_param, False) |
| 350 | cmd = 'groupadd %s' % groupadd_param |
| 351 | |
| 352 | new_extrausers.append(cmd) |
| 353 | |
| 354 | new_extrausers.append('') |
| 355 | d.setVar('EXTRA_USERS_PARAMS', ';'.join(new_extrausers)) |
| 356 | #bb.warn("After: 'EXTRA_USERS_PARAMS' - '%s'" % (d.getVar('EXTRA_USERS_PARAMS'))) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 357 | |
| 358 | |
| 359 | python __anonymous() { |
| 360 | if not bb.data.inherits_class('nativesdk', d) \ |
| 361 | and not bb.data.inherits_class('native', d): |
| 362 | try: |
| 363 | update_useradd_static_config(d) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 364 | except NotImplementedError as f: |
| 365 | bb.debug(1, "Skipping recipe %s: %s" % (d.getVar('PN'), f)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 366 | raise bb.parse.SkipPackage(f) |
| 367 | } |