blob: 70d59e55737462a51b38ac44f2b8bfe145136e31 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# 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
3def update_useradd_static_config(d):
Patrick Williamsf1e5d692016-03-30 15:21:19 -05004 import itertools
Patrick Williamsc124f4f2015-09-15 14:41:29 -05005 import re
Patrick Williamsc0f7c042017-02-23 20:41:17 -06006 import errno
Brad Bishopd7bf8c12018-02-25 22:55:05 -05007 import oe.useradd
Patrick Williamsc124f4f2015-09-15 14:41:29 -05008
Patrick Williamsf1e5d692016-03-30 15:21:19 -05009 def list_extend(iterable, length, obj = None):
10 """Ensure that iterable is the specified length by extending with obj
11 and return it as a list"""
12 return list(itertools.islice(itertools.chain(iterable, itertools.repeat(obj)), length))
13
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050014 def merge_files(file_list, exp_fields):
15 """Read each passwd/group file in file_list, split each line and create
16 a dictionary with the user/group names as keys and the split lines as
17 values. If the user/group name already exists in the dictionary, then
18 update any fields in the list with the values from the new list (if they
19 are set)."""
20 id_table = dict()
21 for conf in file_list.split():
Patrick Williamsc0f7c042017-02-23 20:41:17 -060022 try:
23 with open(conf, "r") as f:
24 for line in f:
25 if line.startswith('#'):
26 continue
27 # Make sure there always are at least exp_fields
28 # elements in the field list. This allows for leaving
29 # out trailing colons in the files.
30 fields = list_extend(line.rstrip().split(":"), exp_fields)
31 if fields[0] not in id_table:
32 id_table[fields[0]] = fields
33 else:
34 id_table[fields[0]] = list(map(lambda x, y: x or y, fields, id_table[fields[0]]))
35 except IOError as e:
36 if e.errno == errno.ENOENT:
37 pass
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050038
39 return id_table
40
Brad Bishopd7bf8c12018-02-25 22:55:05 -050041 def handle_missing_id(id, type, pkg, files, var, value):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060042 # For backwards compatibility we accept "1" in addition to "error"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050043 error_dynamic = d.getVar('USERADD_ERROR_DYNAMIC')
44 msg = "%s - %s: %sname %s does not have a static ID defined." % (d.getVar('PN'), pkg, type, id)
45 if files:
46 msg += " Add %s to one of these files: %s" % (id, files)
47 else:
48 msg += " %s file(s) not found in BBPATH: %s" % (var, value)
49 if error_dynamic == 'error' or error_dynamic == '1':
50 raise NotImplementedError(msg)
51 elif error_dynamic == 'warn':
52 bb.warn(msg)
53 elif error_dynamic == 'skip':
54 raise bb.parse.SkipRecipe(msg)
55
56 # Return a list of configuration files based on either the default
57 # files/group or the contents of USERADD_GID_TABLES, resp.
58 # files/passwd for USERADD_UID_TABLES.
59 # Paths are resolved via BBPATH.
60 def get_table_list(d, var, default):
61 files = []
Brad Bishop977dc1a2019-02-06 16:01:43 -050062 bbpath = d.getVar('BBPATH')
63 tables = d.getVar(var)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050064 if not tables:
65 tables = default
66 for conf_file in tables.split():
67 files.append(bb.utils.which(bbpath, conf_file))
68 return (' '.join(files), var, default)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060069
Patrick Williamsc124f4f2015-09-15 14:41:29 -050070 # We parse and rewrite the useradd components
Brad Bishop6e60e8b2018-02-01 10:27:11 -050071 def rewrite_useradd(params, is_pkg):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050072 parser = oe.useradd.build_useradd_parser()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050073
74 newparams = []
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050075 users = None
Brad Bishopd7bf8c12018-02-25 22:55:05 -050076 for param in oe.useradd.split_commands(params):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050077 try:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050078 uaargs = parser.parse_args(oe.useradd.split_args(param))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050079 except:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050080 bb.fatal("%s: Unable to parse arguments for USERADD_PARAM_%s: '%s'" % (d.getVar('PN'), pkg, param))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050082 # Read all passwd files specified in USERADD_UID_TABLES or files/passwd
Patrick Williamsc124f4f2015-09-15 14:41:29 -050083 # Use the standard passwd layout:
84 # username:password:user_id:group_id:comment:home_directory:login_shell
Patrick Williamsc124f4f2015-09-15 14:41:29 -050085 #
86 # If a field is left blank, the original value will be used. The 'username'
87 # field is required.
88 #
89 # Note: we ignore the password field, as including even the hashed password
90 # in the useradd command may introduce a security hole. It's assumed that
91 # all new users get the default ('*' which prevents login) until the user is
92 # specifically configured by the system admin.
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050093 if not users:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050094 files, table_var, table_value = get_table_list(d, 'USERADD_UID_TABLES', 'files/passwd')
95 users = merge_files(files, 7)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050096
Brad Bishopd7bf8c12018-02-25 22:55:05 -050097 type = 'system user' if uaargs.system else 'normal user'
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050098 if uaargs.LOGIN not in users:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050099 handle_missing_id(uaargs.LOGIN, type, pkg, files, table_var, table_value)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500100 newparams.append(param)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500101 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500102
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500103 field = users[uaargs.LOGIN]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500104
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500105 if uaargs.uid and field[2] and (uaargs.uid != field[2]):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500106 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 -0500107 uaargs.uid = field[2] or uaargs.uid
108
109 # Determine the possible groupname
110 # Unless the group name (or gid) is specified, we assume that the LOGIN is the groupname
111 #
112 # By default the system has creation of the matching groups enabled
113 # So if the implicit username-group creation is on, then the implicit groupname (LOGIN)
114 # is used, and we disable the user_group option.
115 #
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500116 if uaargs.gid:
117 uaargs.groupname = uaargs.gid
118 elif uaargs.user_group is not False:
119 uaargs.groupname = uaargs.LOGIN
120 else:
121 uaargs.groupname = 'users'
122 uaargs.groupid = field[3] or uaargs.groupname
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500123
124 if uaargs.groupid and uaargs.gid != uaargs.groupid:
125 newgroup = None
126 if not uaargs.groupid.isdigit():
127 # We don't have a group number, so we have to add a name
128 bb.debug(1, "Adding group %s!" % uaargs.groupid)
129 newgroup = "%s %s" % (' --system' if uaargs.system else '', uaargs.groupid)
130 elif uaargs.groupname and not uaargs.groupname.isdigit():
131 # We have a group name and a group number to assign it to
132 bb.debug(1, "Adding group %s (gid %s)!" % (uaargs.groupname, uaargs.groupid))
133 newgroup = "-g %s %s" % (uaargs.groupid, uaargs.groupname)
134 else:
135 # We want to add a group, but we don't know it's name... so we can't add the group...
136 # We have to assume the group has previously been added or we'll fail on the adduser...
137 # Note: specifying the actual gid is very rare in OE, usually the group name is specified.
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500138 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 -0500139
140 uaargs.gid = uaargs.groupid
141 uaargs.user_group = None
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500142 if newgroup and is_pkg:
143 groupadd = d.getVar("GROUPADD_PARAM_%s" % pkg)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500144 if groupadd:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500145 # Only add the group if not already specified
146 if not uaargs.groupname in groupadd:
147 d.setVar("GROUPADD_PARAM_%s" % pkg, "%s; %s" % (groupadd, newgroup))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500148 else:
149 d.setVar("GROUPADD_PARAM_%s" % pkg, newgroup)
150
151 uaargs.comment = "'%s'" % field[4] if field[4] else uaargs.comment
152 uaargs.home_dir = field[5] or uaargs.home_dir
153 uaargs.shell = field[6] or uaargs.shell
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500154
155 # Should be an error if a specific option is set...
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600156 if not uaargs.uid or not uaargs.uid.isdigit() or not uaargs.gid:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500157 handle_missing_id(uaargs.LOGIN, type, pkg, files, table_var, table_value)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500158
159 # Reconstruct the args...
160 newparam = ['', ' --defaults'][uaargs.defaults]
161 newparam += ['', ' --base-dir %s' % uaargs.base_dir][uaargs.base_dir != None]
162 newparam += ['', ' --comment %s' % uaargs.comment][uaargs.comment != None]
163 newparam += ['', ' --home-dir %s' % uaargs.home_dir][uaargs.home_dir != None]
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500164 newparam += ['', ' --expiredate %s' % uaargs.expiredate][uaargs.expiredate != None]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500165 newparam += ['', ' --inactive %s' % uaargs.inactive][uaargs.inactive != None]
166 newparam += ['', ' --gid %s' % uaargs.gid][uaargs.gid != None]
167 newparam += ['', ' --groups %s' % uaargs.groups][uaargs.groups != None]
168 newparam += ['', ' --skel %s' % uaargs.skel][uaargs.skel != None]
169 newparam += ['', ' --key %s' % uaargs.key][uaargs.key != None]
170 newparam += ['', ' --no-log-init'][uaargs.no_log_init]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500171 newparam += ['', ' --create-home'][uaargs.create_home is True]
172 newparam += ['', ' --no-create-home'][uaargs.create_home is False]
173 newparam += ['', ' --no-user-group'][uaargs.user_group is False]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500174 newparam += ['', ' --non-unique'][uaargs.non_unique]
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500175 if uaargs.password != None:
176 newparam += ['', ' --password %s' % uaargs.password][uaargs.password != None]
177 elif uaargs.clear_password:
178 newparam += ['', ' --clear-password %s' % uaargs.clear_password][uaargs.clear_password != None]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500179 newparam += ['', ' --root %s' % uaargs.root][uaargs.root != None]
180 newparam += ['', ' --system'][uaargs.system]
181 newparam += ['', ' --shell %s' % uaargs.shell][uaargs.shell != None]
182 newparam += ['', ' --uid %s' % uaargs.uid][uaargs.uid != None]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500183 newparam += ['', ' --user-group'][uaargs.user_group is True]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500184 newparam += ' %s' % uaargs.LOGIN
185
186 newparams.append(newparam)
187
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500188 return ";".join(newparams).strip()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500189
190 # We parse and rewrite the groupadd components
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500191 def rewrite_groupadd(params, is_pkg):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500192 parser = oe.useradd.build_groupadd_parser()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500193
194 newparams = []
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500195 groups = None
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500196 for param in oe.useradd.split_commands(params):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500197 try:
198 # If we're processing multiple lines, we could have left over values here...
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500199 gaargs = parser.parse_args(oe.useradd.split_args(param))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500200 except:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500201 bb.fatal("%s: Unable to parse arguments for GROUPADD_PARAM_%s: '%s'" % (d.getVar('PN'), pkg, param))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500202
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500203 # Read all group files specified in USERADD_GID_TABLES or files/group
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500204 # Use the standard group layout:
205 # groupname:password:group_id:group_members
206 #
207 # If a field is left blank, the original value will be used. The 'groupname' field
208 # is required.
209 #
210 # Note: similar to the passwd file, the 'password' filed is ignored
211 # Note: group_members is ignored, group members must be configured with the GROUPMEMS_PARAM
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500212 if not groups:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500213 files, table_var, table_value = get_table_list(d, 'USERADD_GID_TABLES', 'files/group')
214 groups = merge_files(files, 4)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500215
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500216 type = 'system group' if gaargs.system else 'normal group'
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500217 if gaargs.GROUP not in groups:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500218 handle_missing_id(gaargs.GROUP, type, pkg, files, table_var, table_value)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500219 newparams.append(param)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500220 continue
221
222 field = groups[gaargs.GROUP]
223
224 if field[2]:
225 if gaargs.gid and (gaargs.gid != field[2]):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500226 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 -0500227 gaargs.gid = field[2]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500228
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600229 if not gaargs.gid or not gaargs.gid.isdigit():
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500230 handle_missing_id(gaargs.GROUP, type, pkg, files, table_var, table_value)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500231
232 # Reconstruct the args...
233 newparam = ['', ' --force'][gaargs.force]
234 newparam += ['', ' --gid %s' % gaargs.gid][gaargs.gid != None]
235 newparam += ['', ' --key %s' % gaargs.key][gaargs.key != None]
236 newparam += ['', ' --non-unique'][gaargs.non_unique]
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500237 if gaargs.password != None:
238 newparam += ['', ' --password %s' % gaargs.password][gaargs.password != None]
239 elif gaargs.clear_password:
240 newparam += ['', ' --clear-password %s' % gaargs.clear_password][gaargs.clear_password != None]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500241 newparam += ['', ' --root %s' % gaargs.root][gaargs.root != None]
242 newparam += ['', ' --system'][gaargs.system]
243 newparam += ' %s' % gaargs.GROUP
244
245 newparams.append(newparam)
246
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500247 return ";".join(newparams).strip()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500248
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600249 # The parsing of the current recipe depends on the content of
250 # the files listed in USERADD_UID/GID_TABLES. We need to tell bitbake
251 # about that explicitly to trigger re-parsing and thus re-execution of
252 # this code when the files change.
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500253 bbpath = d.getVar('BBPATH')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600254 for varname, default in (('USERADD_UID_TABLES', 'files/passwd'),
255 ('USERADD_GID_TABLES', 'files/group')):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500256 tables = d.getVar(varname)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600257 if not tables:
258 tables = default
259 for conf_file in tables.split():
260 bb.parse.mark_dependency(d, bb.utils.which(bbpath, conf_file))
261
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500262 # Load and process the users and groups, rewriting the adduser/addgroup params
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500263 useradd_packages = d.getVar('USERADD_PACKAGES') or ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500264
265 for pkg in useradd_packages.split():
266 # Groupmems doesn't have anything we might want to change, so simply validating
267 # is a bit of a waste -- only process useradd/groupadd
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500268 useradd_param = d.getVar('USERADD_PARAM_%s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500269 if useradd_param:
270 #bb.warn("Before: 'USERADD_PARAM_%s' - '%s'" % (pkg, useradd_param))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500271 d.setVar('USERADD_PARAM_%s' % pkg, rewrite_useradd(useradd_param, True))
272 #bb.warn("After: 'USERADD_PARAM_%s' - '%s'" % (pkg, d.getVar('USERADD_PARAM_%s' % pkg)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500273
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500274 groupadd_param = d.getVar('GROUPADD_PARAM_%s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500275 if groupadd_param:
276 #bb.warn("Before: 'GROUPADD_PARAM_%s' - '%s'" % (pkg, groupadd_param))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500277 d.setVar('GROUPADD_PARAM_%s' % pkg, rewrite_groupadd(groupadd_param, True))
278 #bb.warn("After: 'GROUPADD_PARAM_%s' - '%s'" % (pkg, d.getVar('GROUPADD_PARAM_%s' % pkg)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500279
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500280 # Load and process extra users and groups, rewriting only adduser/addgroup params
281 pkg = d.getVar('PN')
282 extrausers = d.getVar('EXTRA_USERS_PARAMS') or ""
283
284 #bb.warn("Before: 'EXTRA_USERS_PARAMS' - '%s'" % (d.getVar('EXTRA_USERS_PARAMS')))
285 new_extrausers = []
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500286 for cmd in oe.useradd.split_commands(extrausers):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500287 if re.match('''useradd (.*)''', cmd):
288 useradd_param = re.match('''useradd (.*)''', cmd).group(1)
289 useradd_param = rewrite_useradd(useradd_param, False)
290 cmd = 'useradd %s' % useradd_param
291 elif re.match('''groupadd (.*)''', cmd):
292 groupadd_param = re.match('''groupadd (.*)''', cmd).group(1)
293 groupadd_param = rewrite_groupadd(groupadd_param, False)
294 cmd = 'groupadd %s' % groupadd_param
295
296 new_extrausers.append(cmd)
297
298 new_extrausers.append('')
299 d.setVar('EXTRA_USERS_PARAMS', ';'.join(new_extrausers))
300 #bb.warn("After: 'EXTRA_USERS_PARAMS' - '%s'" % (d.getVar('EXTRA_USERS_PARAMS')))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500301
302
303python __anonymous() {
304 if not bb.data.inherits_class('nativesdk', d) \
305 and not bb.data.inherits_class('native', d):
306 try:
307 update_useradd_static_config(d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500308 except NotImplementedError as f:
309 bb.debug(1, "Skipping recipe %s: %s" % (d.getVar('PN'), f))
Brad Bishop316dfdd2018-06-25 12:45:53 -0400310 raise bb.parse.SkipRecipe(f)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500311}