def main(): # Configure logging. logging.config.fileConfig(ini_conf_path) # Get a logger. global log log = logging.getLogger('tbxsos-xmlrpc') # Get master configuration. global master_config master_config = get_master_config() # Get KCD external configuration. global kcd_external_conf kcd_external_conf = get_kcd_external_conf_object(master_config=master_config) # Tell kctl lib to commit changes. kparams_set("commit", True) # Get an CGI XMLRPC server. server = CGIXMLRPCRequestHandler() #allow_none=1) # Get an XMLRPC server. #server = SimpleXMLRPCServer(("localhost", 8000)) # Make the API public. #server.register_introspection_functions() # Register all functions from the KPSApi class. server.register_instance(KPSApi()) # Handle CGI request. server.handle_request()
def handle_reinvite(self, opts, args): # Arguments email = args[0] # Options dry_run_flag = 0 for opt, value in opts: if opt == "-d" or opt == "--dry-run": dry_run_flag = 1 # Connect to the KCD database. self.conn = self.get_db_conn() # Connect to KCD kdebug.debug(2, "Instantiating a KCD client.") c = BaseKcdClient(get_kcd_external_conf_object()) c.connect() c.select_role(kanp.KANP_KCD_ROLE_WORKSPACE) kdebug.debug(1, "Instantiated a KCD client.") # Get the list of workspaces. kws_list = [] query = ( "SELECT kws_id FROM kcd_kws_users WHERE lower(email)=lower(%s) AND (bool(flags & %i = 0)) " "ORDER BY kws_id") % (escape_pg_string(email), kanp.KANP_USER_FLAG_BAN) cur = exec_pg_query(self.conn, query) for row in cur.fetchall(): kws_list.append(row[0]) cur.close() # Invite the user. for kws_id in kws_list: self.stdout.write("Reinviting user %s to workspace %i.\n" % (email, kws_id)) if dry_run_flag: continue # Log in the workspace. c.connect_workspace(workspace_id=kws_id, email_id="kwmo", password=c.conf.kcd_passwd) # Send the invitation. msg = "This email was sent automatically to give you access to your Teambox.\n" wi = WorkspaceInvitee(email_address=email, send_mail=1) c.send_invitation(kws_id, msg, [wi]) if wi.error != "": raise Exception(wi.error)
def handle_rm(self, opts, args): # KDebug parameters self.set_debug_levels(opts) # Arguments self.kws_id = int(args[0]) self.inode_id = long(args[1]) self.commit_id = long(args[2]) # Options self.type = 'f' self.user_id = 1 self.share_id = 0 self.email_id = 0 for opt, value in opts: if opt == "-t" or opt == "--type": self.type = value if opt == "-u" or opt == "--user_id": self.user_id = value if opt == "-s" or opt == "--share_id": self.share_id = value if opt == "-e" or opt == "--email_id": self.email_id = value # Connect to KCD kdebug.debug(2, "Instantiating a KCD client.") c = BaseKcdClient(get_kcd_external_conf_object()) kdebug.debug(1, "Instantiated a KCD client.") # Build list of entries (only one entry supported for now). entries_list = [] if self.type == 'd': entry = kfs_lib.KFSOpDirDelete(self.inode_id, self.commit_id) elif self.type == 'f': entry = kfs_lib.KFSOpFileDelete(self.inode_id, self.commit_id) else: raise Exception("Bad entry type.") entries_list = [entry] # Delete entries. c.kfs_delete_entries(self.kws_id, self.share_id, self.user_id, self.email_id, entries_list)
def handle_reinvite(self, opts, args): # Arguments email = args[0] # Options dry_run_flag = 0 for opt, value in opts: if opt == "-d" or opt == "--dry-run": dry_run_flag = 1 # Connect to the KCD database. self.conn = self.get_db_conn() # Connect to KCD kdebug.debug(2, "Instantiating a KCD client.") c = BaseKcdClient(get_kcd_external_conf_object()) c.connect() c.select_role(kanp.KANP_KCD_ROLE_WORKSPACE) kdebug.debug(1, "Instantiated a KCD client.") # Get the list of workspaces. kws_list = [] query = ("SELECT kws_id FROM kcd_kws_users WHERE lower(email)=lower(%s) AND (bool(flags & %i = 0)) " "ORDER BY kws_id") % (escape_pg_string(email), kanp.KANP_USER_FLAG_BAN) cur = exec_pg_query(self.conn, query) for row in cur.fetchall(): kws_list.append(row[0]) cur.close() # Invite the user. for kws_id in kws_list: self.stdout.write("Reinviting user %s to workspace %i.\n" % (email, kws_id)) if dry_run_flag: continue # Log in the workspace. c.connect_workspace(workspace_id=kws_id, email_id="kwmo", password=c.conf.kcd_passwd) # Send the invitation. msg = "This email was sent automatically to give you access to your Teambox.\n" wi = WorkspaceInvitee(email_address=email, send_mail=1) c.send_invitation(kws_id, msg, [ wi ]) if wi.error != "": raise Exception(wi.error)
def start_fetcher(options): global kcd_db_conn # Connect to the kas postgres database on kcd host. logger.info("Connecting to the kcd database.") try: kcd_db_conn = get_kcd_db_conn(get_kcd_external_conf_object()) except: logger.error("Could not connect to the kcd database.") raise logger.debug("Connected to the kcd database.") # Enable keepalives to detect failed connections before 2 hours pass. enable_keepalive() # Listen to the list of workspaces. logger.info("Listening to the list of workspaces.") listen_kws_list() # Enter the main loop. logger.info("Entering the main loop.") do_main_loop()
def create(self): email = '' pwd = '' if ( ('email' in request.params) and ('pwd' in request.params)): email = request.params['email'] pwd = request.params['pwd'] if email and pwd: user_email = email user_pwd = pwd user_license ='' user_nonce = '' tmp_usr = User.get_by(email = user_email) if (tmp_usr): user_license = tmp_usr.license user_nonce = tmp_usr.nonce user_pwd = tmp_usr.pwd if (user_license == 'none'): response.status = 403 return 'user_registration_locked' elif ((user_license == '') or (user_license == 'confirm')): enable_register = c.mc.freemium_autoregister if not enable_register: response.status = 403 return 'registration_disabled' if (user_nonce == '' or (pwd != user_pwd)): user_nonce = self.generateNonce() user_pwd = pwd user_license = 'confirm' # Send confirmation email confirm_link = url_for('confirm', email= user_email , nonce = user_nonce, qualified=True) log.debug("confirm link: " + confirm_link) conf = get_kcd_external_conf_object(master_config=c.mc) if not conf: response.status = 405 return 'The free registration is not available. Please conact your system adminstrator.' try: kc = FreemiumKcdClient(conf) kc.send_freemium_confirmation_email(conf.kcd_passwd, user_email, confirm_link) except Exception, err: log.error(str(err)) response.status = 500 return 'Teambox Main Application Server error' # Update user in freemium and kps DB try: rpc_server, rpc_sid, str_org_id = self.connectToRPC() rpc_server.set_freemium_user(rpc_sid, str_org_id, user_email, user_pwd, user_license , user_nonce, '', False) except Exception, err: log.error(str(err)) response.status = 500 return 'Teambox Sign-on Server error' return 'confirm'
def create(self): email = '' pwd = '' if (('email' in request.params) and ('pwd' in request.params)): email = request.params['email'] pwd = request.params['pwd'] if email and pwd: user_email = email user_pwd = pwd user_license = '' user_nonce = '' tmp_usr = User.get_by(email=user_email) if (tmp_usr): user_license = tmp_usr.license user_nonce = tmp_usr.nonce user_pwd = tmp_usr.pwd if (user_license == 'none'): response.status = 403 return 'user_registration_locked' elif ((user_license == '') or (user_license == 'confirm')): enable_register = c.mc.freemium_autoregister if not enable_register: response.status = 403 return 'registration_disabled' if (user_nonce == '' or (pwd != user_pwd)): user_nonce = self.generateNonce() user_pwd = pwd user_license = 'confirm' # Send confirmation email confirm_link = url_for('confirm', email=user_email, nonce=user_nonce, qualified=True) log.debug("confirm link: " + confirm_link) conf = get_kcd_external_conf_object(master_config=c.mc) if not conf: response.status = 405 return 'The free registration is not available. Please conact your system adminstrator.' try: kc = FreemiumKcdClient(conf) kc.send_freemium_confirmation_email( conf.kcd_passwd, user_email, confirm_link) except Exception, err: log.error(str(err)) response.status = 500 return 'Teambox Main Application Server error' # Update user in freemium and kps DB try: rpc_server, rpc_sid, str_org_id = self.connectToRPC() rpc_server.set_freemium_user(rpc_sid, str_org_id, user_email, user_pwd, user_license, user_nonce, '', False) except Exception, err: log.error(str(err)) response.status = 500 return 'Teambox Sign-on Server error' return 'confirm'
def get_cached_kcd_external_conf_object(): if not _config_cache: raise Exception("Configuration is not cached... you might want to use cache_master_config()" +\ " in config/environment.py.") return get_kcd_external_conf_object(master_config=_config_cache)
def handle_att(): ttw = mon_state.att_last_purge_time + admin_conf.db_purge_interval - time.time() if ttw <= 0: mon_state.action_flag = 1 mon_state.att_last_purge_time = time.time() try: open_pg_conn_if_needed() debug("Purging attachments.") # Get the expired attachments. now = int(time.time()) query = ("SELECT kws_id, email_id from kcd_kws_pub_email_info " + "WHERE att_expire_flag = 0 AND att_expire_date < %i") % (now) cur = exec_pg_query(mon_state.pg_conn, query) kws_list = [] for row in cur.fetchall(): kws_list.append((row[0], row[1])) mon_state.pg_conn.commit() # Purge the expired attachments. for kws_id, email_id in kws_list: # Get the associated files. query = ("SELECT inode_type, inode, commit_id FROM kcd_kws_kfs_current_view " + "WHERE kws_id = %i AND share_id = 0 AND inode_type = %i AND email_id = %i") \ % (kws_id, KFS_FILE, email_id) cur = exec_pg_query(mon_state.pg_conn, query) file_list = [] for row in cur.fetchall(): file_list.append((int(row[0]), long(row[1]), long(row[2]))) mon_state.pg_conn.commit() # Connect to the KCD and delete the files. if len(file_list): # Get a KCD client object. kc = BaseKcdClient(get_kcd_external_conf_object()) # Build a list of entries. delete_list = [] for inode_type, inode, commit_id in file_list: assert inode_type == KFS_FILE entry = KFSOpFileDelete(inode, commit_id) delete_list.append(entry) # Delete entries. share_id = 0 user_id = 0 kc.kfs_delete_entries(kws_id, share_id, user_id, email_id, delete_list) # Mark the email as expired. arg = ANP_msg() arg.add_u64(kws_id) arg.add_u64(email_id) exec_pg_query(mon_state.pg_conn, "SELECT purge_att(%s)" % (escape_pg_bytea(arg.get_payload()))) mon_state.pg_conn.commit() debug("Purged attachments.") except Exception, e: out("Purging attachments failed: %s." % str(e)) mon_state.pg_conn = None
def handle_att(): ttw = mon_state.att_last_purge_time + admin_conf.db_purge_interval - time.time( ) if ttw <= 0: mon_state.action_flag = 1 mon_state.att_last_purge_time = time.time() try: open_pg_conn_if_needed() debug("Purging attachments.") # Get the expired attachments. now = int(time.time()) query = ("SELECT kws_id, email_id from kcd_kws_pub_email_info " + "WHERE att_expire_flag = 0 AND att_expire_date < %i") % ( now) cur = exec_pg_query(mon_state.pg_conn, query) kws_list = [] for row in cur.fetchall(): kws_list.append((row[0], row[1])) mon_state.pg_conn.commit() # Purge the expired attachments. for kws_id, email_id in kws_list: # Get the associated files. query = ("SELECT inode_type, inode, commit_id FROM kcd_kws_kfs_current_view " + "WHERE kws_id = %i AND share_id = 0 AND inode_type = %i AND email_id = %i") \ % (kws_id, KFS_FILE, email_id) cur = exec_pg_query(mon_state.pg_conn, query) file_list = [] for row in cur.fetchall(): file_list.append((int(row[0]), long(row[1]), long(row[2]))) mon_state.pg_conn.commit() # Connect to the KCD and delete the files. if len(file_list): # Get a KCD client object. kc = BaseKcdClient(get_kcd_external_conf_object()) # Build a list of entries. delete_list = [] for inode_type, inode, commit_id in file_list: assert inode_type == KFS_FILE entry = KFSOpFileDelete(inode, commit_id) delete_list.append(entry) # Delete entries. share_id = 0 user_id = 0 kc.kfs_delete_entries(kws_id, share_id, user_id, email_id, delete_list) # Mark the email as expired. arg = ANP_msg() arg.add_u64(kws_id) arg.add_u64(email_id) exec_pg_query( mon_state.pg_conn, "SELECT purge_att(%s)" % (escape_pg_bytea(arg.get_payload()))) mon_state.pg_conn.commit() debug("Purged attachments.") except Exception, e: out("Purging attachments failed: %s." % str(e)) mon_state.pg_conn = None
def handle_download(self, opts, args): # KDebug parameters self.set_debug_levels(opts) # Arguments self.kws_id = int(args[0]) self.file = args[1] # Options self.user_id = 1 self.share_id = 0 self.download_file_as = self.file for opt, value in opts: if opt == "-u" or opt == "--user_id": self.user_id = value if opt == "-s" or opt == "--share_id": self.share_id = value if opt == "-a" or opt == "--download_file_as": self.download_file_as = value if opt == "-p" or opt == "--parent_id": self.parent_id = value # Connect to the KCD database. self.conn = self.get_db_conn() # Connect to KCD kdebug.debug(2, "Instantiating a KCD client.") c = BaseKcdClient(get_kcd_external_conf_object(), db_conn=self.conn) kdebug.debug(1, "Instantiated a KCD client.") # Get the entry details. entry = kfs_lib.kfs_kas_view_lookup_file(self.conn, self.kws_id, self.share_id, self.file, self.parent_inode) if not isinstance(entry, kfs_lib.KFSFile): raise Exception("This entry is not a file.") # Prepare file list. files = [] file = kfs_lib.KFSDownloadFile() file.inode = entry.inode file.commit_id = entry.commit_id file.size = entry.size file.comm = kfs_lib.KFSFileWriter(self.download_file_as) files.append(file) # Connecting to KCD. kdebug.debug(2, "Connecting to KCD.") c.connect() kdebug.debug(1, "Connected to KCD.") # Selecting KFS role. kdebug.debug(2, "Selecting KFS role.") c.select_role(kanp.KANP_KCD_ROLE_FILE_XFER) kdebug.debug(1, "Selected KFS role.") # Uploading file(s). kdebug.debug(2, "Downloading file(s) from kcd: kws_id %i, share %i, user %i." % \ ( self.kws_id, self.share_id, self.user_id ) ) c.kfs_download(self.kws_id, self.share_id, self.user_id, files) kdebug.debug(1, "Downloaded file(s) from kcd: kws_id %i, share %i, user %i." % \ ( self.kws_id, self.share_id, self.user_id ) ) # Errors? i = -1 for file in files: i += 1 if file.kfs_error: print "File %i could not be downloaded: '%s'." % ( i, file.kfs_error ) # Close KCD connection. c.close()
def handle_upload(self, opts, args): # KDebug parameters self.set_debug_levels(opts) # Arguments self.kws_id = int(args[0]) self.file = args[1] # Options self.user_id = 1 self.share_id = 0 self.upload_file_as = os.path.basename(self.file) for opt, value in opts: if opt == "-u" or opt == "--user_id": self.user_id = value if opt == "-s" or opt == "--share_id": self.share_id = value if opt == "-a" or opt == "--upload_file_as": self.upload_file_as = value # Connect to the KCD database. self.conn = self.get_db_conn() # Connect to KCD kdebug.debug(2, "Instantiating a KCD client.") c = BaseKcdClient(get_kcd_external_conf_object(), db_conn=self.conn) kdebug.debug(1, "Instantiated a KCD client.") # Prepare file list. files = [] file = kfs_lib.KFSUploadFile() file.kfs_op = kanp.KANP_KFS_OP_CREATE_FILE file.parent_inode = 0 file.parent_commit_id = 0 file.name = self.upload_file_as fd = os.open(self.file, os.O_RDONLY) file.set_from_fd(fd) files.append(file) # Connecting to KCD. kdebug.debug(2, "Connecting to KCD.") c.connect() kdebug.debug(1, "Connected to KCD.") # Selecting KFS role. kdebug.debug(2, "Selecting KFS role.") c.select_role(kanp.KANP_KCD_ROLE_FILE_XFER) kdebug.debug(1, "Selected KFS role.") # Uploading file(s). kdebug.debug(2, "Uploading file(s) to kcd %i, share %i, user %i." % \ ( self.kws_id, self.share_id, self.user_id ) ) c.kfs_upload(self.kws_id, self.share_id, self.user_id, files) kdebug.debug(1, "Uploaded file(s) to kcd %i, share %i, user %i." % \ ( self.kws_id, self.share_id, self.user_id ) ) # Errors? i = -1 for file in files: i += 1 if file.kfs_error: print "File %i could not be uploaded: '%s'." % ( i, file.kfs_error ) # Close KCD connection. c.close()
def handle_download(self, opts, args): # KDebug parameters self.set_debug_levels(opts) # Arguments self.kws_id = int(args[0]) self.file = args[1] # Options self.user_id = 1 self.share_id = 0 self.download_file_as = self.file for opt, value in opts: if opt == "-u" or opt == "--user_id": self.user_id = value if opt == "-s" or opt == "--share_id": self.share_id = value if opt == "-a" or opt == "--download_file_as": self.download_file_as = value if opt == "-p" or opt == "--parent_id": self.parent_id = value # Connect to the KCD database. self.conn = self.get_db_conn() # Connect to KCD kdebug.debug(2, "Instantiating a KCD client.") c = BaseKcdClient(get_kcd_external_conf_object(), db_conn=self.conn) kdebug.debug(1, "Instantiated a KCD client.") # Get the entry details. entry = kfs_lib.kfs_kas_view_lookup_file(self.conn, self.kws_id, self.share_id, self.file, self.parent_inode) if not isinstance(entry, kfs_lib.KFSFile): raise Exception("This entry is not a file.") # Prepare file list. files = [] file = kfs_lib.KFSDownloadFile() file.inode = entry.inode file.commit_id = entry.commit_id file.size = entry.size file.comm = kfs_lib.KFSFileWriter(self.download_file_as) files.append(file) # Connecting to KCD. kdebug.debug(2, "Connecting to KCD.") c.connect() kdebug.debug(1, "Connected to KCD.") # Selecting KFS role. kdebug.debug(2, "Selecting KFS role.") c.select_role(kanp.KANP_KCD_ROLE_FILE_XFER) kdebug.debug(1, "Selected KFS role.") # Uploading file(s). kdebug.debug(2, "Downloading file(s) from kcd: kws_id %i, share %i, user %i." % \ ( self.kws_id, self.share_id, self.user_id ) ) c.kfs_download(self.kws_id, self.share_id, self.user_id, files) kdebug.debug(1, "Downloaded file(s) from kcd: kws_id %i, share %i, user %i." % \ ( self.kws_id, self.share_id, self.user_id ) ) # Errors? i = -1 for file in files: i += 1 if file.kfs_error: print "File %i could not be downloaded: '%s'." % ( i, file.kfs_error) # Close KCD connection. c.close()
def handle_upload(self, opts, args): # KDebug parameters self.set_debug_levels(opts) # Arguments self.kws_id = int(args[0]) self.file = args[1] # Options self.user_id = 1 self.share_id = 0 self.upload_file_as = os.path.basename(self.file) for opt, value in opts: if opt == "-u" or opt == "--user_id": self.user_id = value if opt == "-s" or opt == "--share_id": self.share_id = value if opt == "-a" or opt == "--upload_file_as": self.upload_file_as = value # Connect to the KCD database. self.conn = self.get_db_conn() # Connect to KCD kdebug.debug(2, "Instantiating a KCD client.") c = BaseKcdClient(get_kcd_external_conf_object(), db_conn=self.conn) kdebug.debug(1, "Instantiated a KCD client.") # Prepare file list. files = [] file = kfs_lib.KFSUploadFile() file.kfs_op = kanp.KANP_KFS_OP_CREATE_FILE file.parent_inode = 0 file.parent_commit_id = 0 file.name = self.upload_file_as fd = os.open(self.file, os.O_RDONLY) file.set_from_fd(fd) files.append(file) # Connecting to KCD. kdebug.debug(2, "Connecting to KCD.") c.connect() kdebug.debug(1, "Connected to KCD.") # Selecting KFS role. kdebug.debug(2, "Selecting KFS role.") c.select_role(kanp.KANP_KCD_ROLE_FILE_XFER) kdebug.debug(1, "Selected KFS role.") # Uploading file(s). kdebug.debug(2, "Uploading file(s) to kcd %i, share %i, user %i." % \ ( self.kws_id, self.share_id, self.user_id ) ) c.kfs_upload(self.kws_id, self.share_id, self.user_id, files) kdebug.debug(1, "Uploaded file(s) to kcd %i, share %i, user %i." % \ ( self.kws_id, self.share_id, self.user_id ) ) # Errors? i = -1 for file in files: i += 1 if file.kfs_error: print "File %i could not be uploaded: '%s'." % (i, file.kfs_error) # Close KCD connection. c.close()
if len(args): usage() os._exit(1) # Print help. if help_flag: usage() os._exit(0) # Enable debug. if debug_flag: do_debug() # Parse the configuration. admin_conf = get_kfs_config_object() master_conf = get_kcd_external_conf_object() # Open the Postgres connection. debug("Opening Postgres connection.") db = open_pg_conn(database = "kcd", host = master_conf.db_host, port = master_conf.db_port, user = master_conf.db_user, password = master_conf.db_passwd) # Dispatch. try: if delete_kws_flag: delete_kws(kws_id) elif sync_kfs_flag: sync_kfs(kws_id) except Exception, e: out("Error: %s" % (str(e)))