This file is indexed.

/usr/sbin/auth2db-alert is in auth2db 0.2.5-2+dfsg-4.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/python

#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
# 
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
# 
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

#  Copyright (c) 2007 Ezequiel Vera


__author__ = "Ezequiel Vera"
__version__ = "$Revision$"
__date__ = "$Date$"
__copyright__ = "Copyright (c) 2007 Ezequiel Vera"
__license__ = "GPL"


import MySQLdb
import os
import sys
import string
import time
import datetime
import traceback

import smtplib
from configobj import ConfigObj


#CONFIG_AUTH_PATH = "/var/log/"
CONFIG_PATH = "/etc/auth2db/"
CONFIG_PATH_FLAG = "/var/lib/auth2db/flag.d/"
CONFIG_PATH_TMP = "/var/lib/auth2db/tmp/"

config = ConfigObj(CONFIG_PATH+'auth2db.conf')
config_filters = ConfigObj(CONFIG_PATH+"filters.conf")

# Carga en las variables el archivo config.dat
CONFIG_HOST = config['dbserver']
CONFIG_DB = config['dbname']
CONFIG_USER = config['dbuser']
CONFIG_PASS = config['dbpass']


def send_email(subject,email,msg_body):
    
    import smtplib
    from email.MIMEText import MIMEText
    
    # connect
    db = MySQLdb.connect(host=CONFIG_HOST, user=CONFIG_USER, passwd=CONFIG_PASS, db=CONFIG_DB)
    # create a cursor
    cursor = db.cursor(MySQLdb.cursors.DictCursor)
    # execute SQL
    sql = "SELECT * from smtp_config "
    cursor.execute(sql)
    result = cursor.fetchall()
    
    for record in result:
        smtp_server = record["smtp_server"]
        port_server = record["smtp_port"]
        mail_from = record["mail_from"]
        auth_active = record["auth_active"]
        auth_user = record["auth_user"]
        auth_pass = record["auth_pass"]
    
    msg = MIMEText(msg_body)

    msg['Subject'] = str(subject)
    msg['From'] = mail_from
    msg['Reply-to'] = mail_from
    msg['To'] = str(email)

    s = smtplib.SMTP()
    s.connect(smtp_server)
    
    # if required authenticate
    if auth_active == 1:
        s.login(auth_user,auth_pass)
    
    # Send the email - real from, real to, extra headers and content ...
    s.sendmail(mail_from,str(email), msg.as_string())
    s.close()


def alert():
    
    # connect
    db = MySQLdb.connect(host=CONFIG_HOST, user=CONFIG_USER, passwd=CONFIG_PASS, db=CONFIG_DB)

    # create a cursor
    #cursor = db.cursor()
    cursor = db.cursor(MySQLdb.cursors.DictCursor)
    # execute SQL
    sql = "SELECT * from alert_config";
    cursor.execute(sql)
    result = cursor.fetchall()

    #print result

    new_alert = 0

    for record in result:
        #print str(record[0])+" "+record[1]
        id = str(record["id"])
        name = str(record["name"])
        log_type = str(record["log_type"])
        log_action = str(record["log_action"])
        log_group = str(record["log_group"])
        criticality = record["criticality"]
        severity = record["severity"]
        occurrences_number = str(record["occurrences_number"])
        occurrences_within = str(record["occurrences_within"])
        active_email = record["active_email"]
        email = record["email"]
        #occurrences_within = "3000000"
            
        sql = "SELECT count("+ log_group +") as cantidad, max(id) as maxid, login.* from login WHERE tipo = '"+ log_type +"' AND action = '"+ log_action +"' AND fecha > ( DATE_SUB( CURDATE(), INTERVAL "+ occurrences_within +" MINUTE) ) AND id > (SELECT IF (max(id_log) is not null, max(id_log), 0) as id FROM alert WHERE id_alert_config = "+ id +") GROUP BY "+ log_group +", server HAVING cantidad >= "+ occurrences_number +""
        #print sql
        #print ""
        
        cursor.execute(sql)
        sub_result = cursor.fetchall()
        
        for sub_record in sub_result:
            id_log = str(sub_record["maxid"])
            cantidad = str(sub_record["cantidad"])
            detalle = sub_record["detalle"]
            hostname = sub_record["server"]
            notified_time = time.strftime('%Y-%m-%d %H:%M:%S')
            
            sql = "INSERT INTO alert (id_alert_config, id_log, alert_name, hostname, criticality, severity, occurrences_number, detalle, notified_time) VALUES ('"+ id +"','"+ id_log +"', '"+ name +"', '"+ hostname +"', '"+ criticality +"','"+ severity +"','"+ cantidad +"','"+ detalle +"','"+ notified_time +"')"
            #print "["+name+"]" 
            #print sql
            cursor.execute(sql)
            insert = cursor.fetchall()
            
            # Send email if active_email = 1
            if active_email == 1:
                msg = "\n Alert Name: '"+ name +"'\n Hostname: '"+ hostname +"'\n Criticality '"+ criticality +"'\n Severity: '"+ severity +"'\n Occurrences Number: '"+ cantidad +"'\n Detail: '"+ detalle +"'\n Notified Time:'"+ notified_time + "'"
                send_email("Auth2DB Alert: " + name, email, msg)
                print "Auth2DB Alert: " + name +" | "+ email
            
            new_alert = new_alert + 1
        

    print "New Alerts: " + str(new_alert)
        #print ""
        #print insert
        #print ""


def main():
	alert()

#Esta linea lanza la funcion principal si aun no esta lanzada
if __name__ =='__main__':
    try:
        main()
    except:
        # print error message re exception
        traceback.print_exc()