summaryrefslogtreecommitdiff
path: root/bin/check_perms
blob: d965bc56e331300fd6ad64fad9918c082aa99dbe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#! /usr/bin/env python

"""Check the permissions for the Mailman installation.

Usage: %(PROGRAM)s [-f] [-v] [-h]

With no arguments, just check and report all the files that have bogus
permissions or group ownership.  With -f (and run as root), fix all the
permission problems found.  With -v be verbose.

"""

import sys
import os
import errno
import getopt
import grp
from stat import *
import paths
from Mailman import mm_cfg

MAILMAN_GRPNAME = 'mailman'
MAILMAN_GID = grp.getgrnam(MAILMAN_GRPNAME)[2]

PROGRAM = sys.argv[0]



class State:
    FIX = 0
    VERBOSE = 0
    ERRORS = 0

STATE = State()



def statmode(path):
    return os.stat(path)[ST_MODE]

def statgidmode(path):
    stat = os.stat(path)
    return stat[ST_MODE], stat[ST_GID]

def checkwalk(arg, dirname, names):
    for name in names:
        path = os.path.join(dirname, name)
        if arg.VERBOSE:
            print 'checking gid and modes for', path
        try:
            mode, gid = statgidmode(path)
        except OSError, (code, msg):
            if code == errno.ENOENT:
                continue
            raise
        if gid <> MAILMAN_GID:
            arg.ERRORS = arg.ERRORS + 1
            print path, 'bad gid (has: %s, expected %s)' % (
                grp.getgrgid(gid)[0], MAILMAN_GRPNAME),
            if STATE.FIX:
                print '(fixing)'
                os.chown(path, -1, MAILMAN_GID)
            else:
                print
        # all directories must be setgid
        if S_ISDIR(mode) and not mode & S_ISGID:
            arg.ERRORS = arg.ERRORS + 1
            print path, 'directory is not setgid',
            if STATE.FIX:
                print '(fixing)'
                os.chmod(path, mode | S_ISGID)
            else:
                print

def checkall():
    os.path.walk(mm_cfg.PREFIX, checkwalk, STATE)


def checkarchives():
    private = mm_cfg.PRIVATE_ARCHIVE_FILE_DIR
    if STATE.VERBOSE:
        print 'checking perms on', private
    # private archives must not be other readable
    mode = statmode(private)
    if mode & S_IROTH:
        STATE.ERRORS = STATE.ERRORS + 1
        print private, 'must not be other-readable',
        if STATE.FIX:
            print '(fixing)'
            os.chmod(private, mode & ~S_IROTH)
        else:
            print


def checkcgi():
    exes = os.listdir(mm_cfg.CGI_DIR)
    for f in exes:
        path = os.path.join(mm_cfg.CGI_DIR, f)
        if STATE.VERBOSE:
            print 'checking set-gid for', path
        mode = statmode(path)
        if mode & S_IXGRP and not mode & S_ISGID:
            STATE.ERRORS = STATE.ERRORS + 1
            print path, 'must be set-gid',
            if STATE.FIX:
                print '(fixing)'
                os.chmod(path, mode | S_ISGID)
            else:
                print

def checkmail():
    wrapper = os.path.join(mm_cfg.WRAPPER_DIR, 'wrapper')
    if STATE.VERBOSE:
        print 'checking set-gid for', wrapper
    mode = statmode(wrapper)
    if not mode & S_ISGID:
        STATE.ERRORS = STATE.ERRORS + 1
        print wrapper, 'must be set-gid',
        if STATE.FIX:
            print '(fixing)'
            os.chmod(wrapper, mode | S_ISGID)

def checkadminpw():
    adminpw = os.path.join(mm_cfg.DATA_DIR, 'adm.pw')
    targetmode = S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP
    if STATE.VERBOSE:
        print 'checking perms on', adminpw
    mode = statmode(adminpw)
    if mode <> targetmode:
        STATE.ERRORS = STATE.ERRORS + 1
        print adminpw, 'permissions must be exactly 0640 (got %s)' % oct(mode)
        if STATE.FIX:
            print '(fixing)'
            os.chmod(adminpw, targetmode)


def usage(code=0, msg=''):
    print __doc__ % globals()
    if msg:
        print msg
    sys.exit(code)


if __name__ == '__main__':
    try:
        opts, args = getopt.getopt(sys.argv[1:],
                                   'fvh',
                                   ['fix', 'verbose', 'help'])
    except getopt.error, msg:
        usage(1, msg)

    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage()
        elif opt in ('-f', '--fix'):
            STATE.FIX = 1
        elif opt in ('-v', '--verbose'):
            STATE.VERBOSE = 1

    checkall()
    checkarchives()
    checkcgi()
    checkmail()
    checkadminpw()

    if not STATE.ERRORS:
        print 'No problems found'
    else:
        print 'Problems found:', STATE.ERRORS
        print 'Re-run as root with -f flag until no errors are found'