forked from palark/ovpn-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router-cert-handler.go
217 lines (189 loc) · 6.13 KB
/
router-cert-handler.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"encoding/json"
"errors"
"log"
"net/http"
"rpiadm/backend/auth"
"rpiadm/backend/model"
"rpiadm/backend/openvpn"
"rpiadm/backend/rpi"
)
func (app *OvpnAdmin) userCreateHandler(w http.ResponseWriter, r *http.Request) {
if !auth.HasWriteRole(app.applicationPreferences.JwtData, r) {
returnErrorMessage(w, http.StatusUnauthorized, errors.New("user not authorized to create certificate"))
return
}
var userDefinition openvpn.UserDefinition
err := json.NewDecoder(r.Body).Decode(&userDefinition)
if err != nil {
returnErrorMessage(w, http.StatusUnprocessableEntity, errors.New("cant parse JSON in body"))
return
}
if openvpn.ExistCcdCommonName(app.serverConf, userDefinition.CommonName) {
returnErrorMessage(w, http.StatusPreconditionFailed, errors.New("conflicting existing files"))
return
}
var newCcd *openvpn.Ccd
if userDefinition.Ccd != nil {
newCcd, err = app.applyCcd(userDefinition.CommonName, *userDefinition.Ccd)
if err != nil {
returnErrorMessage(w, http.StatusUnprocessableEntity, errors.New("cant setup CCD"))
return
}
}
log.Printf("create user with %v\n", userDefinition)
if typeCa := r.URL.Query().Get("type"); typeCa == "ca" {
if app.easyrsa.CaCertExists() {
returnErrorMessage(w, http.StatusUnprocessableEntity, errors.New("ca already exists"))
return
}
certificate, err := openvpn.CreateCaCertificate(app.easyrsa, *authByPassword, *authDatabase, userDefinition)
if err != nil {
//app.removeCcd(userDefinition.CommonName)
returnErrorMessage(w, http.StatusUnprocessableEntity, err)
return
}
log.Printf("certificate: %s", certificate)
//if err = openvpn.BuildCa(app.easyrsa); err != nil {
// returnErrorMessage(w, http.StatusUnprocessableEntity, errors.New("Can't init PKI"))
// return
//}
} else if typeCa == "server" {
certificate, err := app.easyrsa.CreateServerCertificate(userDefinition)
if err != nil {
//app.removeCcd(userDefinition.CommonName)
returnErrorMessage(w, http.StatusUnprocessableEntity, err)
return
}
log.Printf("certificate: %s", certificate)
server := &model.Device{
Username: userDefinition.CommonName,
ConnectionStatus: "",
Certificate: certificate,
RpiState: nil,
Connections: make([]*openvpn.VpnConnection, 0),
Rpic: make([]*rpi.RpiConnection, 0),
Ccd: newCcd,
}
app.easyrsa.RebuildClientRevocationList()
//app.serverConf.MasterCn = userDefinition.CommonName
err = returnJson(w, server)
} else {
certificate, err := app.easyrsa.CreateClientCertificate(userDefinition)
if err != nil {
app.removeCcd(userDefinition.CommonName)
returnErrorMessage(w, http.StatusUnprocessableEntity, err)
return
}
user := &model.Device{
Username: userDefinition.CommonName,
ConnectionStatus: "",
Certificate: certificate,
RpiState: nil,
Connections: make([]*openvpn.VpnConnection, 0),
Rpic: make([]*rpi.RpiConnection, 0),
Ccd: newCcd,
}
app.clients = append(app.clients, user)
log.Printf("created user %v over %d clients\n", user, len(app.clients))
err = returnJson(w, user)
if err != nil {
log.Printf("error sending response")
}
}
}
func (app *OvpnAdmin) userRevokeHandler(w http.ResponseWriter, r *http.Request, commonNameSerialNumber string) {
if !auth.HasReadRole(app.applicationPreferences.JwtData, r) {
w.WriteHeader(http.StatusForbidden)
return
}
err := app.userRevoke(commonNameSerialNumber)
//fmt.Fprintf(w, "%s", ret)
if err != nil {
returnErrorMessage(w, http.StatusUnprocessableEntity, err)
return
}
w.WriteHeader(http.StatusNoContent)
}
func (app *OvpnAdmin) userUnrevokeHandler(w http.ResponseWriter, r *http.Request, commonNameSerialNumber string) {
//log.Printf("%s %s", r.RemoteAddr, r.RequestURI)
if !auth.HasReadRole(app.applicationPreferences.JwtData, r) {
w.WriteHeader(http.StatusForbidden)
return
}
err := app.userUnrevoke(commonNameSerialNumber)
if err != nil {
log.Printf("unrevoke error %s", err.Error())
returnErrorMessage(w, http.StatusUnprocessableEntity, err)
return
}
w.WriteHeader(http.StatusNoContent)
}
func (app *OvpnAdmin) userDeleteHandler(w http.ResponseWriter, r *http.Request, username string) {
if !auth.HasReadRole(app.applicationPreferences.JwtData, r) {
w.WriteHeader(http.StatusForbidden)
return
}
err := app.userDelete(username)
if err != nil {
returnErrorMessage(w, http.StatusInternalServerError, err)
return
}
w.WriteHeader(http.StatusNoContent)
}
type PasswordPayload struct {
Password string `json:"password"`
}
func (app *OvpnAdmin) userRotateHandler(w http.ResponseWriter, r *http.Request, username string) {
//log.Printf("%s %s", r.RemoteAddr, r.RequestURI)
if enableCors(&w, r) {
return
}
if !auth.HasWriteRole(app.applicationPreferences.JwtData, r) {
w.WriteHeader(http.StatusForbidden)
return
}
var userDefinition openvpn.UserDefinition
err := json.NewDecoder(r.Body).Decode(&userDefinition)
if err != nil {
log.Printf(err.Error())
returnErrorMessage(w, http.StatusInternalServerError, err)
return
}
cert, err := app.userRotate(username, &userDefinition)
if err != nil {
returnErrorMessage(w, http.StatusUnprocessableEntity, err)
return
}
//fmt.Sprintf(`{"message":"User %s successfully rotated"}`, username)
//w.WriteHeader(http.StatusNoContent)
returnJson(w, cert)
}
func (app *OvpnAdmin) userChangePasswordHandler(w http.ResponseWriter, r *http.Request, username string) {
//log.Info(r.RemoteAddr, " ", r.RequestURI)
if enableCors(&w, r) {
return
}
if !auth.HasReadRole(app.applicationPreferences.JwtData, r) {
w.WriteHeader(http.StatusForbidden)
return
}
if !*authByPassword {
returnErrorMessage(w, http.StatusNotImplemented, errors.New("not implemented"))
return
}
var passwordPayload PasswordPayload
err := json.NewDecoder(r.Body).Decode(&passwordPayload)
if err != nil {
log.Printf(err.Error())
returnErrorMessage(w, http.StatusInternalServerError, err)
return
}
err = app.userChangePassword(username, passwordPayload.Password)
if err != nil {
returnErrorMessage(w, http.StatusUnprocessableEntity, err)
return
}
w.WriteHeader(http.StatusNoContent)
}