1 |
From: Patrice Clement <monsieurp@g.o> |
2 |
|
3 |
* stage4/groups: create a a list of groups. |
4 |
* stage4/users: create a list of users. users can also be added to |
5 |
groups using the "foo.bar=wheel,audio,baz" format. |
6 |
* stage4/ssh_public_keys: copy an SSH public key into the stage4 user's home |
7 |
(.ssh/authorized_keys) and set the file permission to 0644. |
8 |
|
9 |
Bug: https://bugs.gentoo.org/236905 |
10 |
Signed-off-by: Patrice Clement <monsieurp@g.o> |
11 |
--- |
12 |
catalyst/base/stagebase.py | 70 ++++++++++++++++++++++++++++++++++++++ |
13 |
1 file changed, 70 insertions(+) |
14 |
|
15 |
diff --git a/catalyst/base/stagebase.py b/catalyst/base/stagebase.py |
16 |
index de1e30ef..76feb5f0 100644 |
17 |
--- a/catalyst/base/stagebase.py |
18 |
+++ b/catalyst/base/stagebase.py |
19 |
@@ -201,6 +201,9 @@ class StageBase(TargetBase, ClearBase, GenBase): |
20 |
self.set_packages() |
21 |
self.set_rm() |
22 |
self.set_linuxrc() |
23 |
+ self.set_groups() |
24 |
+ self.set_users() |
25 |
+ self.set_ssh_public_keys() |
26 |
self.set_busybox_config() |
27 |
self.set_overlay() |
28 |
self.set_repos() |
29 |
@@ -583,6 +586,39 @@ class StageBase(TargetBase, ClearBase, GenBase): |
30 |
self.settings[self.settings["spec_prefix"] + "/linuxrc"] |
31 |
del self.settings[self.settings["spec_prefix"] + "/linuxrc"] |
32 |
|
33 |
+ def set_groups(self): |
34 |
+ groups = self.settings["spec_prefix"] + "/groups" |
35 |
+ if groups in self.settings: |
36 |
+ if isinstance(self.settings[groups], str): |
37 |
+ self.settings["groups"] = self.settings[groups].split(",") |
38 |
+ self.settings["groups"] = self.settings[groups] |
39 |
+ del self.settings[groups] |
40 |
+ else: |
41 |
+ self.settings["groups"] = [] |
42 |
+ log.info('groups to create: %s' % self.settings["groups"]) |
43 |
+ |
44 |
+ def set_users(self): |
45 |
+ users = self.settings["spec_prefix"] + "/users" |
46 |
+ if users in self.settings: |
47 |
+ if isinstance(self.settings[users], str): |
48 |
+ self.settings["users"] = self.settings[users].split(",") |
49 |
+ self.settings["users"] = self.settings[users] |
50 |
+ del self.settings[users] |
51 |
+ else: |
52 |
+ self.settings["users"] = [] |
53 |
+ log.info('users to create: %s' % self.settings["users"]) |
54 |
+ |
55 |
+ def set_ssh_public_keys(self): |
56 |
+ ssh_public_keys = self.settings["spec_prefix"] + "/ssh_public_keys" |
57 |
+ if ssh_public_keys in self.settings: |
58 |
+ if isinstance(self.settings[ssh_public_keys], str): |
59 |
+ self.settings["ssh_public_keys"] = self.settings[ssh_public_keys].split(",") |
60 |
+ self.settings["ssh_public_keys"] = self.settings[ssh_public_keys] |
61 |
+ del self.settings[ssh_public_keys] |
62 |
+ else: |
63 |
+ self.settings["ssh_public_keys"] = [] |
64 |
+ log.info('ssh public keys to copy: %s' % self.settings["ssh_public_keys"]) |
65 |
+ |
66 |
def set_busybox_config(self): |
67 |
if self.settings["spec_prefix"] + "/busybox_config" in self.settings: |
68 |
if isinstance(self.settings[self.settings['spec_prefix'] + '/busybox_config'], str): |
69 |
@@ -894,6 +930,40 @@ class StageBase(TargetBase, ClearBase, GenBase): |
70 |
cmd(['rsync', '-a', x + '/', self.settings['stage_path']], |
71 |
env=self.env) |
72 |
|
73 |
+ def groups(self): |
74 |
+ for x in self.settings["groups"].split(): |
75 |
+ log.notice("Creating group: '%s'", x) |
76 |
+ cmd(["groupadd", "-R", self.settings['chroot_path'], x], env=self.env) |
77 |
+ |
78 |
+ def users(self): |
79 |
+ for x in self.settings["users"]: |
80 |
+ usr, grp = '', '' |
81 |
+ try: |
82 |
+ usr, grp = x.split("=") |
83 |
+ except ValueError: |
84 |
+ usr = x |
85 |
+ log.debug("users: '=' separator not found on line " + x) |
86 |
+ log.debug("users: missing separator means no groups found") |
87 |
+ uacmd = ["useradd", "-R", self.settings['chroot_path'], "-m", x] |
88 |
+ if grp != '': |
89 |
+ uacmd = ["useradd", "-R", self.settings['chroot_path'], "-m", "-G", grp, usr] |
90 |
+ log.notice("Creating user: '%s'", f"{usr}={grp}") |
91 |
+ cmd(uacmd, env=self.env) |
92 |
+ |
93 |
+ def ssh_public_keys(self): |
94 |
+ for x in self.settings["ssh_public_keys"]: |
95 |
+ usr, pub_key_src = '', '' |
96 |
+ try: |
97 |
+ usr, pub_key_src = x.split("=") |
98 |
+ except ValueError: |
99 |
+ raise CatalystError(f"ssh_public_keys: '=' separator not found on line {x}") |
100 |
+ log.notice("Copying SSH public key for user: '%s'", usr) |
101 |
+ pub_key_dest = self.settings['chroot_path'] + f"/home/{usr}/.ssh/authorized_keys" |
102 |
+ cpcmd = ["cp", "-av", pub_key_src, pub_key_dest] |
103 |
+ cmd(cpcmd, env=self.env) |
104 |
+ chcmd = ["chmod", "0644", pub_key_dest] |
105 |
+ cmd(chcmd, env=self.env) |
106 |
+ |
107 |
def bind(self): |
108 |
for x in [x for x in self.mount if self.mount[x]['enable']]: |
109 |
if str(self.mount[x]['source']) == 'config': |
110 |
-- |
111 |
2.34.1 |