diff options
author | Tudor Florea <tudor.florea@enea.com> | 2014-10-16 03:05:19 +0200 |
---|---|---|
committer | Tudor Florea <tudor.florea@enea.com> | 2014-10-16 03:05:19 +0200 |
commit | c527fd1f14c27855a37f2e8ac5346ce8d940ced2 (patch) | |
tree | bb002c1fdf011c41dbd2f0927bed23ecb5f83c97 /meta/classes/useradd-staticids.bbclass | |
download | poky-daisy-140929.tar.gz |
initial commit for Enea Linux 4.0-140929daisy-140929
Migrated from the internal git server on the daisy-enea-point-release branch
Signed-off-by: Tudor Florea <tudor.florea@enea.com>
Diffstat (limited to 'meta/classes/useradd-staticids.bbclass')
-rw-r--r-- | meta/classes/useradd-staticids.bbclass | 272 |
1 files changed, 272 insertions, 0 deletions
diff --git a/meta/classes/useradd-staticids.bbclass b/meta/classes/useradd-staticids.bbclass new file mode 100644 index 0000000000..a89cb10a4a --- /dev/null +++ b/meta/classes/useradd-staticids.bbclass | |||
@@ -0,0 +1,272 @@ | |||
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 | ||
5 | import re | ||
6 | |||
7 | class myArgumentParser( argparse.ArgumentParser ): | ||
8 | def _print_message(self, message, file=None): | ||
9 | bb.warn("%s - %s: %s" % (d.getVar('PN', True), pkg, message)) | ||
10 | |||
11 | # This should never be called... | ||
12 | def exit(self, status=0, message=None): | ||
13 | message = message or ("%s - %s: useradd.bbclass: Argument parsing exited" % (d.getVar('PN', True), pkg)) | ||
14 | error(message) | ||
15 | |||
16 | def error(self, message): | ||
17 | raise bb.build.FuncFailed(message) | ||
18 | |||
19 | # We parse and rewrite the useradd components | ||
20 | def rewrite_useradd(params): | ||
21 | # The following comes from --help on useradd from shadow | ||
22 | parser = myArgumentParser(prog='useradd') | ||
23 | parser.add_argument("-b", "--base-dir", metavar="BASE_DIR", help="base directory for the home directory of the new account") | ||
24 | parser.add_argument("-c", "--comment", metavar="COMMENT", help="GECOS field of the new account") | ||
25 | parser.add_argument("-d", "--home-dir", metavar="HOME_DIR", help="home directory of the new account") | ||
26 | parser.add_argument("-D", "--defaults", help="print or change default useradd configuration", action="store_true") | ||
27 | parser.add_argument("-e", "--expiredate", metavar="EXPIRE_DATE", help="expiration date of the new account") | ||
28 | parser.add_argument("-f", "--inactive", metavar="INACTIVE", help="password inactivity period of the new account") | ||
29 | parser.add_argument("-g", "--gid", metavar="GROUP", help="name or ID of the primary group of the new account") | ||
30 | parser.add_argument("-G", "--groups", metavar="GROUPS", help="list of supplementary groups of the new account") | ||
31 | parser.add_argument("-k", "--skel", metavar="SKEL_DIR", help="use this alternative skeleton directory") | ||
32 | parser.add_argument("-K", "--key", metavar="KEY=VALUE", help="override /etc/login.defs defaults") | ||
33 | parser.add_argument("-l", "--no-log-init", help="do not add the user to the lastlog and faillog databases", action="store_true") | ||
34 | parser.add_argument("-m", "--create-home", help="create the user's home directory", action="store_true") | ||
35 | parser.add_argument("-M", "--no-create-home", help="do not create the user's home directory", action="store_true") | ||
36 | parser.add_argument("-N", "--no-user-group", help="do not create a group with the same name as the user", action="store_true") | ||
37 | parser.add_argument("-o", "--non-unique", help="allow to create users with duplicate (non-unique UID)", action="store_true") | ||
38 | parser.add_argument("-p", "--password", metavar="PASSWORD", help="encrypted password of the new account") | ||
39 | parser.add_argument("-R", "--root", metavar="CHROOT_DIR", help="directory to chroot into") | ||
40 | parser.add_argument("-r", "--system", help="create a system account", action="store_true") | ||
41 | parser.add_argument("-s", "--shell", metavar="SHELL", help="login shell of the new account") | ||
42 | parser.add_argument("-u", "--uid", metavar="UID", help="user ID of the new account") | ||
43 | parser.add_argument("-U", "--user-group", help="create a group with the same name as the user", action="store_true") | ||
44 | parser.add_argument("LOGIN", help="Login name of the new user") | ||
45 | |||
46 | # Return a list of configuration files based on either the default | ||
47 | # files/passwd or the contents of USERADD_UID_TABLES | ||
48 | # paths are resulved via BBPATH | ||
49 | def get_passwd_list(d): | ||
50 | str = "" | ||
51 | bbpath = d.getVar('BBPATH', True) | ||
52 | passwd_tables = d.getVar('USERADD_UID_TABLES', True) | ||
53 | if not passwd_tables: | ||
54 | passwd_tables = 'files/passwd' | ||
55 | for conf_file in passwd_tables.split(): | ||
56 | str += " %s" % bb.utils.which(bbpath, conf_file) | ||
57 | return str | ||
58 | |||
59 | newparams = [] | ||
60 | for param in re.split('''[ \t]*;[ \t]*(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', params): | ||
61 | param=param.strip() | ||
62 | try: | ||
63 | uaargs = parser.parse_args(re.split('''[ \t]*(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', param)) | ||
64 | except: | ||
65 | raise bb.build.FuncFailed("%s: Unable to parse arguments for USERADD_PARAM_%s: '%s'" % (d.getVar('PN', True), pkg, param)) | ||
66 | |||
67 | # files/passwd or the contents of USERADD_UID_TABLES | ||
68 | # Use the standard passwd layout: | ||
69 | # username:password:user_id:group_id:comment:home_directory:login_shell | ||
70 | # (we want to process in reverse order, as 'last found' in the list wins) | ||
71 | # | ||
72 | # If a field is left blank, the original value will be used. The 'username' | ||
73 | # field is required. | ||
74 | # | ||
75 | # Note: we ignore the password field, as including even the hashed password | ||
76 | # in the useradd command may introduce a security hole. It's assumed that | ||
77 | # all new users get the default ('*' which prevents login) until the user is | ||
78 | # specifically configured by the system admin. | ||
79 | for conf in get_passwd_list(d).split()[::-1]: | ||
80 | if os.path.exists(conf): | ||
81 | f = open(conf, "r") | ||
82 | for line in f: | ||
83 | if line.startswith('#'): | ||
84 | continue | ||
85 | field = line.rstrip().split(":") | ||
86 | if field[0] == uaargs.LOGIN: | ||
87 | if uaargs.uid and field[2] and (uaargs.uid != field[2]): | ||
88 | 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])) | ||
89 | uaargs.uid = [field[2], uaargs.uid][not field[2]] | ||
90 | |||
91 | # Determine the possible groupname | ||
92 | # Unless the group name (or gid) is specified, we assume that the LOGIN is the groupname | ||
93 | # | ||
94 | # By default the system has creation of the matching groups enabled | ||
95 | # So if the implicit username-group creation is on, then the implicit groupname (LOGIN) | ||
96 | # is used, and we disable the user_group option. | ||
97 | # | ||
98 | uaargs.groupname = [uaargs.gid, uaargs.LOGIN][not uaargs.gid or uaargs.user_group] | ||
99 | uaargs.groupid = [uaargs.gid, uaargs.groupname][not uaargs.gid] | ||
100 | uaargs.groupid = [field[3], uaargs.groupid][not field[3]] | ||
101 | |||
102 | if not uaargs.gid or uaargs.gid != uaargs.groupid: | ||
103 | if (uaargs.groupid and uaargs.groupid.isdigit()) and (uaargs.groupname and uaargs.groupname.isdigit()) and (uaargs.groupid != uaargs.groupname): | ||
104 | # We want to add a group, but we don't know it's name... so we can't add the group... | ||
105 | # We have to assume the group has previously been added or we'll fail on the adduser... | ||
106 | # Note: specifying the actual gid is very rare in OE, usually the group name is specified. | ||
107 | bb.warn("%s: Changing gid for login %s from (%s) to (%s), verify configuration files!" % (d.getVar('PN', True), uaargs.LOGIN, uaargs.groupname, uaargs.gid)) | ||
108 | elif (uaargs.groupid and not uaargs.groupid.isdigit()) and uaargs.groupid == uaargs.groupname: | ||
109 | # We don't have a number, so we have to add a name | ||
110 | bb.debug(1, "Adding group %s!" % (uaargs.groupname)) | ||
111 | uaargs.gid = uaargs.groupid | ||
112 | uaargs.user_group = False | ||
113 | groupadd = d.getVar("GROUPADD_PARAM_%s" % pkg, True) | ||
114 | newgroup = "%s %s" % (['', ' --system'][uaargs.system], uaargs.groupname) | ||
115 | if groupadd: | ||
116 | d.setVar("GROUPADD_PARAM_%s" % pkg, "%s ; %s" % (groupadd, newgroup)) | ||
117 | else: | ||
118 | d.setVar("GROUPADD_PARAM_%s" % pkg, newgroup) | ||
119 | elif uaargs.groupname and (uaargs.groupid and uaargs.groupid.isdigit()): | ||
120 | # We have a group name and a group number to assign it to | ||
121 | bb.debug(1, "Adding group %s gid (%s)!" % (uaargs.groupname, uaargs.groupid)) | ||
122 | uaargs.gid = uaargs.groupid | ||
123 | uaargs.user_group = False | ||
124 | groupadd = d.getVar("GROUPADD_PARAM_%s" % pkg, True) | ||
125 | newgroup = "-g %s %s" % (uaargs.gid, uaargs.groupname) | ||
126 | if groupadd: | ||
127 | d.setVar("GROUPADD_PARAM_%s" % pkg, "%s ; %s" % (groupadd, newgroup)) | ||
128 | else: | ||
129 | d.setVar("GROUPADD_PARAM_%s" % pkg, newgroup) | ||
130 | |||
131 | uaargs.comment = ["'%s'" % field[4], uaargs.comment][not field[4]] | ||
132 | uaargs.home_dir = [field[5], uaargs.home_dir][not field[5]] | ||
133 | uaargs.shell = [field[6], uaargs.shell][not field[6]] | ||
134 | break | ||
135 | |||
136 | # Should be an error if a specific option is set... | ||
137 | if d.getVar('USERADD_ERROR_DYNAMIC', True) == '1' and not ((uaargs.uid and uaargs.uid.isdigit()) and uaargs.gid): | ||
138 | #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)) | ||
139 | raise bb.build.FuncFailed("%s - %s: Username %s does not have a static uid defined." % (d.getVar('PN', True), pkg, uaargs.LOGIN)) | ||
140 | |||
141 | # Reconstruct the args... | ||
142 | newparam = ['', ' --defaults'][uaargs.defaults] | ||
143 | newparam += ['', ' --base-dir %s' % uaargs.base_dir][uaargs.base_dir != None] | ||
144 | newparam += ['', ' --comment %s' % uaargs.comment][uaargs.comment != None] | ||
145 | newparam += ['', ' --home-dir %s' % uaargs.home_dir][uaargs.home_dir != None] | ||
146 | newparam += ['', ' --expiredata %s' % uaargs.expiredate][uaargs.expiredate != None] | ||
147 | newparam += ['', ' --inactive %s' % uaargs.inactive][uaargs.inactive != None] | ||
148 | newparam += ['', ' --gid %s' % uaargs.gid][uaargs.gid != None] | ||
149 | newparam += ['', ' --groups %s' % uaargs.groups][uaargs.groups != None] | ||
150 | newparam += ['', ' --skel %s' % uaargs.skel][uaargs.skel != None] | ||
151 | newparam += ['', ' --key %s' % uaargs.key][uaargs.key != None] | ||
152 | newparam += ['', ' --no-log-init'][uaargs.no_log_init] | ||
153 | newparam += ['', ' --create-home'][uaargs.create_home] | ||
154 | newparam += ['', ' --no-create-home'][uaargs.no_create_home] | ||
155 | newparam += ['', ' --no-user-group'][uaargs.no_user_group] | ||
156 | newparam += ['', ' --non-unique'][uaargs.non_unique] | ||
157 | newparam += ['', ' --password %s' % uaargs.password][uaargs.password != None] | ||
158 | newparam += ['', ' --root %s' % uaargs.root][uaargs.root != None] | ||
159 | newparam += ['', ' --system'][uaargs.system] | ||
160 | newparam += ['', ' --shell %s' % uaargs.shell][uaargs.shell != None] | ||
161 | newparam += ['', ' --uid %s' % uaargs.uid][uaargs.uid != None] | ||
162 | newparam += ['', ' --user-group'][uaargs.user_group] | ||
163 | newparam += ' %s' % uaargs.LOGIN | ||
164 | |||
165 | newparams.append(newparam) | ||
166 | |||
167 | return " ;".join(newparams).strip() | ||
168 | |||
169 | # We parse and rewrite the groupadd components | ||
170 | def rewrite_groupadd(params): | ||
171 | # The following comes from --help on groupadd from shadow | ||
172 | parser = myArgumentParser(prog='groupadd') | ||
173 | 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") | ||
174 | parser.add_argument("-g", "--gid", metavar="GID", help="use GID for the new group") | ||
175 | parser.add_argument("-K", "--key", metavar="KEY=VALUE", help="override /etc/login.defs defaults") | ||
176 | parser.add_argument("-o", "--non-unique", help="allow to create groups with duplicate (non-unique) GID", action="store_true") | ||
177 | parser.add_argument("-p", "--password", metavar="PASSWORD", help="use this encrypted password for the new group") | ||
178 | parser.add_argument("-R", "--root", metavar="CHROOT_DIR", help="directory to chroot into") | ||
179 | parser.add_argument("-r", "--system", help="create a system account", action="store_true") | ||
180 | parser.add_argument("GROUP", help="Group name of the new group") | ||
181 | |||
182 | # Return a list of configuration files based on either the default | ||
183 | # files/group or the contents of USERADD_GID_TABLES | ||
184 | # paths are resulved via BBPATH | ||
185 | def get_group_list(d): | ||
186 | str = "" | ||
187 | bbpath = d.getVar('BBPATH', True) | ||
188 | group_tables = d.getVar('USERADD_GID_TABLES', True) | ||
189 | if not group_tables: | ||
190 | group_tables = 'files/group' | ||
191 | for conf_file in group_tables.split(): | ||
192 | str += " %s" % bb.utils.which(bbpath, conf_file) | ||
193 | return str | ||
194 | |||
195 | newparams = [] | ||
196 | for param in re.split('''[ \t]*;[ \t]*(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', params): | ||
197 | param=param.strip() | ||
198 | try: | ||
199 | # If we're processing multiple lines, we could have left over values here... | ||
200 | gaargs = parser.parse_args(re.split('''[ \t]*(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', param)) | ||
201 | except: | ||
202 | raise bb.build.FuncFailed("%s: Unable to parse arguments for GROUPADD_PARAM_%s: '%s'" % (d.getVar('PN', True), pkg, param)) | ||
203 | |||
204 | # Need to iterate over layers and open the right file(s) | ||
205 | # Use the standard group layout: | ||
206 | # groupname:password:group_id:group_members | ||
207 | # | ||
208 | # If a field is left blank, the original value will be used. The 'groupname' field | ||
209 | # is required. | ||
210 | # | ||
211 | # Note: similar to the passwd file, the 'password' filed is ignored | ||
212 | # Note: group_members is ignored, group members must be configured with the GROUPMEMS_PARAM | ||
213 | for conf in get_group_list(d).split()[::-1]: | ||
214 | if os.path.exists(conf): | ||
215 | f = open(conf, "r") | ||
216 | for line in f: | ||
217 | if line.startswith('#'): | ||
218 | continue | ||
219 | field = line.rstrip().split(":") | ||
220 | if field[0] == gaargs.GROUP and field[2]: | ||
221 | if gaargs.gid and (gaargs.gid != field[2]): | ||
222 | 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])) | ||
223 | gaargs.gid = field[2] | ||
224 | break | ||
225 | |||
226 | if d.getVar('USERADD_ERROR_DYNAMIC', True) == '1' and not (gaargs.gid and gaargs.gid.isdigit()): | ||
227 | #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)) | ||
228 | raise bb.build.FuncFailed("%s - %s: Groupname %s does not have a static gid defined." % (d.getVar('PN', True), pkg, gaargs.GROUP)) | ||
229 | |||
230 | # Reconstruct the args... | ||
231 | newparam = ['', ' --force'][gaargs.force] | ||
232 | newparam += ['', ' --gid %s' % gaargs.gid][gaargs.gid != None] | ||
233 | newparam += ['', ' --key %s' % gaargs.key][gaargs.key != None] | ||
234 | newparam += ['', ' --non-unique'][gaargs.non_unique] | ||
235 | newparam += ['', ' --password %s' % gaargs.password][gaargs.password != None] | ||
236 | newparam += ['', ' --root %s' % gaargs.root][gaargs.root != None] | ||
237 | newparam += ['', ' --system'][gaargs.system] | ||
238 | newparam += ' %s' % gaargs.GROUP | ||
239 | |||
240 | newparams.append(newparam) | ||
241 | |||
242 | return " ;".join(newparams).strip() | ||
243 | |||
244 | # Load and process the users and groups, rewriting the adduser/addgroup params | ||
245 | useradd_packages = d.getVar('USERADD_PACKAGES', True) | ||
246 | |||
247 | for pkg in useradd_packages.split(): | ||
248 | # Groupmems doesn't have anything we might want to change, so simply validating | ||
249 | # is a bit of a waste -- only process useradd/groupadd | ||
250 | useradd_param = d.getVar('USERADD_PARAM_%s' % pkg, True) | ||
251 | if useradd_param: | ||
252 | #bb.warn("Before: 'USERADD_PARAM_%s' - '%s'" % (pkg, useradd_param)) | ||
253 | d.setVar('USERADD_PARAM_%s' % pkg, rewrite_useradd(useradd_param)) | ||
254 | #bb.warn("After: 'USERADD_PARAM_%s' - '%s'" % (pkg, d.getVar('USERADD_PARAM_%s' % pkg, True))) | ||
255 | |||
256 | groupadd_param = d.getVar('GROUPADD_PARAM_%s' % pkg, True) | ||
257 | if groupadd_param: | ||
258 | #bb.warn("Before: 'GROUPADD_PARAM_%s' - '%s'" % (pkg, groupadd_param)) | ||
259 | d.setVar('GROUPADD_PARAM_%s' % pkg, rewrite_groupadd(groupadd_param)) | ||
260 | #bb.warn("After: 'GROUPADD_PARAM_%s' - '%s'" % (pkg, d.getVar('GROUPADD_PARAM_%s' % pkg, True))) | ||
261 | |||
262 | |||
263 | |||
264 | python __anonymous() { | ||
265 | if not bb.data.inherits_class('nativesdk', d) \ | ||
266 | and not bb.data.inherits_class('native', d): | ||
267 | try: | ||
268 | update_useradd_static_config(d) | ||
269 | except bb.build.FuncFailed as f: | ||
270 | bb.debug(1, "Skipping recipe %s: %s" % (d.getVar('PN', True), f)) | ||
271 | raise bb.parse.SkipPackage(f) | ||
272 | } | ||