blob: abe484eb46818fc408004332599279599b35475a [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
Patrick Williamsc124f4f2015-09-15 14:41:29 -05007# In order to support a deterministic set of 'dynamic' users/groups,
8# we need a function to reformat the params based on a static file
9def update_useradd_static_config(d):
Patrick Williamsf1e5d692016-03-30 15:21:19 -050010 import itertools
Patrick Williamsc124f4f2015-09-15 14:41:29 -050011 import re
Patrick Williamsc0f7c042017-02-23 20:41:17 -060012 import errno
Brad Bishopd7bf8c12018-02-25 22:55:05 -050013 import oe.useradd
Patrick Williamsc124f4f2015-09-15 14:41:29 -050014
Patrick Williamsf1e5d692016-03-30 15:21:19 -050015 def list_extend(iterable, length, obj = None):
16 """Ensure that iterable is the specified length by extending with obj
17 and return it as a list"""
18 return list(itertools.islice(itertools.chain(iterable, itertools.repeat(obj)), length))
19
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050020 def merge_files(file_list, exp_fields):
21 """Read each passwd/group file in file_list, split each line and create
22 a dictionary with the user/group names as keys and the split lines as
23 values. If the user/group name already exists in the dictionary, then
24 update any fields in the list with the values from the new list (if they
25 are set)."""
26 id_table = dict()
27 for conf in file_list.split():
Patrick Williamsc0f7c042017-02-23 20:41:17 -060028 try:
29 with open(conf, "r") as f:
30 for line in f:
31 if line.startswith('#'):
32 continue
33 # Make sure there always are at least exp_fields
34 # elements in the field list. This allows for leaving
35 # out trailing colons in the files.
36 fields = list_extend(line.rstrip().split(":"), exp_fields)
37 if fields[0] not in id_table:
38 id_table[fields[0]] = fields
39 else:
40 id_table[fields[0]] = list(map(lambda x, y: x or y, fields, id_table[fields[0]]))
41 except IOError as e:
42 if e.errno == errno.ENOENT:
43 pass
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050044
45 return id_table
46
Brad Bishopd7bf8c12018-02-25 22:55:05 -050047 def handle_missing_id(id, type, pkg, files, var, value):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060048 # For backwards compatibility we accept "1" in addition to "error"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050049 error_dynamic = d.getVar('USERADD_ERROR_DYNAMIC')
50 msg = "%s - %s: %sname %s does not have a static ID defined." % (d.getVar('PN'), pkg, type, id)
51 if files:
52 msg += " Add %s to one of these files: %s" % (id, files)
53 else:
54 msg += " %s file(s) not found in BBPATH: %s" % (var, value)
55 if error_dynamic == 'error' or error_dynamic == '1':
56 raise NotImplementedError(msg)
57 elif error_dynamic == 'warn':
58 bb.warn(msg)
59 elif error_dynamic == 'skip':
60 raise bb.parse.SkipRecipe(msg)
61
62 # Return a list of configuration files based on either the default
63 # files/group or the contents of USERADD_GID_TABLES, resp.
64 # files/passwd for USERADD_UID_TABLES.
65 # Paths are resolved via BBPATH.
66 def get_table_list(d, var, default):
67 files = []
Brad Bishop977dc1a2019-02-06 16:01:43 -050068 bbpath = d.getVar('BBPATH')
69 tables = d.getVar(var)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050070 if not tables:
71 tables = default
72 for conf_file in tables.split():
73 files.append(bb.utils.which(bbpath, conf_file))
74 return (' '.join(files), var, default)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060075
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076 # We parse and rewrite the useradd components
Brad Bishop6e60e8b2018-02-01 10:27:11 -050077 def rewrite_useradd(params, is_pkg):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050078 parser = oe.useradd.build_useradd_parser()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050079
80 newparams = []
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050081 users = None
Brad Bishopd7bf8c12018-02-25 22:55:05 -050082 for param in oe.useradd.split_commands(params):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050083 try:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050084 uaargs = parser.parse_args(oe.useradd.split_args(param))
Brad Bishopc342db32019-05-15 21:57:59 -040085 except Exception as e:
Patrick Williams213cb262021-08-07 19:21:33 -050086 bb.fatal("%s: Unable to parse arguments for USERADD_PARAM:%s '%s': %s" % (d.getVar('PN'), pkg, param, e))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050087
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050088 # Read all passwd files specified in USERADD_UID_TABLES or files/passwd
Patrick Williamsc124f4f2015-09-15 14:41:29 -050089 # Use the standard passwd layout:
90 # username:password:user_id:group_id:comment:home_directory:login_shell
Patrick Williamsc124f4f2015-09-15 14:41:29 -050091 #
92 # If a field is left blank, the original value will be used. The 'username'
93 # field is required.
94 #
95 # Note: we ignore the password field, as including even the hashed password
96 # in the useradd command may introduce a security hole. It's assumed that
97 # all new users get the default ('*' which prevents login) until the user is
98 # specifically configured by the system admin.
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050099 if not users:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500100 files, table_var, table_value = get_table_list(d, 'USERADD_UID_TABLES', 'files/passwd')
101 users = merge_files(files, 7)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500102
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500103 type = 'system user' if uaargs.system else 'normal user'
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500104 if uaargs.LOGIN not in users:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500105 handle_missing_id(uaargs.LOGIN, type, pkg, files, table_var, table_value)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500106 newparams.append(param)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500107 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500108
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500109 field = users[uaargs.LOGIN]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500110
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500111 if uaargs.uid and field[2] and (uaargs.uid != field[2]):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500112 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 Williamsd8c66bc2016-06-20 12:57:21 -0500113 uaargs.uid = field[2] or uaargs.uid
114
115 # Determine the possible groupname
116 # Unless the group name (or gid) is specified, we assume that the LOGIN is the groupname
117 #
118 # By default the system has creation of the matching groups enabled
119 # So if the implicit username-group creation is on, then the implicit groupname (LOGIN)
120 # is used, and we disable the user_group option.
121 #
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500122 if uaargs.gid:
123 uaargs.groupname = uaargs.gid
124 elif uaargs.user_group is not False:
125 uaargs.groupname = uaargs.LOGIN
126 else:
127 uaargs.groupname = 'users'
128 uaargs.groupid = field[3] or uaargs.groupname
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500129
130 if uaargs.groupid and uaargs.gid != uaargs.groupid:
131 newgroup = None
132 if not uaargs.groupid.isdigit():
133 # We don't have a group number, so we have to add a name
134 bb.debug(1, "Adding group %s!" % uaargs.groupid)
135 newgroup = "%s %s" % (' --system' if uaargs.system else '', uaargs.groupid)
136 elif uaargs.groupname and not uaargs.groupname.isdigit():
137 # We have a group name and a group number to assign it to
138 bb.debug(1, "Adding group %s (gid %s)!" % (uaargs.groupname, uaargs.groupid))
139 newgroup = "-g %s %s" % (uaargs.groupid, uaargs.groupname)
140 else:
141 # We want to add a group, but we don't know it's name... so we can't add the group...
142 # We have to assume the group has previously been added or we'll fail on the adduser...
143 # Note: specifying the actual gid is very rare in OE, usually the group name is specified.
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500144 bb.warn("%s: Changing gid for login %s to %s, verify configuration files!" % (d.getVar('PN'), uaargs.LOGIN, uaargs.groupid))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500145
146 uaargs.gid = uaargs.groupid
147 uaargs.user_group = None
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500148 if newgroup and is_pkg:
Patrick Williams213cb262021-08-07 19:21:33 -0500149 groupadd = d.getVar("GROUPADD_PARAM:%s" % pkg)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500150 if groupadd:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500151 # Only add the group if not already specified
152 if not uaargs.groupname in groupadd:
Patrick Williams213cb262021-08-07 19:21:33 -0500153 d.setVar("GROUPADD_PARAM:%s" % pkg, "%s; %s" % (groupadd, newgroup))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500154 else:
Patrick Williams213cb262021-08-07 19:21:33 -0500155 d.setVar("GROUPADD_PARAM:%s" % pkg, newgroup)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500156
157 uaargs.comment = "'%s'" % field[4] if field[4] else uaargs.comment
158 uaargs.home_dir = field[5] or uaargs.home_dir
159 uaargs.shell = field[6] or uaargs.shell
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500160
161 # Should be an error if a specific option is set...
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600162 if not uaargs.uid or not uaargs.uid.isdigit() or not uaargs.gid:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500163 handle_missing_id(uaargs.LOGIN, type, pkg, files, table_var, table_value)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500164
165 # Reconstruct the args...
166 newparam = ['', ' --defaults'][uaargs.defaults]
167 newparam += ['', ' --base-dir %s' % uaargs.base_dir][uaargs.base_dir != None]
168 newparam += ['', ' --comment %s' % uaargs.comment][uaargs.comment != None]
169 newparam += ['', ' --home-dir %s' % uaargs.home_dir][uaargs.home_dir != None]
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500170 newparam += ['', ' --expiredate %s' % uaargs.expiredate][uaargs.expiredate != None]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500171 newparam += ['', ' --inactive %s' % uaargs.inactive][uaargs.inactive != None]
172 newparam += ['', ' --gid %s' % uaargs.gid][uaargs.gid != None]
173 newparam += ['', ' --groups %s' % uaargs.groups][uaargs.groups != None]
174 newparam += ['', ' --skel %s' % uaargs.skel][uaargs.skel != None]
175 newparam += ['', ' --key %s' % uaargs.key][uaargs.key != None]
176 newparam += ['', ' --no-log-init'][uaargs.no_log_init]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500177 newparam += ['', ' --create-home'][uaargs.create_home is True]
178 newparam += ['', ' --no-create-home'][uaargs.create_home is False]
179 newparam += ['', ' --no-user-group'][uaargs.user_group is False]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500180 newparam += ['', ' --non-unique'][uaargs.non_unique]
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500181 if uaargs.password != None:
182 newparam += ['', ' --password %s' % uaargs.password][uaargs.password != None]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500183 newparam += ['', ' --root %s' % uaargs.root][uaargs.root != None]
184 newparam += ['', ' --system'][uaargs.system]
185 newparam += ['', ' --shell %s' % uaargs.shell][uaargs.shell != None]
186 newparam += ['', ' --uid %s' % uaargs.uid][uaargs.uid != None]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500187 newparam += ['', ' --user-group'][uaargs.user_group is True]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500188 newparam += ' %s' % uaargs.LOGIN
189
190 newparams.append(newparam)
191
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500192 return ";".join(newparams).strip()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500193
194 # We parse and rewrite the groupadd components
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500195 def rewrite_groupadd(params, is_pkg):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500196 parser = oe.useradd.build_groupadd_parser()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500197
198 newparams = []
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500199 groups = None
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500200 for param in oe.useradd.split_commands(params):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500201 try:
202 # If we're processing multiple lines, we could have left over values here...
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500203 gaargs = parser.parse_args(oe.useradd.split_args(param))
Brad Bishopc342db32019-05-15 21:57:59 -0400204 except Exception as e:
Patrick Williams213cb262021-08-07 19:21:33 -0500205 bb.fatal("%s: Unable to parse arguments for GROUPADD_PARAM:%s '%s': %s" % (d.getVar('PN'), pkg, param, e))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500206
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500207 # Read all group files specified in USERADD_GID_TABLES or files/group
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500208 # Use the standard group layout:
209 # groupname:password:group_id:group_members
210 #
211 # If a field is left blank, the original value will be used. The 'groupname' field
212 # is required.
213 #
214 # Note: similar to the passwd file, the 'password' filed is ignored
215 # Note: group_members is ignored, group members must be configured with the GROUPMEMS_PARAM
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500216 if not groups:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500217 files, table_var, table_value = get_table_list(d, 'USERADD_GID_TABLES', 'files/group')
218 groups = merge_files(files, 4)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500219
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500220 type = 'system group' if gaargs.system else 'normal group'
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500221 if gaargs.GROUP not in groups:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500222 handle_missing_id(gaargs.GROUP, type, pkg, files, table_var, table_value)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500223 newparams.append(param)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500224 continue
225
226 field = groups[gaargs.GROUP]
227
228 if field[2]:
229 if gaargs.gid and (gaargs.gid != field[2]):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500230 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 Williamsd8c66bc2016-06-20 12:57:21 -0500231 gaargs.gid = field[2]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500232
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600233 if not gaargs.gid or not gaargs.gid.isdigit():
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500234 handle_missing_id(gaargs.GROUP, type, pkg, files, table_var, table_value)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500235
236 # Reconstruct the args...
237 newparam = ['', ' --force'][gaargs.force]
238 newparam += ['', ' --gid %s' % gaargs.gid][gaargs.gid != None]
239 newparam += ['', ' --key %s' % gaargs.key][gaargs.key != None]
240 newparam += ['', ' --non-unique'][gaargs.non_unique]
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500241 if gaargs.password != None:
242 newparam += ['', ' --password %s' % gaargs.password][gaargs.password != None]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500243 newparam += ['', ' --root %s' % gaargs.root][gaargs.root != None]
244 newparam += ['', ' --system'][gaargs.system]
245 newparam += ' %s' % gaargs.GROUP
246
247 newparams.append(newparam)
248
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500249 return ";".join(newparams).strip()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500250
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600251 # The parsing of the current recipe depends on the content of
252 # the files listed in USERADD_UID/GID_TABLES. We need to tell bitbake
253 # about that explicitly to trigger re-parsing and thus re-execution of
254 # this code when the files change.
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500255 bbpath = d.getVar('BBPATH')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600256 for varname, default in (('USERADD_UID_TABLES', 'files/passwd'),
257 ('USERADD_GID_TABLES', 'files/group')):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500258 tables = d.getVar(varname)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600259 if not tables:
260 tables = default
261 for conf_file in tables.split():
262 bb.parse.mark_dependency(d, bb.utils.which(bbpath, conf_file))
263
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500264 # Load and process the users and groups, rewriting the adduser/addgroup params
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500265 useradd_packages = d.getVar('USERADD_PACKAGES') or ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500266
267 for pkg in useradd_packages.split():
268 # Groupmems doesn't have anything we might want to change, so simply validating
269 # is a bit of a waste -- only process useradd/groupadd
Patrick Williams213cb262021-08-07 19:21:33 -0500270 useradd_param = d.getVar('USERADD_PARAM:%s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500271 if useradd_param:
Patrick Williams213cb262021-08-07 19:21:33 -0500272 #bb.warn("Before: 'USERADD_PARAM:%s' - '%s'" % (pkg, useradd_param))
273 d.setVar('USERADD_PARAM:%s' % pkg, rewrite_useradd(useradd_param, True))
274 #bb.warn("After: 'USERADD_PARAM:%s' - '%s'" % (pkg, d.getVar('USERADD_PARAM:%s' % pkg)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500275
Patrick Williams213cb262021-08-07 19:21:33 -0500276 groupadd_param = d.getVar('GROUPADD_PARAM:%s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500277 if groupadd_param:
Patrick Williams213cb262021-08-07 19:21:33 -0500278 #bb.warn("Before: 'GROUPADD_PARAM:%s' - '%s'" % (pkg, groupadd_param))
279 d.setVar('GROUPADD_PARAM:%s' % pkg, rewrite_groupadd(groupadd_param, True))
280 #bb.warn("After: 'GROUPADD_PARAM:%s' - '%s'" % (pkg, d.getVar('GROUPADD_PARAM:%s' % pkg)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500281
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500282 # Load and process extra users and groups, rewriting only adduser/addgroup params
283 pkg = d.getVar('PN')
284 extrausers = d.getVar('EXTRA_USERS_PARAMS') or ""
285
286 #bb.warn("Before: 'EXTRA_USERS_PARAMS' - '%s'" % (d.getVar('EXTRA_USERS_PARAMS')))
287 new_extrausers = []
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500288 for cmd in oe.useradd.split_commands(extrausers):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500289 if re.match('''useradd (.*)''', cmd):
290 useradd_param = re.match('''useradd (.*)''', cmd).group(1)
291 useradd_param = rewrite_useradd(useradd_param, False)
292 cmd = 'useradd %s' % useradd_param
293 elif re.match('''groupadd (.*)''', cmd):
294 groupadd_param = re.match('''groupadd (.*)''', cmd).group(1)
295 groupadd_param = rewrite_groupadd(groupadd_param, False)
296 cmd = 'groupadd %s' % groupadd_param
297
298 new_extrausers.append(cmd)
299
300 new_extrausers.append('')
301 d.setVar('EXTRA_USERS_PARAMS', ';'.join(new_extrausers))
302 #bb.warn("After: 'EXTRA_USERS_PARAMS' - '%s'" % (d.getVar('EXTRA_USERS_PARAMS')))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500303
304
305python __anonymous() {
306 if not bb.data.inherits_class('nativesdk', d) \
307 and not bb.data.inherits_class('native', d):
308 try:
309 update_useradd_static_config(d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500310 except NotImplementedError as f:
311 bb.debug(1, "Skipping recipe %s: %s" % (d.getVar('PN'), f))
Brad Bishop316dfdd2018-06-25 12:45:53 -0400312 raise bb.parse.SkipRecipe(f)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500313}