def ldif_mailuser(domain, username, cn, passwd, quota=0, aliasDomains=[], groups=[],storageBaseDirectory=None,): domain = str(domain).lower() username = ldaputils.removeSpace(str(username)).lower() mail = username + '@' + domain if storageBaseDirectory is None: tmpStorageBaseDirectory = cfg.general.get('storage_base_directory').lower() else: tmpStorageBaseDirectory = storageBaseDirectory splitedSBD = tmpStorageBaseDirectory.rstrip('/').split('/') storageNode = splitedSBD.pop() storageBaseDirectory = '/'.join(splitedSBD) mailMessageStore = storageNode + '/' + iredutils.setMailMessageStore(mail) homeDirectory = storageBaseDirectory + '/' + mailMessageStore # Generate basic LDIF. ldif = [ ('objectClass', ['inetOrgPerson', 'mailUser', 'shadowAccount', 'amavisAccount',]), ('mail', [mail]), ('userPassword', [str(passwd)]), ('sn', [username]), ('uid', [username]), ('storageBaseDirectory', [storageBaseDirectory]), ('mailMessageStore', [mailMessageStore]), ('homeDirectory', [homeDirectory]), ('accountStatus', ['active']), ('enabledService', ['mail', 'deliver', 'lda', 'smtp', 'smtpsecured', 'pop3', 'pop3secured', 'imap', 'imapsecured', 'managesieve', 'managesievesecured', # ManageService name In dovecot-1.2. 'sieve', 'sievesecured', 'forward', 'senderbcc', 'recipientbcc', 'internal', 'shadowaddress', 'displayedInGlobalAddressBook',]), # Amavisd integration. ('amavisLocal', ['TRUE']), ] # Append @shadowAddress. shadowAddresses = [] for d in aliasDomains: if iredutils.isDomain(d): shadowAddresses += [username + '@' + d] if len(shadowAddresses) > 0: ldif += [('shadowAddress', shadowAddresses)] # Append quota. No 'mailQuota' attribute means unlimited. quota = str(quota).strip() if quota.isdigit(): quota = int(quota) * 1024 * 1024 ldif += [('mailQuota', [str(quota)])] # Append cn. ldif += ldaputils.getLdifOfSingleAttr(attr='cn', value=cn, default=username,) # Append groups. if isinstance(groups, list) and len(groups) >= 1: # Remove duplicate items. grps = set() for g in groups: grps.update([str(g).strip()]) ldif += [('memberOfGroup', list(grps))] return ldif
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 if self.domain != domain: return (False, 'PERMISSION_DENIED') if not iredutils.isDomain(self.domain): return (False, 'INVALID_DOMAIN_NAME') # 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='user', domains=[self.domain]) if self.domainProfile.mailboxes == 0: # Unlimited. pass elif self.domainProfile.mailboxes <= numberOfExistAccounts: return (False, 'EXCEEDED_DOMAIN_ACCOUNT_LIMIT') # 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 limited max quota. 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 if spareQuota > 0: if spareQuota < self.mailQuota: self.mailQuota = spareQuota else: # No enough quota. return (False, 'EXCEEDED_DOMAIN_QUOTA_SIZE') # # Get password from <form>. # self.newpw = str(data.get('newpw', '')) self.confirmpw = str(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( self.newpw, self.confirmpw, min_passwd_length=self.minPasswordLength, max_passwd_length=self.maxPasswordLength, ) if resultOfPW[0] is True: self.passwd = iredutils.getSQLPassword(resultOfPW[1]) else: return resultOfPW # Get display name from <form> self.cn = data.get('cn', '') # Assign new user to default mail aliases. assignedAliases = [ str(v).lower() for v in str(self.domainProfile.defaultuseraliases).split(',') if iredutils.isEmail(v) ] try: # Store new user in SQL db. self.conn.insert( 'mailbox', domain=self.domain, username=self.mail, password=self.passwd, name=self.cn, maildir=iredutils.setMailMessageStore(self.mail), quota=self.mailQuota, created=iredutils.sqlNOW, active='1', local_part=self.username, ) # Assign new user to default mail aliases. if len(assignedAliases) > 0: for ali in assignedAliases: try: self.conn.query(''' UPDATE alias SET goto=CONCAT(goto, %s) WHERE address=%s AND domain=%s ''' % ( web.sqlquote(',' + self.mail), web.sqlquote(ali), web.sqlquote(self.domain), )) except: pass # Create an alias account: address=goto. self.conn.insert( 'alias', address=self.mail, goto=self.mail, domain=self.domain, created=iredutils.sqlNOW, active='1', ) web.logger( msg="Create user: %s." % (self.mail), domain=self.domain, event='create', ) return (True, ) except Exception, e: return (False, str(e))
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 if self.domain != domain: return (False, 'PERMISSION_DENIED') if not iredutils.isDomain(self.domain): return (False, 'INVALID_DOMAIN_NAME') # 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='user', domains=[self.domain]) if self.domainProfile.mailboxes == 0: # Unlimited. pass elif self.domainProfile.mailboxes <= numberOfExistAccounts: return (False, 'EXCEEDED_DOMAIN_ACCOUNT_LIMIT') # 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 limited max quota. 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 if spareQuota > 0: if spareQuota < self.mailQuota: self.mailQuota = spareQuota else: # No enough quota. return (False, 'EXCEEDED_DOMAIN_QUOTA_SIZE') # # Get password from <form>. # self.newpw = str(data.get('newpw', '')) self.confirmpw = str(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( self.newpw, self.confirmpw, min_passwd_length=self.minPasswordLength, max_passwd_length=self.maxPasswordLength, ) if resultOfPW[0] is True: self.passwd = iredutils.getSQLPassword(resultOfPW[1]) else: return resultOfPW # Get display name from <form> self.cn = data.get('cn', '') # Assign new user to default mail aliases. assignedAliases = [str(v).lower() for v in str(self.domainProfile.defaultuseraliases).split(',') if iredutils.isEmail(v) ] try: # Store new user in SQL db. self.conn.insert( 'mailbox', domain=self.domain, username=self.mail, password=self.passwd, name=self.cn, maildir=iredutils.setMailMessageStore(self.mail), quota=self.mailQuota, created=iredutils.sqlNOW, active='1', local_part=self.username, ) # Assign new user to default mail aliases. if len(assignedAliases) > 0: for ali in assignedAliases: try: self.conn.query( ''' UPDATE alias SET goto=CONCAT(goto, %s) WHERE address=%s AND domain=%s ''' % ( web.sqlquote(','+self.mail), web.sqlquote(ali), web.sqlquote(self.domain), ) ) except: pass # Create an alias account: address=goto. self.conn.insert( 'alias', address=self.mail, goto=self.mail, domain=self.domain, created=iredutils.sqlNOW, active='1', ) web.logger(msg="Create user: %s." % (self.mail), domain=self.domain, event='create',) return (True,) except Exception, e: return (False, str(e))
def ldif_mailuser( domain, username, cn, passwd, quota=0, aliasDomains=[], groups=[], storageBaseDirectory=None, ): domain = str(domain).lower() username = ldaputils.removeSpace(str(username)).lower() mail = username + '@' + domain if storageBaseDirectory is None: tmpStorageBaseDirectory = cfg.general.get( 'storage_base_directory').lower() else: tmpStorageBaseDirectory = storageBaseDirectory splitedSBD = tmpStorageBaseDirectory.rstrip('/').split('/') storageNode = splitedSBD.pop() storageBaseDirectory = '/'.join(splitedSBD) mailMessageStore = storageNode + '/' + iredutils.setMailMessageStore(mail) homeDirectory = storageBaseDirectory + '/' + mailMessageStore # Generate basic LDIF. ldif = [ ('objectClass', [ 'inetOrgPerson', 'mailUser', 'shadowAccount', 'amavisAccount', ]), ('mail', [mail]), ('userPassword', [str(passwd)]), ('sn', [username]), ('uid', [username]), ('storageBaseDirectory', [storageBaseDirectory]), ('mailMessageStore', [mailMessageStore]), ('homeDirectory', [homeDirectory]), ('accountStatus', ['active']), ( 'enabledService', [ 'mail', 'deliver', 'lda', 'smtp', 'smtpsecured', 'pop3', 'pop3secured', 'imap', 'imapsecured', 'managesieve', 'managesievesecured', # ManageService name In dovecot-1.2. 'sieve', 'sievesecured', 'forward', 'senderbcc', 'recipientbcc', 'internal', 'shadowaddress', 'displayedInGlobalAddressBook', ]), # Amavisd integration. ('amavisLocal', ['TRUE']), ] # Append @shadowAddress. shadowAddresses = [] for d in aliasDomains: if iredutils.isDomain(d): shadowAddresses += [username + '@' + d] if len(shadowAddresses) > 0: ldif += [('shadowAddress', shadowAddresses)] # Append quota. No 'mailQuota' attribute means unlimited. quota = str(quota).strip() if quota.isdigit(): quota = int(quota) * 1024 * 1024 ldif += [('mailQuota', [str(quota)])] # Append cn. ldif += ldaputils.getLdifOfSingleAttr( attr='cn', value=cn, default=username, ) # Append groups. if isinstance(groups, list) and len(groups) >= 1: # Remove duplicate items. grps = set() for g in groups: grps.update([str(g).strip()]) ldif += [('memberOfGroup', list(grps))] return ldif