Example #1
0
def checkUser(validate, recipient, password = None):
    we_serve = False
    
    if settings.backend == 'ldap':
        adm_con = get_db_conn('ldap')
        if adm_con:
            if validate == 'email':
                filter = "(&(objectClass=mailUser)(mail=%s))" % recipient
                result = adm_con.search_s(settings.ldap_basedn, 
                                          ldap.SCOPE_SUBTREE, 
                                          filter, 
                                          ['mail'])
                if result:
                    we_serve = True
            elif validate == 'user':
                if password:
                    filter = "(&(objectClass=mailUser)(mail=%s))" % recipient
                    result = adm_con.search_s(settings.ldap_basedn, 
                                              ldap.SCOPE_SUBTREE, 
                                              filter,
                                              ['userPassword'])
                    if result:
                        challenge = result[0][1]['userPassword'][0]
                        we_serve = iredpwd.verify_password_hash(challenge, password)
            else:
                domain = recipient.split('@')[1]
                filter =  "(&(objectClass=mailDomain)(domainName=%s))" % domain
                result = adm_con.search_s(settings.ldap_basedn, 
                                          ldap.SCOPE_SUBTREE, 
                                          filter, 
                                          ['domainName'])
                if result:
                    we_serve = True
    else:
        adm_con = get_db_conn('vmail')
        if adm_con:
            if validate == 'email':
                domain = recipient.split('@')[1]
                user_where = "username='******' and domain='%s'" % (recipient, domain)
                row = adm_con.select('mailbox', where=user_where)
                if row:
                    we_serve = True
            elif validate == 'user':
                if password:
                    domain = recipient.split('@')[1]
                    user_where = "username='******' and domain='%s'" % (recipient, domain)
                    row = adm_con.select('mailbox', what='password', where=user_where)
                    if row:
                        challenge = row[0].password
                        print challenge
                        we_serve = iredpwd.verify_password_hash(challenge, password)
            else:
                domain_where = "domain='%s'" % recipient.split('@')[1]
                row = adm_con.select('domain', where=domain_where)
                if row:
                    we_serve = True

    return we_serve
Example #2
0
def show_wblist(list_type = None, recipient = None, silent = False):
    all = """select u.email as recipient, m.email as sender, w.wb as policy, 
            m.priority as priority from users u, mailaddr m, wblist w 
            where m.id = w.sid and u.id = w.rid
           """
    
    if recipient:
        sql = "%s and u.email = '%s'" % (all, recipient)
    else:
        sql = all

    if list_type and list_type not in ("blacklist", "whitelist"):
        raise Exception("%s: Unknown list type" % list_type)
    elif list_type:
        if list_type == 'blacklist':
            sql += " and w.wb = 'B'"
        else:
            sql += " and w.wb = 'W'"

    try:
        conn = get_db_conn('amavisd')
        
        rows = conn.query(sql)
        
        if rows:
            if silent:
                list = []
            else:
                out = "%-30s %-30s %s %s\n" % ("Recipient","Sender","Policy", "Priority")
                out += "%s %s %s %s\n" % ("------------------------------","------------------------------","------","--------")
            for row in rows:
                if silent:
                    list.append(row)
                else:            
                    out += "%-30s %-30s %+6s %+8s\n" % (row.recipient, row.sender, row.policy, row.priority)
            if not silent:
                out += "\nFound %d instances." % len(rows)
        else:
            if silent:
                list = []
            else:
                out = "Nothing to show"
        
        if not silent:
            print out
        else:
            return list
    except:
        raise
Example #3
0
def update_wblist(action, list_type, wblist, recipient = None):
    
    if not checkRecipient(recipient):
        raise Exception("%s: Unknown recipient" % recipient)

    user_priority = getPriority(recipient)
    if not user_priority:
        raise Exception("Error: Could not determine address")

    if list_type not in ("blacklist", "whitelist"):
        raise Exception("%s: Unknown list type" % list_type)

    if action not in ("add", "delete"):
        raise Exception("%s: Unknown action" % action)

    conn = get_db_conn('amavisd')

    try:
        t = conn.transaction()
    except:
        raise

    rid = None
    try:
        # The mysql driver in webpy.db crashes if any exceptions is raised
        where = "email='%s'" % user_priority['email']
        row = conn.select('users', where=where)
        if row:
            rid = int(row[0].id)
        if not rid:
            raise Exception("Error: Recipient does not exist")
    except:
        t.rollback()
        raise

    if list_type == 'blacklist':
        wb = 'B'
    else:
        wb = 'W'
    
    try:
        for l in wblist:
            sid = None
            priority = getPriority(l)
            if not priority:
                msg = "%s: Could not determine priority" % l
                logging.warning(msg)
                continue
            # The mysql driver in webpy.db crashes if any exceptions is raised
            where = "email='%s'" % priority['email']
            row = conn.select('mailaddr', where=where)

            if action == 'delete':
                if row:
                    sid = int(row[0].id)
                else:
                    msg = "%s: Does not exists" % priority['email']
                    logging.warning(msg)
                    continue
                where = "rid=%d and sid=%d and wb='%s'" % (rid, sid, wb)
                n = int(conn.delete('wblist', where=where))
                if n:
                    where = "email='%s'" % priority['email']
                    n = int(conn.delete('mailaddr', where=where))
                else:
                    msg = "%s: No %s" % (priority['email'], list_type)
                    logging.warning(msg)
                    continue
                if not n:
                    msg = "%s: Missing relation" % priority['email']
                    logging.error(msg)
                    raise Exception(msg)
            else:
                if row:
                    msg = "%s: Exists" % row[0].email
                    logging.warning(msg)
                    continue
                sid = int(conn.insert('mailaddr', email=priority['email'], priority=int(priority['priority'])))
                conn.insert('wblist', rid=rid, sid=sid, wb=wb)
        t.commit()
    except:
        t.rollback()
        raise