github.com/amar224/phishing-tool@v0.9.0/models/imap.go (about)

     1  package models
     2  
     3  import (
     4  	"errors"
     5  	"net"
     6  	"time"
     7  
     8  	log "github.com/gophish/gophish/logger"
     9  )
    10  
    11  const DefaultIMAPFolder = "INBOX"
    12  const DefaultIMAPFreq = 60 // Every 60 seconds
    13  
    14  // IMAP contains the attributes needed to handle logging into an IMAP server to check
    15  // for reported emails
    16  type IMAP struct {
    17  	UserId                      int64     `json:"-" gorm:"column:user_id"`
    18  	Enabled                     bool      `json:"enabled"`
    19  	Host                        string    `json:"host"`
    20  	Port                        uint16    `json:"port,string,omitempty"`
    21  	Username                    string    `json:"username"`
    22  	Password                    string    `json:"password"`
    23  	TLS                         bool      `json:"tls"`
    24  	Folder                      string    `json:"folder"`
    25  	RestrictDomain              string    `json:"restrict_domain"`
    26  	DeleteReportedCampaignEmail bool      `json:"delete_reported_campaign_email"`
    27  	LastLogin                   time.Time `json:"last_login,omitempty"`
    28  	ModifiedDate                time.Time `json:"modified_date"`
    29  	IMAPFreq                    uint32    `json:"imap_freq,string,omitempty"`
    30  }
    31  
    32  // ErrIMAPHostNotSpecified is thrown when there is no Host specified
    33  // in the IMAP configuration
    34  var ErrIMAPHostNotSpecified = errors.New("No IMAP Host specified")
    35  
    36  // ErrIMAPPortNotSpecified is thrown when there is no Port specified
    37  // in the IMAP configuration
    38  var ErrIMAPPortNotSpecified = errors.New("No IMAP Port specified")
    39  
    40  // ErrInvalidIMAPHost indicates that the IMAP server string is invalid
    41  var ErrInvalidIMAPHost = errors.New("Invalid IMAP server address")
    42  
    43  // ErrInvalidIMAPPort indicates that the IMAP Port is invalid
    44  var ErrInvalidIMAPPort = errors.New("Invalid IMAP Port")
    45  
    46  // ErrIMAPUsernameNotSpecified is thrown when there is no Username specified
    47  // in the IMAP configuration
    48  var ErrIMAPUsernameNotSpecified = errors.New("No Username specified")
    49  
    50  // ErrIMAPPasswordNotSpecified is thrown when there is no Password specified
    51  // in the IMAP configuration
    52  var ErrIMAPPasswordNotSpecified = errors.New("No Password specified")
    53  
    54  // ErrInvalidIMAPFreq is thrown when the frequency for polling the
    55  // IMAP server is invalid
    56  var ErrInvalidIMAPFreq = errors.New("Invalid polling frequency.")
    57  
    58  // TableName specifies the database tablename for Gorm to use
    59  func (im IMAP) TableName() string {
    60  	return "imap"
    61  }
    62  
    63  // Validate ensures that IMAP configs/connections are valid
    64  func (im *IMAP) Validate() error {
    65  	switch {
    66  	case im.Host == "":
    67  		return ErrIMAPHostNotSpecified
    68  	case im.Port == 0:
    69  		return ErrIMAPPortNotSpecified
    70  	case im.Username == "":
    71  		return ErrIMAPUsernameNotSpecified
    72  	case im.Password == "":
    73  		return ErrIMAPPasswordNotSpecified
    74  	}
    75  
    76  	// Set the default value for Folder
    77  	if im.Folder == "" {
    78  		im.Folder = DefaultIMAPFolder
    79  	}
    80  
    81  	// Make sure im.Host is an IP or hostname. NB will fail if unable to resolve the hostname.
    82  	ip := net.ParseIP(im.Host)
    83  	_, err := net.LookupHost(im.Host)
    84  	if ip == nil && err != nil {
    85  		return ErrInvalidIMAPHost
    86  	}
    87  
    88  	// Make sure 1 >= port <= 65535
    89  	if im.Port < 1 || im.Port > 65535 {
    90  		return ErrInvalidIMAPPort
    91  	}
    92  
    93  	// Make sure the polling frequency is between every 30 seconds and every year
    94  	// If not set it to the default
    95  	if im.IMAPFreq < 30 || im.IMAPFreq > 31540000 {
    96  		im.IMAPFreq = DefaultIMAPFreq
    97  	}
    98  
    99  	return nil
   100  }
   101  
   102  // GetIMAP returns the IMAP server owned by the given user.
   103  func GetIMAP(uid int64) ([]IMAP, error) {
   104  	im := []IMAP{}
   105  	count := 0
   106  	err := db.Where("user_id=?", uid).Find(&im).Count(&count).Error
   107  
   108  	if err != nil {
   109  		log.Error(err)
   110  		return im, err
   111  	}
   112  	return im, nil
   113  }
   114  
   115  // PostIMAP updates IMAP settings for a user in the database.
   116  func PostIMAP(im *IMAP, uid int64) error {
   117  	err := im.Validate()
   118  	if err != nil {
   119  		log.Error(err)
   120  		return err
   121  	}
   122  
   123  	// Delete old entry. TODO: Save settings and if fails to Save below replace with original
   124  	err = DeleteIMAP(uid)
   125  	if err != nil {
   126  		log.Error(err)
   127  		return err
   128  	}
   129  
   130  	// Insert new settings into the DB
   131  	err = db.Save(im).Error
   132  	if err != nil {
   133  		log.Error("Unable to save to database: ", err.Error())
   134  	}
   135  	return err
   136  }
   137  
   138  // DeleteIMAP deletes the existing IMAP in the database.
   139  func DeleteIMAP(uid int64) error {
   140  	err := db.Where("user_id=?", uid).Delete(&IMAP{}).Error
   141  	if err != nil {
   142  		log.Error(err)
   143  	}
   144  	return err
   145  }
   146  
   147  func SuccessfulLogin(im *IMAP) error {
   148  	err := db.Model(&im).Where("user_id = ?", im.UserId).Update("last_login", time.Now().UTC()).Error
   149  	if err != nil {
   150  		log.Error("Unable to update database: ", err.Error())
   151  	}
   152  	return err
   153  }