Skip to content

Commit ecf3270

Browse files
committed
Remove user/group dropping from plain server
1 parent b4fb4cf commit ecf3270

File tree

6 files changed

+0
-160
lines changed

6 files changed

+0
-160
lines changed

plain/plain/server/config.py

Lines changed: 0 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99

1010
import argparse
1111
import copy
12-
import grp
1312
import inspect
1413
import ipaddress
1514
import os
16-
import pwd
1715
import re
1816
import ssl
1917
import sys
@@ -129,14 +127,6 @@ def address(self):
129127
s = self.settings["bind"].get()
130128
return [util.parse_address(util.bytes_to_str(bind)) for bind in s]
131129

132-
@property
133-
def uid(self):
134-
return self.settings["user"].get()
135-
136-
@property
137-
def gid(self):
138-
return self.settings["group"].get()
139-
140130
@property
141131
def proc_name(self):
142132
pn = self.settings["proc_name"].get()
@@ -426,35 +416,6 @@ def _validate_callable(val):
426416
return _validate_callable
427417

428418

429-
def validate_user(val):
430-
if val is None:
431-
return os.geteuid()
432-
if isinstance(val, int):
433-
return val
434-
elif val.isdigit():
435-
return int(val)
436-
else:
437-
try:
438-
return pwd.getpwnam(val).pw_uid
439-
except KeyError:
440-
raise ConfigError(f"No such user: '{val}'")
441-
442-
443-
def validate_group(val):
444-
if val is None:
445-
return os.getegid()
446-
447-
if isinstance(val, int):
448-
return val
449-
elif val.isdigit():
450-
return int(val)
451-
else:
452-
try:
453-
return grp.getgrnam(val).gr_gid
454-
except KeyError:
455-
raise ConfigError(f"No such group: '{val}'")
456-
457-
458419
def validate_post_request(val):
459420
val = validate_callable(-1)(val)
460421

@@ -1025,77 +986,6 @@ class WorkerTmpDir(Setting):
1025986
"""
1026987

1027988

1028-
class User(Setting):
1029-
name = "user"
1030-
section = "Server Mechanics"
1031-
cli = ["-u", "--user"]
1032-
meta = "USER"
1033-
validator = validate_user
1034-
default = os.geteuid()
1035-
default_doc = "``os.geteuid()``"
1036-
desc = """\
1037-
Switch worker processes to run as this user.
1038-
1039-
A valid user id (as an integer) or the name of a user that can be
1040-
retrieved with a call to ``pwd.getpwnam(value)`` or ``None`` to not
1041-
change the worker process user.
1042-
"""
1043-
1044-
1045-
class Group(Setting):
1046-
name = "group"
1047-
section = "Server Mechanics"
1048-
cli = ["-g", "--group"]
1049-
meta = "GROUP"
1050-
validator = validate_group
1051-
default = os.getegid()
1052-
default_doc = "``os.getegid()``"
1053-
desc = """\
1054-
Switch worker process to run as this group.
1055-
1056-
A valid group id (as an integer) or the name of a user that can be
1057-
retrieved with a call to ``grp.getgrnam(value)`` or ``None`` to not
1058-
change the worker processes group.
1059-
"""
1060-
1061-
1062-
class Umask(Setting):
1063-
name = "umask"
1064-
section = "Server Mechanics"
1065-
cli = ["-m", "--umask"]
1066-
meta = "INT"
1067-
validator = validate_pos_int
1068-
type = auto_int
1069-
default = 0
1070-
desc = """\
1071-
A bit mask for the file mode on files written by Gunicorn.
1072-
1073-
Note that this affects unix socket permissions.
1074-
1075-
A valid value for the ``os.umask(mode)`` call or a string compatible
1076-
with ``int(value, 0)`` (``0`` means Python guesses the base, so values
1077-
like ``0``, ``0xFF``, ``0022`` are valid for decimal, hex, and octal
1078-
representations)
1079-
"""
1080-
1081-
1082-
class Initgroups(Setting):
1083-
name = "initgroups"
1084-
section = "Server Mechanics"
1085-
cli = ["--initgroups"]
1086-
validator = validate_bool
1087-
action = "store_true"
1088-
default = False
1089-
1090-
desc = """\
1091-
If true, set the worker process's group access list with all of the
1092-
groups of which the specified username is a member, plus the specified
1093-
group id.
1094-
1095-
.. versionadded:: 19.7
1096-
"""
1097-
1098-
1099989
class TmpUploadDir(Setting):
1100990
name = "tmp_upload_dir"
1101991
section = "Server Mechanics"

plain/plain/server/glogging.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -420,13 +420,6 @@ def _set_handler(self, log, output, fmt, stream=None):
420420
else:
421421
util.check_is_writable(output)
422422
h = logging.FileHandler(output)
423-
# make sure the user can reopen the file
424-
try:
425-
os.chown(h.baseFilename, self.cfg.user, self.cfg.group)
426-
except OSError:
427-
# it's probably OK there, we assume the user has given
428-
# /dev/null as a parameter.
429-
pass
430423

431424
h.setFormatter(fmt)
432425
h._gunicorn = True

plain/plain/server/sock.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,7 @@ def __str__(self):
118118
return f"unix:{self.cfg_addr}"
119119

120120
def bind(self, sock):
121-
old_umask = os.umask(self.conf.umask)
122121
sock.bind(self.cfg_addr)
123-
util.chown(self.cfg_addr, self.conf.uid, self.conf.gid)
124-
os.umask(old_umask)
125122

126123

127124
def _sock_type(addr):

plain/plain/server/util.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import io
1616
import logging
1717
import os
18-
import pwd
1918
import random
2019
import re
2120
import socket
@@ -92,34 +91,6 @@ def get_arity(f):
9291
return arity
9392

9493

95-
def get_username(uid):
96-
"""get the username for a user id"""
97-
return pwd.getpwuid(uid).pw_name
98-
99-
100-
def set_owner_process(uid, gid, initgroups=False):
101-
"""set user and group of workers processes"""
102-
103-
if gid:
104-
if uid:
105-
try:
106-
username = get_username(uid)
107-
except KeyError:
108-
initgroups = False
109-
110-
if initgroups:
111-
os.initgroups(username, gid)
112-
elif gid != os.getgid():
113-
os.setgid(gid)
114-
115-
if uid and uid != os.getuid():
116-
os.setuid(uid)
117-
118-
119-
def chown(path, uid, gid):
120-
os.chown(path, uid, gid)
121-
122-
12394
if sys.platform.startswith("win"):
12495

12596
def _waitfor(func, pathname, waitall=False):

plain/plain/server/workers/base.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,6 @@ def init_process(self):
104104
for k, v in self.cfg.env.items():
105105
os.environ[k] = v
106106

107-
util.set_owner_process(
108-
self.cfg.uid, self.cfg.gid, initgroups=self.cfg.initgroups
109-
)
110-
111107
# Reseed the random number generator
112108
util.seed()
113109

plain/plain/server/workers/workertmp.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,10 @@
1818

1919
class WorkerTmp:
2020
def __init__(self, cfg):
21-
old_umask = os.umask(cfg.umask)
2221
fdir = cfg.worker_tmp_dir
2322
if fdir and not os.path.isdir(fdir):
2423
raise RuntimeError(f"{fdir} doesn't exist. Can't create workertmp.")
2524
fd, name = tempfile.mkstemp(prefix="wgunicorn-", dir=fdir)
26-
os.umask(old_umask)
27-
28-
# change the owner and group of the file if the worker will run as
29-
# a different user or group, so that the worker can modify the file
30-
if cfg.uid != os.geteuid() or cfg.gid != os.getegid():
31-
util.chown(name, cfg.uid, cfg.gid)
3225

3326
# unlink the file so we don't leak temporary files
3427
try:

0 commit comments

Comments
 (0)