/[shmookey]/portconf/update-login.py


UCC Code Repository

Contents of /portconf/update-login.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 120 - (show annotations) (download) (as text)
Thu Jun 5 07:30:56 2008 UTC (12 years, 5 months ago) by shmookey
File MIME type: text/x-python
File size: 3474 byte(s)
general improvements, beginning stump of a configuration interface

1 #!/usr/bin/python2.5
2 ''' update-login.py - pact login interface for ajax
3 '''
4
5 import sys, os, re, pexpect, time, Cookie
6 import Log
7
8 try:
9 from Config import libPaths, dbName, dbUser, dbPass, dbHost, dbPort
10 except:
11 Log.Message ("Error occured in finding basic configuration paramaters. AJAX request failed.", "update.py", "ERROR")
12 print "Configuration failure."
13 sys.exit ()
14
15 for path in libPaths: sys.path.append (path)
16
17 try:
18 import pyaaa
19 import pycisco
20 except:
21 Log.Message ("Couldn't import pycisco or pyaaa. AJAX request failed.", "update.py", "ERROR")
22 print "Import failure."
23 sys.exit ()
24
25 import cgi, cgitb
26 cgitb.enable ()
27
28 # Cookie stuff
29 cookieExpiry = time.gmtime(time.time())
30 if cookieExpiry[3] == 23: cookieExpiry = cookieExpiry[:2] + (0,) + cookieExpiry[4:]
31 else: cookieExpiry = cookieExpiry[:2] + (cookieExpiry[3]+1,) + cookieExpiry[4:]
32 cookie = Cookie.SimpleCookie (os.environ.get("HTTP_COOKIE", ""))
33
34 def RunPage (): pass
35
36 def LoginSetCookie (user, pw):
37 obj = pyaaa.SafeBase ()
38 try:
39 sessionID = obj._Login (user, pw)
40 except pyaaa.InvalidLogin:
41 print "Content-type: text/html"
42 print
43 print "result = 'failure';"
44 sys.exit ()
45 except pyaaa.DatabaseError:
46 print "Content-type: text/html"
47 print
48 print "result = 'dberror';"
49 sys.exit ()
50 cookie["sid"] = sessionID
51 print "Content-type: text/html"
52 print cookie
53 print
54 print "result = 'success';"
55 sys.exit ()
56
57
58 ''' Connector functions! If you want your web front-end to be able to call Python functions, add them here.
59 COMMAND NAME: The "action" value in the GET request that you want to map a Python function to.
60 PYTHON FUNCTION: The function you want it to call.
61 ARGUMENT LIST: An ordered list of arguments to look for in the GET request string, and pass to the Python function.
62 RETURN VALUE TRANSLATION: A dictionary of mappings to transform the return value of the Python function into something
63 readable. If a "__default" key exists, use that if a more appropriate key isn't found. Otherwise,
64 just return the raw value. You can also use this field to do things after the function returns.
65 '''
66 actions = {
67 # COMMAND NAME PYTHON FUNCTION ARGUMENT LIST RETURN VALUE TRANSLATOR
68 "login" : (LoginSetCookie, ("username", "password"), lambda x: X(x,{}))
69 }
70
71
72 # Exception handling down the bottom. You probably don't need to change these next two functions.
73 #
74
75 def LoginHandler (result):
76 if result == False:
77 print "Failed."
78
79
80 def X (key, translations):
81 if translations.has_key (key):
82 return translations [key]
83 if translations.has_key ("__default"):
84 return translations ["__default"]
85 return key
86
87 def RunPage ():
88 arglist = []
89 form = cgi.FieldStorage ()
90
91 if not form.has_key ("action"):
92 print "Content-type: text/html"
93 print
94 print "Error 1000"
95 return
96 action = form["action"].value
97 if not actions.has_key (action):
98 print "Content-type: text/html"
99 print
100 print "Error 1001"
101 return
102 for arg in actions[action][1]:
103 if not form.has_key (arg):
104 print "Content-type: text/html"
105 print
106 print "Error 1002"
107 return
108 arglist.append (form[arg].value)
109 result = actions[action][0] (*arglist)
110 print "Content-type: text/html"
111 print
112 print actions[action][2] (result)
113
114 try:
115 RunPage ()
116 except pycisco.NoSuchSwitchException:
117 print "Invalid"
118 except pycisco.TrunkException:
119 print "Disallowed"
120 except pycisco.VLANDisallowedException:
121 print "Disallowed"
122 #except EOF:
123 # print "Too many sessions!"

Properties

Name Value
svn:executable *

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26