Zhang Huangbin avatar Zhang Huangbin committed dd03fa6

New plugin: sql_user_restrictions. Not finished yet.

Comments (0)

Files changed (2)

src/libs/ldaplib.py

         if listpolicy == 'membersOnly':
             baseDN = self.baseDN
             searchScope = ldap.SCOPE_SUBTREE
-            # Filter used to get domain members.
+            # Filter used to get mail list members.
             searchFilter = self.cfg.get("ldap", "filter_member")
             searchAttr = self.cfg.get("ldap", "attr_member")
         else:
             baseDN = listdn
-            searchScope = ldap.SCOPE_BASE   # Use SCOPE_BASE to improve performance.
-            # Filter used to get domain moderators.
+            # Use SCOPE_BASE to improve performance.
+            searchScope = ldap.SCOPE_BASE
+            # Filter used to get mail list moderators.
             searchFilter = self.cfg.get("ldap", "filter_allowed_senders")
             searchAttr = self.cfg.get("ldap", "attr_moderator")
 

src/plugins/sql_user_restrictions.py

+# Author: Zhang Huangbin <zhb _at_ iredmail.org>
+
+# Purpose: Per-user send/receive restrictions.
+#
+# Required SQL columns of table `vmail.mailbox`.
+#   - mailbox.allowedrecipients: Allow user to send TO listed recipients
+#   - mailbox.rejectedrecipients: Reject emails sent TO listed recipients
+#   - mailbox.allowedsenders: Accept emails FROM listed senders
+#   - mailbox.rejectedsenders: Reject emails FROM listed senders
+#
+# Valid sender/recipient addresses:
+#
+#   - .*:           all addresses (user, domain, sub-domain)
+#   - domain.com:   single domain
+#   - .domain.com:  single domain and its all sub-domains
+#   - user@domain.com:  single email address
+
+from web import sqlquote
+from libs import SMTP_ACTIONS
+
+PLUGIN_NAME = 'sql_user_restrictions'
+
+def restriction(dbConn, senderReceiver, smtpSessionData, logger, **kargs):
+    #
+    # Allow to send to users under same domain and alias domains.
+    #
+    # Get restrictions for sender
+    sql = '''
+        SELECT \
+            allowedrecipients,rejectedrecipients,\
+            allowedsenders,rejectedsenders \
+        FROM mailbox \
+        WHERE username=%s
+        LIMIT 1
+    ''' % sqlquote(senderReceiver['sender'])
+    logger.debug('SQL: %s' % sql)
+
+    dbConn.execute(sql)
+    sql_record = dbConn.fetchone()
+    logger.debug('Returned SQL Record: %s' % str(sql_record))
+
+    # Recipient account doesn't exist.
+    if not sql_record:
+        return 'DUNNO Not a local user'
+
+    allowed_recipients, rejected_recipients, allowed_senders, rejected_senders = sql_record
+
+    # TODO Allowed first
+
+    return SMTP_ACTIONS['default']
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.