File: analyze.go

package info (click to toggle)
dmarc-cat 0.9.2-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 164 kB
  • sloc: xml: 104; makefile: 27
file content (182 lines) | stat: -rw-r--r-- 3,889 bytes parent folder | download
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
172
173
174
175
176
177
178
179
180
181
182
package main

import (
	"bytes"
	"fmt"
	"text/template"
	"time"

	"github.com/intel/tfortools"
	"github.com/ivpusic/grpool"
	"github.com/pkg/errors"
)

const (
	reportTmpl = `{{.MyName}} {{.MyVersion}}/j{{.Jobs}} by {{.Author}}

Reporting by: {{.Org}} — {{.Email}}
From {{.DateBegin}} to {{.DateEnd}}

Domain: {{.Domain}}
Policy: p={{.Disposition}}; dkim={{.DKIM}}; spf={{.SPF}}

Reports({{.Count}}):
`

	rowTmpl = `{{ table (sort . %s)}}`
)

// My template vars
type headVars struct {
	MyName      string
	MyVersion   string
	Jobs        string
	Author      string
	Org         string
	Email       string
	DateBegin   string
	DateEnd     string
	Domain      string
	Disposition string
	DKIM        string
	SPF         string
	Pct         int
	Count       int
}

// Entry representes a single entry
type Entry struct {
	IP    string
	Count int
	From  string
	RFrom string
	RDKIM string
	RSPF  string
}

// ResolveIP returns the first name associated with the IP
func ResolveIP(ctx *Context, ip string) string {
	ips, err := ctx.r.LookupAddr(ip)
	if err != nil {
		return ip
	}
	// XXX FIXME?
	return ips[0]
}

type IP struct {
	IP   string
	Name string
}

func ParallelSolve(ctx *Context, iplist []IP) []IP {
	resolved := make([]IP, len(iplist))
	pool := grpool.NewPool(ctx.jobs, len(iplist))
	verbose("ParallelSolve with %d workers", ctx.jobs)
	defer pool.Release()

	pool.WaitCount(len(iplist))
	for i, e := range iplist {
		current := e.IP
		ind := i
		pool.JobQueue <- func() {
			defer pool.JobDone()

			resolved[ind].Name = ResolveIP(ctx, current)
		}
	}
	pool.WaitAll()

	return resolved
}

// GatherRows extracts all IP and return the rows
func GatherRows(ctx *Context, r Feedback) []Entry {
	var (
		rows    []Entry
		iplist  []IP
		newlist []IP
	)

	ipslen := len(r.Records)

	if !fNoResolv {
		verbose("Resolving all %d IPs", ipslen)
		iplist = make([]IP, ipslen)
		// Get all IPs
		for i, report := range r.Records {
			iplist[i] = IP{IP: report.Row.SourceIP.String()}
		}

		// Now we have a nice array
		newlist = ParallelSolve(ctx, iplist)
		verbose("Resolved %d IPs", ipslen)
	} else {
		newlist = make([]IP, ipslen)
		// Get all IPs
		for i, report := range r.Records {
			newlist[i] = IP{Name: report.Row.SourceIP.String()}
		}
	}

	for i, report := range r.Records {
		ip0 := newlist[i].Name
		current := Entry{
			IP:    ip0,
			Count: report.Row.Count,
			From:  report.Identifiers.HeaderFrom,
			RSPF:  report.AuthResults.SPF.Result,
			RDKIM: report.AuthResults.DKIM.Result,
		}
		if report.AuthResults.DKIM.Domain == "" {
			current.RFrom = report.AuthResults.SPF.Domain
		} else {
			current.RFrom = report.AuthResults.DKIM.Domain
		}
		rows = append(rows, current)
	}
	return rows
}

// Analyze extract and display what we want
func Analyze(ctx *Context, r Feedback) (string, error) {
	var buf bytes.Buffer

	tmplvars := &headVars{
		MyName:      MyName,
		MyVersion:   MyVersion,
		Jobs:        fmt.Sprintf("%d", fJobs),
		Author:      Author,
		Org:         r.Metadata.OrgName,
		Email:       r.Metadata.Email,
		DateBegin:   time.Unix(r.Metadata.Date.Begin, 0).String(),
		DateEnd:     time.Unix(r.Metadata.Date.End, 0).String(),
		Domain:      r.Policy.Domain,
		Disposition: r.Policy.P,
		DKIM:        r.Policy.ADKIM,
		SPF:         r.Policy.ASPF,
		Pct:         r.Policy.Pct,
		Count:       len(r.Records),
	}

	rows := GatherRows(ctx, r)
	if len(rows) == 0 {
		return "", fmt.Errorf("empty report")
	}

	// Header
	t := template.Must(template.New("r").Parse(string(reportTmpl)))
	err := t.ExecuteTemplate(&buf, "r", tmplvars)
	if err != nil {
		return "", errors.Wrapf(err, "error in template 'r'")
	}

	// Generate our template
	sortTmpl := fmt.Sprintf(rowTmpl, fSort)
	err = tfortools.OutputToTemplate(&buf, "reports", sortTmpl, rows, nil)
	if err != nil {
		return "", errors.Wrapf(err, "error in template 'reports'")
	}

	return buf.String(), nil
}