summaryrefslogtreecommitdiffstats
path: root/py-bin
diff options
context:
space:
mode:
authormh@immerda.ch <mh@immerda.ch>2012-09-21 16:03:00 +0200
committeralice <alice@immerda.ch>2012-10-19 15:20:07 +0200
commit53f287d98ebf04d77c68df302addd1ea593cb5f5 (patch)
tree409fc2bf2dbcc9ba3ddcfae8bb370e2469930cf8 /py-bin
parent80d33154edd702140ee7ce43ab39644c7a30aedd (diff)
implement a cli to support maintenance work on the new jabber interface
Diffstat (limited to 'py-bin')
-rw-r--r--py-bin/cli.py108
-rw-r--r--py-bin/jabberman.py61
-rw-r--r--py-bin/utils.py6
3 files changed, 158 insertions, 17 deletions
diff --git a/py-bin/cli.py b/py-bin/cli.py
new file mode 100644
index 0000000..75c9bec
--- /dev/null
+++ b/py-bin/cli.py
@@ -0,0 +1,108 @@
+#!/usr/bin/python
+
+# this is a simple cli script to provide actions for common tasks.
+
+import sys, logging, os
+import config
+import utils
+from struct import *
+from jabberman import JabberDB
+
+class JabberCli:
+ def __init__(self, action, jid):
+ if action == 'reset_pwd':
+ if jid == None:
+ self.usage()
+ elif action == 'delete_jid':
+ if jid == None:
+ self.usage()
+ elif action == 'get_web_user':
+ if jid == None:
+ self.usage()
+ elif action == 'delete_web_user':
+ if jid == None:
+ self.usage()
+ elif (action != 'list_web_users') and (action != 'list_jids'):
+ self.usage()
+
+ self.jadb = JabberDB()
+ getattr(self, action)(jid)
+
+ def usage(self):
+ print 'Unknown action!'
+ print
+ print sys.argv[0] + ' action [jid]'
+ print
+ print 'Possible Actions:'
+ print ' * reset_pwd <jid>'
+ print ' * list_web_users'
+ print ' * get_web_user <jid>'
+ print ' * list_jids [<email>]'
+ print ' * delete_web_user <email>'
+ print ' * delete_jid <jid>'
+ print
+ sys.exit(1)
+
+ def list_web_users(self, email=None):
+ sorted_web_users = sorted(self.jadb.select_web_users(), key=lambda wu: wu.email)
+ for web_user in sorted_web_users:
+ print web_user.email
+
+ def list_jids(self, email=None):
+ if email == None:
+ jids = self.jadb.select_all_jids()
+ else:
+ web_user = self.__get_web_user(email)
+ jids = self.jadb.select_jids_by_web_user_id(web_user.id)
+
+ for jid in sorted(jids, key=lambda jid: jid.jid):
+ print jid.jid
+
+ def reset_pwd(self, jid):
+ db_jid = self.__get_jid(jid)
+ newpwd = utils.generate_token(16)
+ self.jadb.update_jid(jid,newpwd)
+ print "New password: %s" % newpwd
+
+ def get_web_user(self,jid):
+ db_jid = self.__get_jid(jid)
+ web_user = self.jadb.get_web_user(db_jid.web_user_id,'id')
+ print web_user.email
+
+ def delete_jid(self, jid):
+ db_jid = self.__get_jid(jid)
+ if not self.jadb.is_primary_jid_by_db(db_jid):
+ self.jadb.delete_jid(jid)
+ print "Jid %s successfully deleted" % jid
+ else:
+ print "Jid %s is a primary jid, you better delete the WebUser directly" % jid
+ sys.exit(1)
+
+ def delete_web_user(self, email):
+ web_user = self.__get_web_user(email)
+ self.jadb.delete_all_jids(web_user.id)
+ self.jadb.delete_web_user(email)
+ print "WebUser %s successfully deleted" % email
+
+ def __get_web_user(self,email):
+ web_user = self.jadb.get_web_user(email)
+ if not web_user:
+ print "Unknown WebUser: %s" % email
+ sys.exit(1)
+ return web_user
+
+ def __get_jid(self,jid):
+ db_jid = self.jadb.get_jid(jid)
+ if not db_jid:
+ print "Unknown jid: %s" % jid
+ sys.exit(1)
+ return db_jid
+
+action = jid = None
+if len(sys.argv) > 1:
+ action = sys.argv[1]
+
+if len(sys.argv) > 2:
+ jid = sys.argv[2]
+
+JabberCli(action,jid) \ No newline at end of file
diff --git a/py-bin/jabberman.py b/py-bin/jabberman.py
index 42b305b..8b739c0 100644
--- a/py-bin/jabberman.py
+++ b/py-bin/jabberman.py
@@ -2,6 +2,7 @@
import sqlite3, atexit, bcrypt, sha, hmac, random, os, struct, time, re, logging, datetime, string
import config
+import utils
# cleanup: renaming for consistency, rm dead code, setters/getters or just properties?
# FIXME: handle transactions properly (or are they ok?)
@@ -116,22 +117,17 @@ class JabberDB:
return (True, "Passwort geaendert.")
def generate_web_user(self, email):
- token = ''.join(map(lambda x: "%02x" % x, struct.unpack("32B",os.urandom(32))))
token_date = self.__now()
cur = self.__connection.cursor()
- cur.execute("INSERT INTO web_users VALUES (NULL,?,?,?)", (email, token, token_date))
+ cur.execute("INSERT INTO web_users VALUES (NULL,?,?,?)", (email, utils.generate_token(), token_date))
self.__connection.commit()
return token
- def get_web_user(self, email):
+ def get_web_user(self, val, by='email'):
cur = self.__connection.cursor()
- cur.execute("SELECT * FROM web_users where email=?", (email,))
- row = cur.fetchone()
- if row == None:
- return None
-
- return WebUser(row[0],row[1],row[2],row[3])
+ cur.execute(("SELECT * FROM web_users where %s=?" % by), (val,))
+ return self.__gen_web_user(cur.fetchone())
def update_web_user(self, email, plain_pwd, provided_token):
cur = self.__connection.cursor()
@@ -180,10 +176,29 @@ class JabberDB:
return True
def is_jid(self, jid):
+ db_jid = self.get_jid(jid)
+ return db_jid != None and db_jid.jid == jid
+
+ def is_primary_jid(self, jid):
+ db_jid = self.get_jid(jid)
+ if db_jid:
+ return self.is_primary_jid_by_db(db_jid)
+ else:
+ return False
+
+ def is_primary_jid_by_db(self,db_jid):
+ web_user = self.get_web_user(db_jid.web_user_id,'id')
+ return web_user != None and web_user.get_primary_jid() == db_jid.jid
+
+ def get_jid(self, jid):
cur = self.__connection.cursor()
cur.execute("SELECT * FROM jabber_users WHERE jid=?", (jid,))
- row = cur.fetchone()
- return row != None and row[1] == jid
+ return self.__gen_jabber_user(cur.fetchone())
+
+ def delete_all_jids(self, web_user_id):
+ cur = self.__connection.cursor()
+ cur.execute("DELETE FROM jabber_users WHERE web_user_id=?", (web_user_id,))
+ self.__connection.commit()
def delete_jid(self, jid):
cur = self.__connection.cursor()
@@ -199,19 +214,24 @@ class JabberDB:
cur = self.__connection.cursor()
cur.execute("SELECT * FROM web_users where email=?", (email,))
row = cur.fetchone()
- web_user_id = row[0]
+ self.select_jids_by_web_user_id(row[0])
+ def select_jids_by_web_user_id(self, web_user_id):
+ cur = self.__connection.cursor()
cur.execute("SELECT * FROM jabber_users where web_user_id=?", (web_user_id,))
- rows = cur.fetchall()
+ return self.__gen_jabber_users(cur.fetchall())
- return map(lambda row: JabberUser(row[0],row[1],row[2],row[3]), rows)
+ def select_all_jids(self):
+ cur = self.__connection.cursor()
+ cur.execute("SELECT * FROM jabber_users")
+ return self.__gen_jabber_users(cur.fetchall())
def select_web_users(self):
cur = self.__connection.cursor()
cur.execute("SELECT * FROM web_users")
rows = cur.fetchall()
- return map(lambda row: WebUser(row[0],row[1],row[2],row[3]), rows)
+ return map(lambda row: self.__gen_web_user(row), rows)
def __now(self):
return datetime.datetime.now()
@@ -222,6 +242,15 @@ class JabberDB:
def __timediff(self, a, b):
return self.__to_timestamp(a) - self.__to_timestamp(b)
+ def __gen_jabber_users(self, rows):
+ return map(lambda row: self.__gen_jabber_user(row), rows)
+
+ def __gen_jabber_user(self, row):
+ return (JabberUser(row[0],row[1],row[2],row[3]) if row else None)
+
+ def __gen_web_user(self, row):
+ return (WebUser(row[0],row[1],row[2],row[3]) if row else None)
+
class JabberManager:
def __init__(self, session):
self.jadb = JabberDB()
@@ -384,7 +413,7 @@ class JabberManager:
del(self.session["pass"])
if self.session.get("token"):
del(self.session["tok"])
-
+
@staticmethod
def __check_account_form(acc_type, domains, account):
try:
diff --git a/py-bin/utils.py b/py-bin/utils.py
index b13949e..15a15a3 100644
--- a/py-bin/utils.py
+++ b/py-bin/utils.py
@@ -1,10 +1,14 @@
#helper functions/classes
-import string, logging, re, os, urllib
+import string, logging, re, os, urllib, struct
import lib.jon.cgi as cgi, lib.jon.session as session
import lib.em as em
import config
+def generate_token(length=32):
+ return ''.join(map(lambda x: "%02x" % x, struct.unpack(("%sB" % length),os.urandom(length))))
+
+
def xmap(function, data):
if isinstance(data, str):
return function(data)