Compare commits
21 Commits
customLogg
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 88a3f1f3a5 | |||
|
|
ec6c4f3113 | ||
|
|
c60841ef06 | ||
|
|
01964e5391 | ||
|
|
0c9e549560 | ||
|
|
d6bf69609f | ||
|
|
6a57e8c3fc | ||
|
|
39a4baf02e | ||
|
|
f724fd7bcb | ||
|
|
c99ec2226f | ||
|
|
00d59ae5ab | ||
|
|
dc84b56af1 | ||
|
|
64fb9af89b | ||
|
|
385e5106c2 | ||
|
|
882ca25f76 | ||
|
|
8746179952 | ||
|
|
4dc71bdbbc | ||
|
|
a6870a91fe | ||
|
|
24ed8dcbc3 | ||
|
|
c0f4b47c5e | ||
|
|
13b82e7dd5 |
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# Compiled test programs
|
||||||
|
samplefilter
|
||||||
|
samplefilter2
|
||||||
95
README.md
95
README.md
@@ -1,2 +1,95 @@
|
|||||||
# gomilter
|
# gomilter
|
||||||
Go Bindings for libmilter
|
Go Bindings for Sendmail's libmilter
|
||||||
|
|
||||||
|
Tested on Linux and FreeBSD
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
The Sendmail header file libmilter/mfapi.h is required. For Redhat/CentOS, install the sendmail-devel package:
|
||||||
|
```sh
|
||||||
|
yum install sendmail-devel
|
||||||
|
```
|
||||||
|
|
||||||
|
Install the gomilter package:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
go get github.com/leonrbaker/gomilter
|
||||||
|
```
|
||||||
|
|
||||||
|
##Usage
|
||||||
|
|
||||||
|
The milter is implemented in a struct. Start by defining your own struct type and embeding the gomilter MilterRaw struct.
|
||||||
|
|
||||||
|
```go
|
||||||
|
type Mymilter struct {
|
||||||
|
gomilter.MilterRaw // Embed the basic functionality.
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Milter callbacks are added by implementing methods for the struct with matching predefined names.
|
||||||
|
|
||||||
|
### Callbacks
|
||||||
|
|
||||||
|
* Connect
|
||||||
|
* Helo
|
||||||
|
* EnvFrom
|
||||||
|
* EnvRcpt
|
||||||
|
* Header
|
||||||
|
* Eoh
|
||||||
|
* Body
|
||||||
|
* Eom
|
||||||
|
* Abort
|
||||||
|
* Close
|
||||||
|
|
||||||
|
Not all the callbacks need to be defined. The callbacks are explained on the milter.org site. Unfortunately the milter.org site has been shut down but it is still on [web.archive.org](http://web.archive.org/web/20150510034154/https://www.milter.org/developers/api/index)
|
||||||
|
|
||||||
|
### Message Modification Functions
|
||||||
|
|
||||||
|
* AddHeader
|
||||||
|
* ChgHeader
|
||||||
|
* InsHeader
|
||||||
|
* ChgFrom
|
||||||
|
* AddRcpt
|
||||||
|
* AddRcpt_Par
|
||||||
|
* DelRcpt
|
||||||
|
* ReplaceBody
|
||||||
|
|
||||||
|
### Other Message Handling Functions
|
||||||
|
* progress
|
||||||
|
|
||||||
|
### Startup
|
||||||
|
|
||||||
|
The Socket field of the milter struct must be set. For example:
|
||||||
|
```go
|
||||||
|
mymilter.Socket = "unix:/var/gomilter/socket"
|
||||||
|
```
|
||||||
|
|
||||||
|
Control is handed over to the libmilter smfi_main function by calling the Run method and passing it a pointer to your milter struct
|
||||||
|
```go
|
||||||
|
gomilter.Run(mymilter)
|
||||||
|
```
|
||||||
|
|
||||||
|
The milter has a Stop method which calls the libmilter smfi_stop function.
|
||||||
|
|
||||||
|
### Private Data
|
||||||
|
|
||||||
|
libmilter is able to store private data for a connection. This data can be accessed from other functions and callbacks for the same connection. You can pass a pointer to any data structure to SetPriv. The data is retrieved with GetPriv
|
||||||
|
```go
|
||||||
|
t := T{1, 2, 3}
|
||||||
|
m.SetPriv(ctx, &t)
|
||||||
|
```
|
||||||
|
Retrieve the data with
|
||||||
|
```go
|
||||||
|
var t T
|
||||||
|
m.GetPriv(ctx, &t))
|
||||||
|
```
|
||||||
|
|
||||||
|
GetPriv should only be called once. If the private data is needed in another function or callback then call SetPriv again.
|
||||||
|
|
||||||
|
## Sample Programs
|
||||||
|
There are two sample programs included, samplefilter.go and samplefilter2.go
|
||||||
|
|
||||||
|
##Other Libraries
|
||||||
|
|
||||||
|
A usefull MIME parsing library is [go.enmime](https://github.com/jhillyerd/go.enmime)
|
||||||
|
|
||||||
|
|||||||
1080
gomilter.go
1080
gomilter.go
File diff suppressed because it is too large
Load Diff
@@ -15,36 +15,34 @@ Sample Milter
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"."
|
||||||
"."
|
"fmt"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type Mymilter struct {
|
type Mymilter struct {
|
||||||
gomilter.MilterRaw // Embed the basic functionality. No callbacks yet
|
gomilter.MilterRaw // Embed the basic functionality. No callbacks yet
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define the callback functions we are going to use
|
// Define the callback functions we are going to use
|
||||||
func (m *Mymilter) Connect(ctx uintptr, hostname, ip string) (sfsistat int8) {
|
func (m *Mymilter) Connect(ctx uintptr, hostname, ip string) (sfsistat int8) {
|
||||||
fmt.Println("mymilter.connect was called")
|
fmt.Println("mymilter.connect was called")
|
||||||
fmt.Printf("hostname: %s\n", hostname)
|
fmt.Printf("hostname: %s\n", hostname)
|
||||||
fmt.Printf("ip: %s\n", ip)
|
fmt.Printf("ip: %s\n", ip)
|
||||||
return gomilter.Reject
|
return gomilter.Reject
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Mymilter) Helo(ctx uintptr, helohost string) (sfsistat int8) {
|
func (m *Mymilter) Helo(ctx uintptr, helohost string) (sfsistat int8) {
|
||||||
fmt.Println("mymilter.helo was called")
|
fmt.Println("mymilter.helo was called")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
mymilter := new(Mymilter)
|
mymilter := new(Mymilter)
|
||||||
mymilter.FilterName = "TestFilter"
|
mymilter.FilterName = "TestFilter"
|
||||||
mymilter.Debug = true
|
mymilter.Debug = true
|
||||||
mymilter.Flags = gomilter.AddHdrs|gomilter.AddRcpt
|
mymilter.Flags = gomilter.ADDHDRS | gomilter.ADDRCPT
|
||||||
|
mymilter.Socket = "unix:/var/milterattachcheck/socket"
|
||||||
|
|
||||||
// Start Milter
|
// Start Milter
|
||||||
gomilter.Run(mymilter)
|
gomilter.Run(mymilter)
|
||||||
}
|
}
|
||||||
|
|||||||
121
samplefilter2.go
121
samplefilter2.go
@@ -7,109 +7,114 @@ Copyright (c) 2015 Leon Baker
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
m "github.com/leonrbaker/gomilter"
|
||||||
m "github.com/leonrbaker/gomilter"
|
"log"
|
||||||
"fmt"
|
"os"
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Mymilter struct {
|
type Mymilter struct {
|
||||||
m.MilterRaw // Embed the basic functionality. No callbacks defined yet
|
m.MilterRaw // Embed the basic functionality. No callbacks defined yet
|
||||||
}
|
}
|
||||||
|
|
||||||
// Data type I want use for private data. Can be any type
|
// Data type I want use for private data. Can be any type
|
||||||
type T struct {
|
type T struct {
|
||||||
A uint8
|
A uint8
|
||||||
B string
|
B string
|
||||||
C string
|
C string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var logger *log.Logger
|
||||||
|
|
||||||
// Define the callback functions we are going to use
|
// Define the callback functions we are going to use
|
||||||
func (milter *Mymilter) Connect(ctx uintptr, hostname, ip string) (sfsistat int8) {
|
func (milter *Mymilter) Connect(ctx uintptr, hostname, ip string) (sfsistat int8) {
|
||||||
fmt.Println("mymilter.Connect was called")
|
logger.Println("mymilter.Connect was called")
|
||||||
fmt.Printf("hostname: %s\n", hostname)
|
logger.Printf("hostname: %s\n", hostname)
|
||||||
fmt.Printf("ip: %s\n", ip)
|
logger.Printf("ip: %s\n", ip)
|
||||||
|
|
||||||
t := T{1, hostname, "Test"}
|
t := T{1, hostname, "Test"}
|
||||||
m.SetPriv(ctx, &t)
|
m.SetPriv(ctx, &t)
|
||||||
|
|
||||||
return m.Continue
|
return m.Continue
|
||||||
}
|
}
|
||||||
|
|
||||||
func (milter *Mymilter) Helo(ctx uintptr, helohost string) (sfsistat int8) {
|
func (milter *Mymilter) Helo(ctx uintptr, helohost string) (sfsistat int8) {
|
||||||
fmt.Println("mymilter.Helo was called")
|
logger.Println("mymilter.Helo was called")
|
||||||
fmt.Printf("helohost: %s\n", helohost)
|
logger.Printf("helohost: %s\n", helohost)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (milter *Mymilter) EnvFrom(ctx uintptr, myargv []string) (sfsistat int8) {
|
func (milter *Mymilter) EnvFrom(ctx uintptr, myargv []string) (sfsistat int8) {
|
||||||
fmt.Println("mymilter.EnvFrom was called")
|
logger.Println("mymilter.EnvFrom was called")
|
||||||
fmt.Printf("myargv: %s\n", myargv)
|
logger.Printf("myargv: %s\n", myargv)
|
||||||
// Show the value of a symbol
|
// Show the value of a symbol
|
||||||
fmt.Printf("{mail_addr}: %v\n", m.GetSymVal(ctx, "{mail_addr}"))
|
logger.Printf("{mail_addr}: %v\n", m.GetSymVal(ctx, "{mail_addr}"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (milter *Mymilter) EnvRcpt(ctx uintptr, myargv []string) (sfsistat int8) {
|
func (milter *Mymilter) EnvRcpt(ctx uintptr, myargv []string) (sfsistat int8) {
|
||||||
fmt.Println("mymilter.EnvRcpt was called")
|
logger.Println("mymilter.EnvRcpt was called")
|
||||||
fmt.Printf("myargv: %s\n", myargv)
|
logger.Printf("myargv: %s\n", myargv)
|
||||||
// Show the value of a symbol
|
// Show the value of a symbol
|
||||||
fmt.Printf("{rcpt_addr}: %v\n", m.GetSymVal(ctx, "{rcpt_addr}"))
|
logger.Printf("{rcpt_addr}: %v\n", m.GetSymVal(ctx, "{rcpt_addr}"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (milter *Mymilter) Header(ctx uintptr, headerf, headerv string) (sfsistat int8) {
|
func (milter *Mymilter) Header(ctx uintptr, headerf, headerv string) (sfsistat int8) {
|
||||||
fmt.Println("mymilter.Header was called")
|
logger.Println("mymilter.Header was called")
|
||||||
fmt.Printf("header field: %s\n", headerf)
|
logger.Printf("header field: %s\n", headerf)
|
||||||
fmt.Printf("header value: %s\n", headerv)
|
logger.Printf("header value: %s\n", headerv)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (milter *Mymilter) Eoh(ctx uintptr) (sfsistat int8) {
|
func (milter *Mymilter) Eoh(ctx uintptr) (sfsistat int8) {
|
||||||
fmt.Println("mymilter.Eoh was called")
|
logger.Println("mymilter.Eoh was called")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (milter *Mymilter) Body(ctx uintptr, body []byte) (sfsistat int8) {
|
func (milter *Mymilter) Body(ctx uintptr, body []byte) (sfsistat int8) {
|
||||||
// Be careful as a conversion of body to string will make a copy of body
|
// Be careful as a conversion of body to string will make a copy of body
|
||||||
fmt.Println("mymilter.Body was called")
|
logger.Println("mymilter.Body was called")
|
||||||
fmt.Println(string(body))
|
logger.Println(string(body))
|
||||||
fmt.Printf("Body Length: %d\n", len(body))
|
logger.Printf("Body Length: %d\n", len(body))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (milter *Mymilter) Eom(ctx uintptr) (sfsistat int8) {
|
func (milter *Mymilter) Eom(ctx uintptr) (sfsistat int8) {
|
||||||
fmt.Println("mymilter.Eom was called")
|
logger.Println("mymilter.Eom was called")
|
||||||
|
|
||||||
var t T
|
var t T
|
||||||
fmt.Println("m.GetPri:", m.GetPriv(ctx, &t))
|
logger.Println("m.GetPri:", m.GetPriv(ctx, &t))
|
||||||
fmt.Println("t:", t)
|
logger.Println("t:", t)
|
||||||
|
|
||||||
m.AddHeader(ctx, "LEONUX-Mailer",
|
m.AddHeader(ctx, "LEONUX-Mailer",
|
||||||
"test server;\n\ttest1=\"foobar\"")
|
"test server;\n\ttest1=\"foobar\"")
|
||||||
|
|
||||||
newBody := []byte("This is a new body")
|
newBody := []byte("This is a new body")
|
||||||
m.ReplaceBody(ctx, newBody)
|
m.ReplaceBody(ctx, newBody)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (milter *Mymilter) Abort(ctx uintptr) (sfsistat int8) {
|
func (milter *Mymilter) Abort(ctx uintptr) (sfsistat int8) {
|
||||||
fmt.Println("mymilter.Abort was called")
|
logger.Println("mymilter.Abort was called")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (milter *Mymilter) Close(ctx uintptr) (sfsistat int8) {
|
func (milter *Mymilter) Close(ctx uintptr) (sfsistat int8) {
|
||||||
fmt.Println("mymilter.Close was called")
|
logger.Println("mymilter.Close was called")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
mymilter := new(Mymilter)
|
mymilter := new(Mymilter)
|
||||||
mymilter.FilterName = "TestFilter"
|
mymilter.FilterName = "TestFilter"
|
||||||
mymilter.Debug = true
|
|
||||||
mymilter.Flags = m.ADDHDRS|m.ADDRCPT|m.CHGFROM|m.CHGBODY
|
|
||||||
mymilter.Socket = "unix:/var/milterattachcheck/socket"
|
|
||||||
|
|
||||||
// Start Milter
|
logger = log.New(os.Stdout, "", log.LstdFlags)
|
||||||
m.Run(mymilter)
|
mymilter.Logger = logger
|
||||||
|
mymilter.Debug = true
|
||||||
|
|
||||||
|
mymilter.Flags = m.ADDHDRS | m.ADDRCPT | m.CHGFROM | m.CHGBODY
|
||||||
|
mymilter.Socket = "unix:/var/milterattachcheck/socket"
|
||||||
|
|
||||||
|
// Start Milter
|
||||||
|
m.Run(mymilter)
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user