Skip to content

Commit 23d702c

Browse files
committed
Append client IP to subscribe log entries when available.
Add shared helpers to read the remote address from CGI environment variables and append it to membership add/delete/change syslog lines when present, without duplicating addresses already in the whence field.
1 parent 4bf646f commit 23d702c

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

Mailman/MailList.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,8 +1185,9 @@ def ApprovedAddMember(self, userdesc, ack=None, admin_notif=None, text='',
11851185
if isinstance(name, bytes):
11861186
name = name.decode(Utils.GetCharSet(lang))
11871187

1188+
log_whence = Utils.append_remote_for_log(whence or '')
11881189
syslog('subscribe', '%s: new%s %s, %s', self.internal_name(),
1189-
kind, formataddr((name, email)), whence)
1190+
kind, formataddr((name, email)), log_whence)
11901191
if ack:
11911192
lang = self.preferred_language
11921193
otrans = i18n.get_translation()
@@ -1255,6 +1256,7 @@ def ApprovedDeleteMember(self, name, whence=None,
12551256
whence = "; %s" % whence
12561257
else:
12571258
whence = ""
1259+
whence = Utils.append_remote_for_log(whence)
12581260
syslog('subscribe', '%s: deleted %s%s',
12591261
self.internal_name(), name, whence)
12601262

@@ -1396,8 +1398,11 @@ def ApprovedChangeMemberAddress(self, oldaddr, newaddr, globally):
13961398

13971399
def log_and_notify_admin(self, oldaddr, newaddr):
13981400
"""Log member address change and notify admin if requested."""
1399-
syslog('subscribe', '%s: changed member address from %s to %s',
1400-
self.internal_name(), oldaddr, newaddr)
1401+
log_suffix = Utils.append_remote_for_log('')
1402+
if log_suffix:
1403+
log_suffix = ' %s' % log_suffix
1404+
syslog('subscribe', '%s: changed member address from %s to %s%s',
1405+
self.internal_name(), oldaddr, newaddr, log_suffix)
14011406
if self.admin_notify_mchanges:
14021407
lang = self.preferred_language
14031408
otrans = i18n.get_translation()

Mailman/Utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,26 @@ def ValidateEmail(s):
497497
# line in the mailman error log. (TK: advisory by Moritz Naumann)
498498
CRNLpat = re.compile(r'[^\x21-\x7e]')
499499

500+
def get_remote_addr():
501+
"""Return the client IP from CGI environment, or None if unavailable."""
502+
return (os.environ.get('HTTP_FORWARDED_FOR') or
503+
os.environ.get('HTTP_X_FORWARDED_FOR') or
504+
os.environ.get('REMOTE_ADDR'))
505+
506+
507+
def append_remote_for_log(whence=''):
508+
"""Append client IP to a subscribe-log whence string when available."""
509+
remote = get_remote_addr()
510+
if not remote:
511+
return whence or ''
512+
whence = whence or ''
513+
if remote in whence:
514+
return whence
515+
if whence:
516+
return '%s %s' % (whence, remote)
517+
return remote
518+
519+
500520
def GetPathPieces(envar='PATH_INFO'):
501521
path = os.environ.get(envar)
502522
if path:

0 commit comments

Comments
 (0)