summaryrefslogtreecommitdiff
path: root/www/zope/files/zope-install.py
blob: 0318dd7b998f27b32e09a0c281d4d133d5482b74 (plain)
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
171
172
173
174
175
176
177
#!/usr/pkg/bin/python
#
#	$NetBSD: zope-install.py,v 1.2 1999/01/09 20:49:27 kleink Exp $
#	$Endicor$
#
# Copyright (c) 1998 Endicor Technologies, Inc.
# All rights reserved. Written by Ty Sarna <tsarna@endicor.com>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

# This will have to do for now...

import sys, os, re, getopt, string

prefix = "/usr/pkg"
cgidir = prefix + "/libexec/cgi-bin"
apconf = prefix + "/etc/httpd/httpd.conf"
zopedir = prefix + "/zope"
zopevar = zopedir + "/var"
zopedata = "/var/zope"

def usage():
    print 'usage: zope-install [-n] [-p perms] [-u user] [-g group] [-d dir] [-c cgidir] instancename'
    print
    print '\t-n\tshow what would be done, but don\'t actually do it'
    print '\t-p\tmanager permissions, in the form'
    print '\t\tusername:plaintextpassword[:domain-restrinction]'
    print '\t\teg: "root:mypass" or "root:mypass:*.mydomain.com'
    print '\t-u\tusername to run as, default to Apache\'s user'
    print '\t-g\tgroupname to run as, default to Apache\'s group'
    print '\t-d\tdirectory for instance, defaults to %s/instancename' % zopedata
    print '\t-d\tdirectory for CGIs, defaults to %s' % cgidir
    
    sys.exit(1)

def exists(f):
    e = 1
    try:
        os.stat(f)
    except:
        e = 0
    return e

def resourcefile(v):
    resource = '''#!%(zopedir)s/pcgi/pcgi-wrapper
PCGI_NAME=Main
PCGI_MODULE_PATH=%(zopedir)s/lib/python/Main.py
PCGI_PUBLISHER=%(zopedir)s/pcgi/pcgi_publisher.py
PCGI_EXE=%(python)s
PCGI_SOCKET_FILE=%(instvar)s/pcgi.soc
PCGI_PID_FILE=%(instvar)s/pcgi.pid
PCGI_ERROR_LOG=%(instvar)s/pcgi.log
PCGI_DISPLAY_ERRORS=1
BOBO_REALM=%(instance)s
BOBO_DEBUG_MODE=1
INSTANCE_HOME=%(dirname)s
''' % v
    
    return resource
    
def GetApachePerms():
    user = group = "?ERROR?"
    
    rx = re.compile("^(User|Group)\\s([^\\s]+)")
    f = open(apconf, "r")
    for l in f.readlines():
        m = rx.match(l)
        if m:
            if m.group(1) == "User":
                user = m.group(2)
            if m.group(1) == "Group":
                group = m.group(2)
    return user, group

def runsys(pretend, cmd):
    if pretend:
        print cmd
    else:
        r = os.system(cmd)
        if r:
            sys.exit(r)
            
def createfile(pretend, fname, contents):
    if pretend:
        print "cat >%(fname)s <__EOF__\n%(contents)s__EOF__" % vars()
    else:
        f = open(fname, "w")
        f.write(contents)
        f.close()
            
if __name__ == "__main__":
    optlist, args = getopt.getopt(sys.argv[1:], 'np:u:g:d:')
    if len(args) != 1:
        usage()

    instance = args[0]
    user, group = GetApachePerms()
    pretend = 0
    seenperms = 0
    perms = "root:password"
    dirname = None
    python = sys.executable

    for (oname, oarg) in optlist:
        if oname == '-n':
            pretend = 1
        elif oname == '-p':
            perms = oarg
            seenperms = 1
        elif oname == '-u':
            user = oarg
        elif oname == '-g':
            group = oarg
        elif oname == '-d':
            dirname = oarg
        elif oname == '-c':
            cgidir = oarg
            
    if not dirname:
        dirname = zopedata + '/' + instance

    instvar = dirname + '/var'
    
    runsys(pretend, "mkdir -p " + instvar)

    if not exists(instvar + "/Data.bbb"):
        runsys(pretend, "cp %(zopevar)s/Data.bbb.in %(instvar)s" % vars())

    runsys(pretend, "chown -R %(user)s:%(group)s %(dirname)s" % vars())
    runsys(pretend, "chmod -R u+rwX,g+rX,g-w,o-rwx %(dirname)s" % vars())

    fname = dirname + "/access"
    if not exists(fname):
        if not seenperms:
            sys.stderr.write("%s: warning: perms not set, dedaulting to %s\n"
                % (sys.argv[0], perms))

        createfile(pretend, fname, perms + '\n')

    runsys(pretend, 
        "chmod 600 %(fname)s; chown %(user)s:%(group)s %(fname)s" % vars())

    fname = cgidir + '/' + instance + ".cgi"
    createfile(pretend, fname, resourcefile(vars()))
    runsys(pretend, 
        "chmod 755 %(fname)s; chown %(user)s:%(group)s %(fname)s" % vars())

    sys.stderr.write("""
now you will need to add lines similar to these to your
Apache srm.conf file to enable access to your instance
via http://yourwebserver/instance/

RewriteEngine on
RewriteCond %%{HTTP:Authorization}  ^(.*)
RewriteRule ^/%(instance)s/(.*) %(cgidir)s/%(instance)s.cgi/$1  [e=HTTP_CGI_AUTHORIZATION:%%1,t=application/x-httpd-cgi,l]
""" % vars())