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
|
#!/usr/bin/env python
#
# Web Interface for Cobbler - CGI Controller
#
# Copyright 2007 Albert P. Tobey <tobert@gmail.com>
#
# This software may be freely redistributed under the terms of the GNU
# general public license.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
import cgi
import cgitb
import Cookie
import os
import sys
from cobbler.webui.CobblerWeb import CobblerWeb
def map_modes():
path = os.environ.get( 'PATH_INFO', 'index' )
if path.startswith('/'):
path = path[1:]
if path.endswith('/'):
path = path[:-1]
if path is '':
path = 'index'
return path
def base_url():
return os.environ.get('SCRIPT_NAME', '')
def configure():
# FIXME: read a config file ...
config = {
'token': None,
'server': None,
'base_url': None,
'token_cookie_name': None,
'username': None,
'password': None,
'cgitb_enabled': 0
}
#config.username = 'testuser',
#config.password = 'llamas2007'
# defaults
if config['server'] is None:
config['server'] = "http://localhost/cobbler_api_rw"
if config['base_url'] is None:
config['base_url'] = base_url()
if config['token_cookie_name'] is None:
config['token_cookie_name'] = 'cobbler_xmlrpc_token'
return config
def main():
content = "Something went wrong and I couldn't generate any content for you!"
cw_conf = configure()
path = map_modes()
form = cgi.parse()
cookies = Cookie.SimpleCookie(os.environ.get("HTTP_COOKIE",""))
# make cgitb enablement configurable
if cw_conf['cgitb_enabled'] == 1:
cgitb.enable()
cw_conf.pop('cgitb_enabled')
# look for the token cookie and put it in the config dict if found
if cookies.has_key( cw_conf['token_cookie_name'] ):
cw_conf['token'] = cookies[ cw_conf['token_cookie_name'] ].value
# exchnage single-element arrays in the 'form' dictionary for just that item
# so there isn't a ton of 'foo[0]' craziness where 'foo' should suffice
# - may be bad for form elements that are sometimes lists and sometimes
# single items
for key,val in form.items():
if isinstance(val, list):
if len(val) == 1:
form[key] = val[0]
# instantiate a CobblerWeb object
cw = CobblerWeb( **cw_conf )
if not path.startswith('login') and (cw_conf['token'] is None and (cw_conf['username'] is None or cw_conf['password'] is None)):
func = getattr( cw, 'login' )
content = func( message="Authentication Required." )
# check for a valid path/mode
elif path in cw.modes():
func = getattr( cw, path )
try:
content = func( **form )
# handle failed authentication gracefully
except Exception, e:
if str(e).find('login failed:') > 0:
func = getattr( cw, 'login' )
content = func( message="Authentication failed." )
# everything else is a bug?
else:
raise e
# handle invalid paths gracefully
else:
func = getattr( cw, 'error_page' )
content = func( "Invalid Mode: \"%s\"" % path )
# finally, get any cookies generated by the CobblerWeb object
cookie_header = cw.cookies().output()
if cookie_header:
print cookie_header
# deliver content
print "Content-type: text/html"
print
print content
main()
|