-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
153 lines (120 loc) · 4.43 KB
/
main.go
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
package main
// ocfw - open connect server firewall script
// Copyright (C) 2016 Maximilian Pachl
// 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 3 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, see <http://www.gnu.org/licenses/>.
// ----------------------------------------------------------------------------------
// imports
// ----------------------------------------------------------------------------------
import (
"fmt"
"log"
"os"
"strings"
"github.com/faryon93/ocfw/ocenv"
"github.com/faryon93/ocfw/iptables"
"github.com/faryon93/ocfw/config"
"github.com/alexflint/go-filemutex"
)
// ----------------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------------
const (
CONFIG_FILE = "/etc/ocserv/ocfw.conf"
)
// ----------------------------------------------------------------------------------
// application entry
// ----------------------------------------------------------------------------------
func main() {
retval := 0
defer os.Exit(retval)
// load the config file
conf, err := config.Load(CONFIG_FILE)
if err != nil {
fmt.Println("failed to load configuration file:", err.Error())
retval = -1
return
}
// aquire the lockfile
mutex, err := filemutex.New(conf.Common.LockFile)
if err != nil {
log.Println("failed to aquire lock file:", err.Error())
retval = -1
return
}
mutex.Lock()
defer mutex.Unlock()
// norma ocserver connection handler
if len(os.Args) < 2 {
retval = connection(conf)
// firewall setup mode
} else {
retval = setup(conf)
}
}
// ----------------------------------------------------------------------------------
// application modi
// ----------------------------------------------------------------------------------
func connection(conf *config.Config) (int) {
// print some info aber the request
log.Println("user", ocenv.Username, ocenv.Reason, "from", ocenv.RealIp)
// a connect job
if ocenv.IsConnect() {
return connect(conf)
// this is a disconnect call
} else if (ocenv.IsDisconnect()) {
return disconnect(conf)
}
// invalid REASON was supplied
log.Println("failure: invalid openconnect reason:", "\"" + ocenv.Reason + "\"")
return -1
}
func setup(conf *config.Config) (int) {
log.Println("initializing iptables firewall")
// flush the forward chain
err := iptables.FlushChain("FORWARD")
if err != nil {
log.Println("failed to flush the FORWARD chain:", err.Error())
return -1
}
// allow established and related connections
err = iptables.Chain("FORWARD").Append().State("ESTABLISHED", "RELATED").Accept().Apply()
if err != nil {
log.Println("failed to allow ESTABLISHED and RELATED connections:", err.Error())
return -1
}
// drop anything else
err = iptables.Chain("FORWARD").Append().Drop().Apply()
if err != nil {
log.Println("failed to drop anything else:", err.Error())
return -1
}
// setup all groups as individual chains
for name, group := range conf.Groups {
// some metadata
groupChain := "VPN_GROUP_" + strings.ToUpper(name)
// create the new group specific chains
err = iptables.NewChain(groupChain)
if err != nil {
log.Println("failed to create group chain", groupChain + ":", err.Error())
return -1
}
// add all allows to the group specific chains
for _, destination := range group.Allow {
err = iptables.Chain(groupChain).Append().Destination(destination).Accept().Apply()
if err != nil {
log.Println("failed to add allowed destination", destination + ":", err.Error())
}
}
log.Println("created chain", groupChain)
}
return 0
}