Skip to content

Commit 80091ce

Browse files
committed
Use CMS user's actual primary group for permissions setup
It can happen that the CMS user's primary group name doesn't match the user name (in case of a pre-existing user), so we should use the actual group name for setting file permissions. We don't try to always create and use a "cmsuser" group instead, because this wouldn't work within a single CI session.
1 parent 98cf097 commit 80091ce

2 files changed

Lines changed: 13 additions & 18 deletions

File tree

cms/util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2323

2424
import argparse
25-
import grp
2625
import itertools
2726
import logging
2827
import netifaces
2928
import os
29+
import pwd
3030
import stat
3131
import sys
3232

@@ -56,7 +56,7 @@ def mkdir(path):
5656
else:
5757
try:
5858
os.chmod(path, 0o770)
59-
cmsuser_gid = grp.getgrnam(config.cmsuser).gr_gid
59+
cmsuser_gid = pwd.getpwnam(config.cmsuser).pw_gid
6060
os.chown(path, -1, cmsuser_gid)
6161
except OSError:
6262
os.rmdir(path)

prerequisites.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def install_isolate():
207207
assert_root()
208208
root = pwd.getpwnam("root")
209209
try:
210-
cmsuser_grp = grp.getgrnam(CMSUSER)
210+
cmsuser_grp = grp.getgrgid(pwd.getpwnam(CMSUSER).pw_gid)
211211
except:
212212
print("[Error] The user %s doesn't exist yet" % CMSUSER)
213213
print("[Error] You need to run the install command at least once")
@@ -279,22 +279,15 @@ def install():
279279
# Get real user to run non-sudo commands
280280
real_user = get_real_user()
281281

282-
try:
283-
cmsuser_gr = grp.getgrnam(CMSUSER)
284-
except KeyError:
285-
print("===== Creating group %s" % CMSUSER)
286-
subprocess.check_call(["groupadd", CMSUSER, "--system"])
287-
cmsuser_gr = grp.getgrnam(CMSUSER)
288-
289282
try:
290283
cmsuser_pw = pwd.getpwnam(CMSUSER)
291284
except KeyError:
292285
print("===== Creating user %s" % CMSUSER)
293286
subprocess.check_call(["useradd", CMSUSER, "--system",
294287
"--comment", "CMS default user",
295-
"--shell", "/bin/false", "--no-create-home",
296-
"--no-user-group", "--gid", CMSUSER])
288+
"--shell", "/bin/false", "-U"])
297289
cmsuser_pw = pwd.getpwnam(CMSUSER)
290+
cmsuser_gr = grp.getgrgid(cmsuser_pw.pw_gid)
298291

299292
root_pw = pwd.getpwnam("root")
300293

@@ -338,18 +331,19 @@ def install():
338331
os.umask(old_umask)
339332

340333
if real_user != "root":
341-
print("===== Adding yourself to the %s group" % CMSUSER)
334+
gr_name = cmsuser_gr.gr_name
335+
print("===== Adding yourself to the %s group" % gr_name)
342336
if ask("Type Y if you want me to automatically add "
343-
"\"%s\" to the %s group: " % (real_user, CMSUSER)):
344-
subprocess.check_call(["usermod", "-a", "-G", CMSUSER, real_user])
337+
"\"%s\" to the %s group: " % (real_user, gr_name)):
338+
subprocess.check_call(["usermod", "-a", "-G", gr_name, real_user])
345339
print("""
346340
###########################################################################
347341
### ###
348342
### Remember that you must now logout in order to make the change ###
349343
### effective ("the change" is: being in the %s group). ###
350344
### ###
351345
###########################################################################
352-
""" % CMSUSER)
346+
""" % gr_name)
353347
else:
354348
print("""
355349
###########################################################################
@@ -361,7 +355,7 @@ def install():
361355
### You must also logout to make the change effective. ###
362356
### ###
363357
###########################################################################
364-
""" % (CMSUSER, CMSUSER))
358+
""" % (gr_name, gr_name))
365359

366360

367361
def uninstall():
@@ -407,7 +401,8 @@ def uninstall():
407401
if ask("Do you want to delete user %s? [y/N] " % CMSUSER):
408402
subprocess.check_call(["userdel", CMSUSER])
409403
try:
410-
# Just to check whether it exists.
404+
# Just to check whether it exists. If CMSUSER had a different primary
405+
# group, we'll do nothing here.
411406
grp.getgrnam(CMSUSER)
412407
except KeyError:
413408
pass

0 commit comments

Comments
 (0)