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 |
| 7 | |
| 8 | class myArgumentParser( argparse.ArgumentParser ): |
| 9 | def _print_message(self, message, file=None): |
| 10 | bb.warn("%s - %s: %s" % (d.getVar('PN', True), pkg, message)) |
| 11 | |
| 12 | # This should never be called... |
| 13 | def exit(self, status=0, message=None): |
| 14 | message = message or ("%s - %s: useradd.bbclass: Argument parsing exited" % (d.getVar('PN', True), pkg)) |
| 15 | error(message) |
| 16 | |
| 17 | def error(self, message): |
| 18 | raise bb.build.FuncFailed(message) |
| 19 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 20 | def list_extend(iterable, length, obj = None): |
| 21 | """Ensure that iterable is the specified length by extending with obj |
| 22 | and return it as a list""" |
| 23 | return list(itertools.islice(itertools.chain(iterable, itertools.repeat(obj)), length)) |
| 24 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 25 | def merge_files(file_list, exp_fields): |
| 26 | """Read each passwd/group file in file_list, split each line and create |
| 27 | a dictionary with the user/group names as keys and the split lines as |
| 28 | values. If the user/group name already exists in the dictionary, then |
| 29 | update any fields in the list with the values from the new list (if they |
| 30 | are set).""" |
| 31 | id_table = dict() |
| 32 | for conf in file_list.split(): |
| 33 | if os.path.exists(conf): |
| 34 | f = open(conf, "r") |
| 35 | for line in f: |
| 36 | if line.startswith('#'): |
| 37 | continue |
| 38 | # Make sure there always are at least exp_fields elements in |
| 39 | # the field list. This allows for leaving out trailing |
| 40 | # colons in the files. |
| 41 | fields = list_extend(line.rstrip().split(":"), exp_fields) |
| 42 | if fields[0] not in id_table: |
| 43 | id_table[fields[0]] = fields |
| 44 | else: |
| 45 | id_table[fields[0]] = list(itertools.imap(lambda x, y: x or y, fields, id_table[fields[0]])) |
| 46 | |
| 47 | return id_table |
| 48 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 49 | # We parse and rewrite the useradd components |
| 50 | def rewrite_useradd(params): |
| 51 | # The following comes from --help on useradd from shadow |
| 52 | parser = myArgumentParser(prog='useradd') |
| 53 | parser.add_argument("-b", "--base-dir", metavar="BASE_DIR", help="base directory for the home directory of the new account") |
| 54 | parser.add_argument("-c", "--comment", metavar="COMMENT", help="GECOS field of the new account") |
| 55 | parser.add_argument("-d", "--home-dir", metavar="HOME_DIR", help="home directory of the new account") |
| 56 | parser.add_argument("-D", "--defaults", help="print or change default useradd configuration", action="store_true") |
| 57 | parser.add_argument("-e", "--expiredate", metavar="EXPIRE_DATE", help="expiration date of the new account") |
| 58 | parser.add_argument("-f", "--inactive", metavar="INACTIVE", help="password inactivity period of the new account") |
| 59 | parser.add_argument("-g", "--gid", metavar="GROUP", help="name or ID of the primary group of the new account") |
| 60 | parser.add_argument("-G", "--groups", metavar="GROUPS", help="list of supplementary groups of the new account") |
| 61 | parser.add_argument("-k", "--skel", metavar="SKEL_DIR", help="use this alternative skeleton directory") |
| 62 | parser.add_argument("-K", "--key", metavar="KEY=VALUE", help="override /etc/login.defs defaults") |
| 63 | 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] | 64 | parser.add_argument("-m", "--create-home", help="create the user's home directory", action="store_const", const=True) |
| 65 | parser.add_argument("-M", "--no-create-home", dest="create_home", help="do not create the user's home directory", action="store_const", const=False) |
| 66 | 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] | 67 | parser.add_argument("-o", "--non-unique", help="allow to create users with duplicate (non-unique UID)", action="store_true") |
| 68 | parser.add_argument("-p", "--password", metavar="PASSWORD", help="encrypted password of the new account") |
| 69 | parser.add_argument("-R", "--root", metavar="CHROOT_DIR", help="directory to chroot into") |
| 70 | parser.add_argument("-r", "--system", help="create a system account", action="store_true") |
| 71 | parser.add_argument("-s", "--shell", metavar="SHELL", help="login shell of the new account") |
| 72 | 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] | 73 | 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] | 74 | parser.add_argument("LOGIN", help="Login name of the new user") |
| 75 | |
| 76 | # Return a list of configuration files based on either the default |
| 77 | # files/passwd or the contents of USERADD_UID_TABLES |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 78 | # paths are resolved via BBPATH |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 79 | def get_passwd_list(d): |
| 80 | str = "" |
| 81 | bbpath = d.getVar('BBPATH', True) |
| 82 | passwd_tables = d.getVar('USERADD_UID_TABLES', True) |
| 83 | if not passwd_tables: |
| 84 | passwd_tables = 'files/passwd' |
| 85 | for conf_file in passwd_tables.split(): |
| 86 | str += " %s" % bb.utils.which(bbpath, conf_file) |
| 87 | return str |
| 88 | |
| 89 | newparams = [] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 90 | users = None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 91 | for param in re.split('''[ \t]*;[ \t]*(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', params): |
| 92 | param = param.strip() |
| 93 | if not param: |
| 94 | continue |
| 95 | try: |
| 96 | uaargs = parser.parse_args(re.split('''[ \t]*(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', param)) |
| 97 | except: |
| 98 | raise bb.build.FuncFailed("%s: Unable to parse arguments for USERADD_PARAM_%s: '%s'" % (d.getVar('PN', True), pkg, param)) |
| 99 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 100 | # Read all passwd files specified in USERADD_UID_TABLES or files/passwd |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 101 | # Use the standard passwd layout: |
| 102 | # username:password:user_id:group_id:comment:home_directory:login_shell |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 103 | # |
| 104 | # If a field is left blank, the original value will be used. The 'username' |
| 105 | # field is required. |
| 106 | # |
| 107 | # Note: we ignore the password field, as including even the hashed password |
| 108 | # in the useradd command may introduce a security hole. It's assumed that |
| 109 | # all new users get the default ('*' which prevents login) until the user is |
| 110 | # specifically configured by the system admin. |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 111 | if not users: |
| 112 | users = merge_files(get_passwd_list(d), 7) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 113 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 114 | if uaargs.LOGIN not in users: |
| 115 | continue |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 116 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 117 | field = users[uaargs.LOGIN] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 118 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 119 | if uaargs.uid and field[2] and (uaargs.uid != field[2]): |
| 120 | bb.warn("%s: Changing username %s's uid from (%s) to (%s), verify configuration files!" % (d.getVar('PN', True), uaargs.LOGIN, uaargs.uid, field[2])) |
| 121 | uaargs.uid = field[2] or uaargs.uid |
| 122 | |
| 123 | # Determine the possible groupname |
| 124 | # Unless the group name (or gid) is specified, we assume that the LOGIN is the groupname |
| 125 | # |
| 126 | # By default the system has creation of the matching groups enabled |
| 127 | # So if the implicit username-group creation is on, then the implicit groupname (LOGIN) |
| 128 | # is used, and we disable the user_group option. |
| 129 | # |
| 130 | user_group = uaargs.user_group is None or uaargs.user_group is True |
| 131 | uaargs.groupname = uaargs.LOGIN if user_group else uaargs.gid |
| 132 | uaargs.groupid = field[3] or uaargs.gid or uaargs.groupname |
| 133 | |
| 134 | if uaargs.groupid and uaargs.gid != uaargs.groupid: |
| 135 | newgroup = None |
| 136 | if not uaargs.groupid.isdigit(): |
| 137 | # We don't have a group number, so we have to add a name |
| 138 | bb.debug(1, "Adding group %s!" % uaargs.groupid) |
| 139 | newgroup = "%s %s" % (' --system' if uaargs.system else '', uaargs.groupid) |
| 140 | elif uaargs.groupname and not uaargs.groupname.isdigit(): |
| 141 | # We have a group name and a group number to assign it to |
| 142 | bb.debug(1, "Adding group %s (gid %s)!" % (uaargs.groupname, uaargs.groupid)) |
| 143 | newgroup = "-g %s %s" % (uaargs.groupid, uaargs.groupname) |
| 144 | else: |
| 145 | # We want to add a group, but we don't know it's name... so we can't add the group... |
| 146 | # We have to assume the group has previously been added or we'll fail on the adduser... |
| 147 | # Note: specifying the actual gid is very rare in OE, usually the group name is specified. |
| 148 | bb.warn("%s: Changing gid for login %s to %s, verify configuration files!" % (d.getVar('PN', True), uaargs.LOGIN, uaargs.groupid)) |
| 149 | |
| 150 | uaargs.gid = uaargs.groupid |
| 151 | uaargs.user_group = None |
| 152 | if newgroup: |
| 153 | groupadd = d.getVar("GROUPADD_PARAM_%s" % pkg, True) |
| 154 | if groupadd: |
| 155 | d.setVar("GROUPADD_PARAM_%s" % pkg, "%s; %s" % (groupadd, newgroup)) |
| 156 | else: |
| 157 | d.setVar("GROUPADD_PARAM_%s" % pkg, newgroup) |
| 158 | |
| 159 | uaargs.comment = "'%s'" % field[4] if field[4] else uaargs.comment |
| 160 | uaargs.home_dir = field[5] or uaargs.home_dir |
| 161 | uaargs.shell = field[6] or uaargs.shell |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 162 | |
| 163 | # Should be an error if a specific option is set... |
| 164 | if d.getVar('USERADD_ERROR_DYNAMIC', True) == '1' and not ((uaargs.uid and uaargs.uid.isdigit()) and uaargs.gid): |
| 165 | #bb.error("Skipping recipe %s, package %s which adds username %s does not have a static uid defined." % (d.getVar('PN', True), pkg, uaargs.LOGIN)) |
| 166 | raise bb.build.FuncFailed("%s - %s: Username %s does not have a static uid defined." % (d.getVar('PN', True), pkg, uaargs.LOGIN)) |
| 167 | |
| 168 | # Reconstruct the args... |
| 169 | newparam = ['', ' --defaults'][uaargs.defaults] |
| 170 | newparam += ['', ' --base-dir %s' % uaargs.base_dir][uaargs.base_dir != None] |
| 171 | newparam += ['', ' --comment %s' % uaargs.comment][uaargs.comment != None] |
| 172 | newparam += ['', ' --home-dir %s' % uaargs.home_dir][uaargs.home_dir != None] |
| 173 | newparam += ['', ' --expiredata %s' % uaargs.expiredate][uaargs.expiredate != None] |
| 174 | newparam += ['', ' --inactive %s' % uaargs.inactive][uaargs.inactive != None] |
| 175 | newparam += ['', ' --gid %s' % uaargs.gid][uaargs.gid != None] |
| 176 | newparam += ['', ' --groups %s' % uaargs.groups][uaargs.groups != None] |
| 177 | newparam += ['', ' --skel %s' % uaargs.skel][uaargs.skel != None] |
| 178 | newparam += ['', ' --key %s' % uaargs.key][uaargs.key != None] |
| 179 | newparam += ['', ' --no-log-init'][uaargs.no_log_init] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 180 | newparam += ['', ' --create-home'][uaargs.create_home is True] |
| 181 | newparam += ['', ' --no-create-home'][uaargs.create_home is False] |
| 182 | newparam += ['', ' --no-user-group'][uaargs.user_group is False] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 183 | newparam += ['', ' --non-unique'][uaargs.non_unique] |
| 184 | newparam += ['', ' --password %s' % uaargs.password][uaargs.password != None] |
| 185 | newparam += ['', ' --root %s' % uaargs.root][uaargs.root != None] |
| 186 | newparam += ['', ' --system'][uaargs.system] |
| 187 | newparam += ['', ' --shell %s' % uaargs.shell][uaargs.shell != None] |
| 188 | newparam += ['', ' --uid %s' % uaargs.uid][uaargs.uid != None] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 189 | newparam += ['', ' --user-group'][uaargs.user_group is True] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 190 | newparam += ' %s' % uaargs.LOGIN |
| 191 | |
| 192 | newparams.append(newparam) |
| 193 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 194 | return ";".join(newparams).strip() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 195 | |
| 196 | # We parse and rewrite the groupadd components |
| 197 | def rewrite_groupadd(params): |
| 198 | # The following comes from --help on groupadd from shadow |
| 199 | parser = myArgumentParser(prog='groupadd') |
| 200 | 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") |
| 201 | parser.add_argument("-g", "--gid", metavar="GID", help="use GID for the new group") |
| 202 | parser.add_argument("-K", "--key", metavar="KEY=VALUE", help="override /etc/login.defs defaults") |
| 203 | parser.add_argument("-o", "--non-unique", help="allow to create groups with duplicate (non-unique) GID", action="store_true") |
| 204 | parser.add_argument("-p", "--password", metavar="PASSWORD", help="use this encrypted password for the new group") |
| 205 | parser.add_argument("-R", "--root", metavar="CHROOT_DIR", help="directory to chroot into") |
| 206 | parser.add_argument("-r", "--system", help="create a system account", action="store_true") |
| 207 | parser.add_argument("GROUP", help="Group name of the new group") |
| 208 | |
| 209 | # Return a list of configuration files based on either the default |
| 210 | # files/group or the contents of USERADD_GID_TABLES |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 211 | # paths are resolved via BBPATH |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 212 | def get_group_list(d): |
| 213 | str = "" |
| 214 | bbpath = d.getVar('BBPATH', True) |
| 215 | group_tables = d.getVar('USERADD_GID_TABLES', True) |
| 216 | if not group_tables: |
| 217 | group_tables = 'files/group' |
| 218 | for conf_file in group_tables.split(): |
| 219 | str += " %s" % bb.utils.which(bbpath, conf_file) |
| 220 | return str |
| 221 | |
| 222 | newparams = [] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 223 | groups = None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 224 | for param in re.split('''[ \t]*;[ \t]*(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', params): |
| 225 | param = param.strip() |
| 226 | if not param: |
| 227 | continue |
| 228 | try: |
| 229 | # If we're processing multiple lines, we could have left over values here... |
| 230 | gaargs = parser.parse_args(re.split('''[ \t]*(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', param)) |
| 231 | except: |
| 232 | raise bb.build.FuncFailed("%s: Unable to parse arguments for GROUPADD_PARAM_%s: '%s'" % (d.getVar('PN', True), pkg, param)) |
| 233 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 234 | # Read all group files specified in USERADD_GID_TABLES or files/group |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 235 | # Use the standard group layout: |
| 236 | # groupname:password:group_id:group_members |
| 237 | # |
| 238 | # If a field is left blank, the original value will be used. The 'groupname' field |
| 239 | # is required. |
| 240 | # |
| 241 | # Note: similar to the passwd file, the 'password' filed is ignored |
| 242 | # 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] | 243 | if not groups: |
| 244 | groups = merge_files(get_group_list(d), 4) |
| 245 | |
| 246 | if gaargs.GROUP not in groups: |
| 247 | continue |
| 248 | |
| 249 | field = groups[gaargs.GROUP] |
| 250 | |
| 251 | if field[2]: |
| 252 | if gaargs.gid and (gaargs.gid != field[2]): |
| 253 | bb.warn("%s: Changing groupname %s's gid from (%s) to (%s), verify configuration files!" % (d.getVar('PN', True), gaargs.GROUP, gaargs.gid, field[2])) |
| 254 | gaargs.gid = field[2] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 255 | |
| 256 | if d.getVar('USERADD_ERROR_DYNAMIC', True) == '1' and not (gaargs.gid and gaargs.gid.isdigit()): |
| 257 | #bb.error("Skipping recipe %s, package %s which adds groupname %s does not have a static gid defined." % (d.getVar('PN', True), pkg, gaargs.GROUP)) |
| 258 | raise bb.build.FuncFailed("%s - %s: Groupname %s does not have a static gid defined." % (d.getVar('PN', True), pkg, gaargs.GROUP)) |
| 259 | |
| 260 | # Reconstruct the args... |
| 261 | newparam = ['', ' --force'][gaargs.force] |
| 262 | newparam += ['', ' --gid %s' % gaargs.gid][gaargs.gid != None] |
| 263 | newparam += ['', ' --key %s' % gaargs.key][gaargs.key != None] |
| 264 | newparam += ['', ' --non-unique'][gaargs.non_unique] |
| 265 | newparam += ['', ' --password %s' % gaargs.password][gaargs.password != None] |
| 266 | newparam += ['', ' --root %s' % gaargs.root][gaargs.root != None] |
| 267 | newparam += ['', ' --system'][gaargs.system] |
| 268 | newparam += ' %s' % gaargs.GROUP |
| 269 | |
| 270 | newparams.append(newparam) |
| 271 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 272 | return ";".join(newparams).strip() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 273 | |
| 274 | # Load and process the users and groups, rewriting the adduser/addgroup params |
| 275 | useradd_packages = d.getVar('USERADD_PACKAGES', True) |
| 276 | |
| 277 | for pkg in useradd_packages.split(): |
| 278 | # Groupmems doesn't have anything we might want to change, so simply validating |
| 279 | # is a bit of a waste -- only process useradd/groupadd |
| 280 | useradd_param = d.getVar('USERADD_PARAM_%s' % pkg, True) |
| 281 | if useradd_param: |
| 282 | #bb.warn("Before: 'USERADD_PARAM_%s' - '%s'" % (pkg, useradd_param)) |
| 283 | d.setVar('USERADD_PARAM_%s' % pkg, rewrite_useradd(useradd_param)) |
| 284 | #bb.warn("After: 'USERADD_PARAM_%s' - '%s'" % (pkg, d.getVar('USERADD_PARAM_%s' % pkg, True))) |
| 285 | |
| 286 | groupadd_param = d.getVar('GROUPADD_PARAM_%s' % pkg, True) |
| 287 | if groupadd_param: |
| 288 | #bb.warn("Before: 'GROUPADD_PARAM_%s' - '%s'" % (pkg, groupadd_param)) |
| 289 | d.setVar('GROUPADD_PARAM_%s' % pkg, rewrite_groupadd(groupadd_param)) |
| 290 | #bb.warn("After: 'GROUPADD_PARAM_%s' - '%s'" % (pkg, d.getVar('GROUPADD_PARAM_%s' % pkg, True))) |
| 291 | |
| 292 | |
| 293 | |
| 294 | python __anonymous() { |
| 295 | if not bb.data.inherits_class('nativesdk', d) \ |
| 296 | and not bb.data.inherits_class('native', d): |
| 297 | try: |
| 298 | update_useradd_static_config(d) |
| 299 | except bb.build.FuncFailed as f: |
| 300 | bb.debug(1, "Skipping recipe %s: %s" % (d.getVar('PN', True), f)) |
| 301 | raise bb.parse.SkipPackage(f) |
| 302 | } |