def POST(self): i = web.input( domainName=[], _unicode=False, ) domainName = i.get('domainName', None) self.action = i.get('action') domainLib = domainlib.Domain() if self.action == 'delete': result = domainLib.delete(domains=domainName) msg = 'DELETED' elif self.action == 'disable': result = domainLib.enableOrDisableAccount( accounts=domainName, active=False, ) msg = 'DISABLED' elif self.action == 'enable': result = domainLib.enableOrDisableAccount( accounts=domainName, active=True, ) msg = 'ENABLED' else: result = (False, 'INVALID_ACTION') msg = i.get('msg', None) if result[0] is True: raise web.seeother('/domains?msg=%s' % msg) else: raise web.seeother('/domains?msg=' + web.urlquote(result[1]))
def POST(self, profile_type, domain): self.profile_type = str(profile_type) self.domain = str(domain) i = web.input( domainAliasName=[], domainAdmin=[], defaultList=[], ) domainLib = domainlib.Domain() result = domainLib.update( profile_type=self.profile_type, domain=self.domain, data=i, ) if result[0] is True: raise web.seeother('/profile/domain/%s/%s?msg=UPDATED' % (self.profile_type, self.domain)) else: raise web.seeother('/profile/domain/%s/%s?msg=%s' % ( self.profile_type, self.domain, web.urlquote(result[1]), ))
def GET( self, domain=None, ): if domain is None: self.cur_domain = None else: self.cur_domain = str(domain) if not iredutils.isDomain(self.cur_domain): raise web.seeother('/domains?msg=INVALID_DOMAIN_NAME') i = web.input() # Get all managed domains. connutils = connUtils.Utils() qr = connutils.getManagedDomains( admin=session.get('username'), domainNameOnly=True, ) if qr[0] is True: allDomains = qr[1] else: raise web.seeother('/domains?msg=' + web.urlquote(qr[1])) # Set first domain as current domain. if self.cur_domain is None: if len(allDomains) > 0: raise web.seeother('/create/alias/%s' % str(allDomains[0])) else: raise web.seeother('/domains?msg=NO_DOMAIN_AVAILABLE') # Get domain profile. domainLib = domainlib.Domain() resultOfProfile = domainLib.profile(domain=self.cur_domain) if resultOfProfile[0] is True: self.profile = resultOfProfile[1] else: raise web.seeother('/domains?msg=%s' % web.urlquote(resultOfProfile[1])) # Cet total number and allocated quota size of existing users under domain. self.numberOfExistAccounts = 0 resultOfCount = domainLib.getCountsOfExistAccountsUnderDomain( domain=self.cur_domain, accountType='alias', ) if resultOfCount[0] is True: self.numberOfExistAccounts = resultOfCount[1] return web.render( 'dbmail_mysql/alias/create.html', cur_domain=self.cur_domain, allDomains=allDomains, profile=self.profile, numberOfExistAccounts=self.numberOfExistAccounts, numberOfAccounts=2, msg=i.get('msg'), )
def GET(self, profile_type, mail): i = web.input() self.mail = web.safestr(mail) self.profile_type = web.safestr(profile_type) if not iredutils.isEmail(self.mail): raise web.seeother('/admins?msg=INVALID_MAIL') if session.get('domainGlobalAdmin' ) is not True and session.get('username') != self.mail: # Don't allow to view/update other admins' profile. raise web.seeother( '/profile/admin/general/%s?msg=PERMISSION_DENIED' % session.get('username')) adminLib = adminlib.Admin() result = adminLib.profile(mail=self.mail) if result[0] is True: domainGlobalAdmin, profile = result[1], result[2] # Get all domains. self.allDomains = [] domainLib = domainlib.Domain() resultOfAllDomains = domainLib.getAllDomains() if resultOfAllDomains[0] is True: self.allDomains = resultOfAllDomains[1] # Get managed domains. self.managedDomains = [] connutils = connUtils.Utils() qr = connutils.getManagedDomains( admin=self.mail, domainNameOnly=True, listedOnly=True, ) if qr[0] is True: self.managedDomains += qr[1] return web.render( 'dbmail_mysql/admin/profile.html', mail=self.mail, profile_type=self.profile_type, domainGlobalAdmin=domainGlobalAdmin, profile=profile, languagemaps=languages.getLanguageMaps(), allDomains=self.allDomains, managedDomains=self.managedDomains, min_passwd_length=cfg.general.get('min_passwd_length', '0'), max_passwd_length=cfg.general.get('max_passwd_length', '0'), msg=i.get('msg'), ) else: raise web.seeother('/admins?msg=' + web.urlquote(result[1]))
def add(self, domain, data): # Get domain name, username, cn. self.domain = web.safestr(data.get('domainName')).strip().lower() self.username = web.safestr(data.get('listname')).strip().lower() self.mail = self.username + '@' + self.domain if self.domain != domain: return (False, 'PERMISSION_DENIED') if not iredutils.isDomain(self.domain): return (False, 'INVALID_DOMAIN_NAME') if not iredutils.isEmail(self.mail): return (False, 'INVALID_MAIL') # Define columns and values used to insert. columns = {'domain': self.domain, 'alias': self.mail, } # Check account existing. connutils = connUtils.Utils() if connutils.isEmailExists(mail=self.mail): return (False, 'ALREADY_EXISTS') # Get domain profile. domainLib = domainlib.Domain() resultOfDomainProfile = domainLib.profile(domain=self.domain) if resultOfDomainProfile[0] is True: self.domainProfile = resultOfDomainProfile[1] else: return resultOfDomainProfile # Check account limit. adminLib = adminlib.Admin() numberOfExistAccounts = adminLib.getNumberOfManagedAccounts(accountType='alias', domains=[self.domain]) if self.domainProfile.aliases == -1: return (False, 'NOT_ALLOWED') elif self.domainProfile.aliases > 0: if self.domainProfile.aliases <= numberOfExistAccounts: return (False, 'EXCEEDED_DOMAIN_ACCOUNT_LIMIT') # Get display name from <form> columns['name'] = data.get('cn', '') try: # Store new user in required SQL DBs. self.conn.insert( 'dbmail_aliases_extra', **columns ) web.logger(msg="Create mail alias: %s." % (self.mail), domain=self.domain, event='create',) return (True,) except Exception, e: return (False, str(e))
def POST(self): i = web.input() self.domain = web.safestr(i.get('domainName')).strip().lower() domainLib = domainlib.Domain() result = domainLib.add(data=i) if result[0] is True: raise web.seeother('/profile/domain/general/%s?msg=CREATED' % self.domain) else: raise web.seeother('/create/domain?msg=%s' % web.urlquote(result[1]))
def delete(self, domain, mails=[]): self.domain = str(domain) if not iredutils.isDomain(self.domain): return (False, 'INVALID_DOMAIN_NAME') if not isinstance(mails, (list, tuple,)): return (False, 'INVALID_MAIL') self.mails = [str(v).lower() for v in mails if iredutils.isEmail(v) and str(v).endswith('@' + self.domain) ] # Remove alias from domain.defaultuseraliases. # Get domain profile. domainLib = domainlib.Domain() qr = domainLib.simpleProfile(domain=self.domain, columns=['domain', 'defaultuseraliases', ]) if qr[0] is True: self.domainProfile = qr[1] else: return qr self.defaultUserAliases = self.domainProfile.defaultuseraliases.split(',') # Remove from domain.defaultuseraliases. self.newDefaultAliases = [str(v).lower() for v in self.defaultUserAliases if v not in self.mails ] # Delete domain and related records. try: self.conn.delete('dbmail_aliases_extra', where='%s' % web.sqlors('alias = ', self.mails),) self.conn.delete('dbmail_aliases', where='%s' % web.sqlors('alias = ', self.mails),) self.conn.update('dbmail_domains', vars={'domain': self.domain, }, defaultuseraliases=','.join(self.newDefaultAliases), modified=iredutils.getGMTTime(), where='domain = $domain', ) web.logger( msg="Delete mail alias: %s." % ', '.join(self.mails), domain=self.domain, event='delete', ) return (True,) except Exception, e: return (False, str(e))
def GET( self, cur_page=1, ): i = web.input() cur_page = int(cur_page) or 1 domainLib = domainlib.Domain() result = domainLib.listAccounts(cur_page=cur_page) if result[0] is True: allDomains = result[2] aliasDomains = {} # Get list of domain names. all_domain_names = [r.domain for r in allDomains] qr = domainLib.getAllAliasDomains(all_domain_names, namesOnly=True) if qr[0] is True: aliasDomains = qr[1] return web.render( 'dbmail_mysql/domain/list.html', cur_page=cur_page, total=result[1], allDomains=result[2], aliasDomains=aliasDomains, msg=i.get('msg', None), ) else: return web.render( 'dbmail_mysql/domain/list.html', cur_page=cur_page, total=0, allDomains=[], msg=result[1], )
def GET(self, profile_type, domain): i = web.input() self.domain = web.safestr(domain.split('/', 1)[0]) self.profile_type = web.safestr(profile_type) domainLib = domainlib.Domain() result = domainLib.profile(domain=self.domain) if result[0] is not True: raise web.seeother('/domains?msg=' + web.urlquote(result[1])) else: self.profile = result[1] allAdmins = [] # Get all admins. domainAdmins = [] # Get domain admins. aliasDomains = [] # Get all alias domains. allAliases = [] # Get all mail aliases. # profile_type == 'throttle' throttleOfSender = {} throttleOfRecipient = {} adminLib = adminlib.Admin() # Get all admins. qr = adminLib.getAllAdmins(columns=['username', 'name']) if qr[0] is True: allAdmins = qr[1] # Get domain admins. qr = domainLib.getDomainAdmins(domain=self.domain, mailOnly=True) if qr[0] is True: domainAdmins = qr[1] # Get alias domains. qr = domainLib.getAllAliasDomains( self.domain, namesOnly=True, ) if qr[0] is True: aliasDomains = qr[1] # Get all mail aliases. mailsOfAllAliases = [] aliasLib = aliaslib.Alias() qr = aliasLib.getAllAliases(domain=self.domain, columns=[ 'name', 'alias', ]) if qr[0] is True: allAliases = qr[1] for ali in allAliases: mailsOfAllAliases += [ali.alias] # Get sender/recipient throttle data from policyd database. if session.get('enablePolicyd'): throttleLib = throttle.Throttle() result_throttle = throttleLib.getThrottling( sender='@' + self.domain, recipient='@' + self.domain) if result_throttle[0] is True: throttleOfSender = result_throttle[1] throttleOfRecipient = result_throttle[2] return web.render( 'dbmail_mysql/domain/profile.html', cur_domain=self.domain, profile_type=self.profile_type, profile=self.profile, allAdmins=allAdmins, domainAdmins=domainAdmins, aliasDomains=aliasDomains, allAliases=allAliases, mailsOfAllAliases=mailsOfAllAliases, throttleOfSender=throttleOfSender, throttleOfRecipient=throttleOfRecipient, msg=i.get('msg'), )
def add(self, domain, data): # Get domain name, username, cn. self.domain = web.safestr(data.get('domainName')).strip().lower() self.username = web.safestr(data.get('username')).strip().lower() self.mail = self.username + '@' + self.domain sql_vars = { 'mail': self.mail, } if not iredutils.isDomain(self.domain): return (False, 'INVALID_DOMAIN_NAME') if self.domain != domain: return (False, 'PERMISSION_DENIED') if not iredutils.isEmail(self.mail): return (False, 'INVALID_MAIL') # Check account existing. connutils = connUtils.Utils() if connutils.isEmailExists(self.mail): return (False, 'ALREADY_EXISTS') # Get domain profile. domainLib = domainlib.Domain() resultOfDomainProfile = domainLib.profile(domain=self.domain) if resultOfDomainProfile[0] is True: self.domainProfile = resultOfDomainProfile[1] else: return resultOfDomainProfile # Check account limit. adminLib = adminlib.Admin() numberOfExistAccounts = adminLib.getNumberOfManagedAccounts( accountType='user', domains=[self.domain]) if self.domainProfile.mailboxes == -1: return (False, 'NOT_ALLOWED') elif self.domainProfile.mailboxes > 0: if self.domainProfile.mailboxes <= numberOfExistAccounts: return (False, 'EXCEEDED_DOMAIN_ACCOUNT_LIMIT') columns = { 'userid': self.mail, 'domain': self.domain, } # Check spare quota and number of spare account limit. # Get quota from form. self.mailQuota = str(data.get('mailQuota')).strip() self.defaultUserQuota = self.domainProfile.get('defaultuserquota', 0) if self.mailQuota.isdigit(): self.mailQuota = int(self.mailQuota) else: self.mailQuota = self.defaultUserQuota # Re-calculate mail quota if this domain has max quota limit. if self.domainProfile.maxquota > 0: # Get used quota. qr = domainLib.getAllocatedQuotaSize(domain=self.domain) if qr[0] is True: self.allocatedQuota = qr[1] else: return qr spareQuota = self.domainProfile.maxquota - self.allocatedQuota / 1024 / 1024 if spareQuota > 0: if spareQuota < self.mailQuota: self.mailQuota = spareQuota else: # No enough quota. return (False, 'EXCEEDED_DOMAIN_QUOTA_SIZE') columns['maxmail_size'] = self.mailQuota * 1024 * 1024 # # Get password from <form>. # newpw = web.safestr(data.get('newpw', '')) confirmpw = web.safestr(data.get('confirmpw', '')) # Get password length limit from domain profile or global setting. self.minPasswordLength = self.domainProfile.get( 'minpasswordlength', cfg.general.get('min_passwd_length', '0')) self.maxPasswordLength = self.domainProfile.get( 'maxpasswordlength', cfg.general.get('max_passwd_length', '0')) resultOfPW = iredutils.verifyNewPasswords( newpw, confirmpw, min_passwd_length=self.minPasswordLength, max_passwd_length=self.maxPasswordLength, ) if resultOfPW[0] is True: if 'storePasswordInPlainText' in data: columns['passwd'] = iredutils.getSQLPassword(resultOfPW[1], pwscheme='PLAIN') columns['encryption_type'] = '' else: columns['passwd'] = iredutils.getSQLPassword(resultOfPW[1]) columns[ 'encryption_type'] = settings.SQL_DEFAULT_PASSWD_SCHEME.lower( ) else: return resultOfPW # Get display name from <form> columns['name'] = data.get('cn', '') # Assign new user to default mail aliases. assignedAliases = [ str(addr).lower() for addr in str(self.domainProfile.defaultuseraliases).split(',') if iredutils.isEmail(addr) ] try: # Store new user in SQL db. self.conn.insert('dbmail_users', **columns) # Get dbmail_users.user_idnr. qr = self.conn.select( 'dbmail_users', vars=sql_vars, what='user_idnr,client_idnr', where='userid=$mail', limit=1, ) p = qr[0] user_idnr, client_idnr = p.user_idnr, p.client_idnr self.conn.insert( 'dbmail_aliases', alias=self.mail, deliver_to=user_idnr, client_idnr=client_idnr, ) # Create and subscribe to default IMAP folders. if settings.DBMAIL_CREATE_DEFAULT_IMAP_FOLDERS: # Create default IMAP folders. imap_folders = [ '(%d, "%s")' % (user_idnr, fld) for fld in settings.DBMAIL_DEFAULT_IMAP_FOLDERS ] self.conn.query( '''INSERT INTO dbmail_mailboxes (owner_idnr, name) VALUES %s''' % ','.join(imap_folders)) # Subscribe to folders by default. self.conn.query( '''INSERT INTO dbmail_subscription (user_id, mailbox_id) SELECT owner_idnr, mailbox_idnr FROM dbmail_mailboxes WHERE owner_idnr = %d ''' % user_idnr) # Assign new user to default mail aliases. if len(assignedAliases) > 0: for ali in assignedAliases: try: self.conn.update( 'dbmail_aliases', vars={ 'mail': self.mail, 'ali': ali, 'user_idnr': user_idnr, }, where='alias = $ali AND deliver_to <> $user_idnr', deliver_to=web.sqlliteral( 'CONCAT($mail, ",", deliver_to)'), ) except: pass vars_addition_sql = { 'user_idnr': user_idnr, 'mail': self.mail, 'username': self.username, 'domain': self.domain, } # Execute addition SQL commands after successfully created new users. if settings.DBMAIL_SQL_FOR_NEWLY_CREATED_USER: try: for sql_cmd in settings.DBMAIL_SQL_FOR_NEWLY_CREATED_USER: self.conn.query(sql_cmd, vars=vars_addition_sql) except Exception: pass # Create Amavisd policy for newly created user. if settings.AMAVISD_EXECUTE_SQL_WITHOUT_ENABLED and settings.AMAVISD_SQL_FOR_NEWLY_CREATED_USER: try: from libs.amavisd.core import AmavisdWrap amwrap = AmavisdWrap() for sql_cmd in settings.AMAVISD_SQL_FOR_NEWLY_CREATED_USER: amwrap.db.query(sql_cmd, vars=vars_addition_sql) except: pass web.logger( msg="Create user: %s." % (self.mail), domain=self.domain, event='create', ) return (True, ) except Exception, e: return (False, str(e))