-
Notifications
You must be signed in to change notification settings - Fork 4
/
ui.go
534 lines (492 loc) · 14.2 KB
/
ui.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
package webca
import (
"fmt"
"html/template"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
)
const (
PORT = 443
PORTFIX = 8000
REQUEST = "Request"
LOGGEDUSER = "LoggedUser"
)
// address is a complex bind address
type address struct {
addr, certfile, keyfile string
tls bool
}
// fakedLogin for development environments
var fakedLogin bool
// portFix contains the port correction when low ports are not permited
var portFix int
// listenAndServe starts the server with or without TLS on the address
func (a address) listenAndServe(smux *http.ServeMux) error {
if a.tls {
return http.ListenAndServeTLS(a.addr, a.certfile, a.keyfile, smux)
}
return http.ListenAndServe(a.addr, smux)
}
// String prints this address properly
func (a address) String() string {
prefix := "http"
if a.tls {
prefix = "https"
}
return prefix + "://" + a.addr
}
// templates contains all web templates
var templates *template.Template
// defaultHandler points to the handler for '/' requests
var defaultHandler func(w http.ResponseWriter, r *http.Request)
// PageStatus contains all values that a page and its templates need
// (including the SetupWizard when the setup is running)
// U User
// SetupWizard contains the status of the setup wizard and may be included in the PageStatus Map
// CA CertSetup
// Cert CertSetup
// M Mailer
type PageStatus map[string]interface{}
// init prepares all web templates before anything else
func init() {
templates = template.New("webcaTemplates")
templates.Funcs(template.FuncMap{
// The name "title" is what the function will be called in the template text.
"tr": tr, "indexOf": indexOf, "showPeriod": showPeriod, "qEsc": qEsc,
})
template.Must(templates.Parse(htmlTemplates))
template.Must(templates.Parse(jsTemplates))
template.Must(templates.Parse(pages))
template.Must(templates.ParseFiles("style.css"))
}
// LoadCrt loads variables "Prfx" and "Crt" into PageSetup to point to the right
// CertSetup and its prefix and sets a default duration for that cert
func (ps PageStatus) LoadCrt(arg interface{}, prfx string, defaultDuration int) string {
var cs *CertSetup
if arg != nil {
cs = arg.(*CertSetup)
} else {
cs = &CertSetup{}
}
ps["Crt"] = cs
ps["Prfx"] = prfx
cs.Duration = defaultDuration
return ""
}
// IsDuration returns whether or not the given duration is the selected one on the loaded Crt
func (ps PageStatus) IsSelected(duration int) bool {
crt := ps["Crt"]
if crt == nil {
return false
}
cs := crt.(*CertSetup)
return cs.Duration == duration
}
// tr is the app translation function
func tr(s string, args ...interface{}) string {
if args == nil || len(args) == 0 {
return s
}
return fmt.Sprintf(s, args...)
}
// indexOf allows to access strings on a string array
func indexOf(sa []string, index int) string {
if sa == nil || len(sa) < (index+1) {
return ""
}
return sa[index]
}
// qEsc escapes a query string to be laced in the URL
func qEsc(s string, args ...interface{}) string {
return url.QueryEscape(fmt.Sprintf(s, args...))
}
// WebCA starts the prepares and serves the WebApp
func WebCA() {
smux := http.DefaultServeMux
addr := PrepareServer(smux)
err := addr.listenAndServe(smux)
if portFix == 0 { // port Fixing is only applied once
if err != nil {
log.Printf("Could not start server on address %v!: %s\n", addr, err)
}
portFix = PORTFIX
addr = fixAddress(addr)
log.Printf("(Warning) Failed to listen on standard port, go to %v\n", addr)
err = addr.listenAndServe(smux)
}
if err != nil {
log.Fatalf("Could not start!: %s", err)
}
}
// webCA will start the WebApp once it has been configured properly on a NEW http.ServeMux
func webCA() {
smux := http.NewServeMux()
addr := PrepareServer(smux)
log.Printf("Go to %v\n", addr)
err := addr.listenAndServe(smux)
if err != nil {
log.Fatalf("Could not start!: %s", err)
}
}
// fixAddress returns a repaired alternate address by portFix
func fixAddress(a address) address {
port := 80
if strings.Contains(a.addr, ":") {
parts := strings.Split(a.addr, ":")
a.addr = parts[0]
var err error
port, err = strconv.Atoi(parts[1])
if err != nil {
port = 80
}
}
a.addr = fmt.Sprintf("%s:%v", a.addr, port+portFix)
return a
}
// prepareServer prepares the Web handlers for the setup wizard if there is no HTTPS config or
// the normal app if the app is already configured
func PrepareServer(smux *http.ServeMux) address {
// load config...
cfg := LoadConfig()
if cfg == nil { // if config is empty then run the setup
return PrepareSetup(smux) // always on the default serve mux
}
// otherwise start the normal app
log.Printf("Starting WebCA normal startup...")
smux.Handle("/", accessControl(index))
smux.HandleFunc("/login", login)
smux.Handle("/img/", http.StripPrefix("/img/", http.FileServer(http.Dir("img"))))
smux.Handle("/favicon.ico", http.FileServer(http.Dir("img")))
smux.Handle("/cert", accessControl(cert))
smux.Handle("/gen", accessControl(gen))
smux.Handle("/certControl", accessControl(certControl))
smux.Handle("/cert/", authCertServer("/cert/", http.Dir(".")))
smux.Handle("/renew", accessControl(renew))
smux.Handle("/clone", accessControl(clone))
smux.Handle("/del", accessControl(del))
return address{webCAURL(cfg), certFile(cfg.getWebCert()), keyFile(cfg.getWebCert()), true}
}
// webCAURL returns the WebCA URL
func webCAURL(cfg *config) string {
certName := cfg.getWebCert().Crt.Subject.CommonName
return fmt.Sprintf("%s:%v", certName, PORT+portFix)
}
// authCertServer returns a authorized certServer for downloading certificates
func authCertServer(prefix string, dir http.Dir) http.Handler {
return accessControlHandler(http.StripPrefix(prefix, certServer(dir)))
}
// certServer returns a certificate server filtering the downloadable cert files properly
func certServer(dir http.Dir) http.Handler {
h := http.FileServer(dir)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, ".key.pem") || !strings.HasSuffix(r.URL.Path, ".pem") {
http.NotFound(w, r)
return
}
w.Header().Set("Content-disposition", "attachment; filename="+r.URL.Path)
w.Header().Set("Content-type", "application/x-pem-file")
h.ServeHTTP(w, r)
})
}
// readUser reads the user data from the request
func readUser(r *http.Request) User {
u := User{}
u.Username = r.FormValue("Username")
u.Fullname = r.FormValue("Fullname")
u.Email = r.FormValue("Email")
u.Password = r.FormValue("Password")
return u
}
// readCertSetup reads the certificate setup from the request
func readCertSetup(prefix string, r *http.Request) (*CertSetup, error) {
cs := CertSetup{}
prepareName(&cs.Name)
cs.Name.CommonName = r.FormValue(prefix + ".CommonName")
cs.Name.StreetAddress[0] = r.FormValue(prefix + ".StreetAddress")
cs.Name.PostalCode[0] = r.FormValue(prefix + ".PostalCode")
cs.Name.Locality[0] = r.FormValue(prefix + ".Locality")
cs.Name.Province[0] = r.FormValue(prefix + ".Province")
cs.Name.OrganizationalUnit[0] = r.FormValue(prefix + ".OrganizationalUnit")
cs.Name.Organization[0] = r.FormValue(prefix + ".Organization")
cs.Name.Country[0] = r.FormValue(prefix + ".Country")
duration, err := strconv.Atoi(r.FormValue(prefix + ".Duration"))
if err != nil || duration < 0 {
return nil, fmt.Errorf("%s: %v", tr("Wrong duration!"), err)
}
cs.Duration = duration
return &cs, nil
}
// readMailer reads the mailer config from the request
func readMailer(r *http.Request) Mailer {
m := Mailer{}
m.User = r.FormValue("M.User")
m.Server = r.FormValue("M.Server")
port := r.FormValue("M.Port")
if port != "" {
m.Server += ":" + port
}
m.Passwd = r.FormValue("M.Password")
return m
}
// index displays the index page
func index(w http.ResponseWriter, r *http.Request) {
ps := newLoggedPage(w, r)
if ps == nil {
return
}
ct := ListCerts()
ps["CAs"] = ct.roots
ps["Others"] = ct.foreign
err := templates.ExecuteTemplate(w, "index", ps)
handleError(w, r, err)
}
// setCertPageTexts sets cert's page texts for CA or Certs
func setCertPageTexts(ps PageStatus, parent string) {
if parent != "" {
ps["Title"] = tr("New Certificate at %s", parent)
ps["CommonName"] = tr("Certificate Name")
ps["Action"] = tr("Generate Certificate")
} else {
ps["Title"] = tr("New CA")
ps["CommonName"] = tr("CA Name")
ps["Action"] = tr("Generate CA")
}
}
// cert allows the web user to generate a new certificate
func cert(w http.ResponseWriter, r *http.Request) {
ps := newLoggedPage(w, r)
if ps == nil {
return
}
parent := r.FormValue("parent")
if parent != "" {
pc, err := FindCertOrFail(parent)
if handleError(w, r, err) {
return
}
pc.Crt.Subject.CommonName = ""
ps["parent"] = parent
ps["Cert"] = &CertSetup{Name: pc.Crt.Subject}
}
setCertPageTexts(ps, parent)
err := templates.ExecuteTemplate(w, "cert", ps)
handleError(w, r, err)
}
// gen will generate a certificate with the given request data
func gen(w http.ResponseWriter, r *http.Request) {
ps := newLoggedPage(w, r)
if ps == nil {
return
}
parent := r.FormValue("parent")
cs, err := readCertSetup("Cert", r)
handleError(w, r, err)
if cs.Name.CommonName == "" {
ps["Error"] = tr("Can't create a certificate with no name!")
ps["Cert"] = cs
ps["parent"] = parent
setCertPageTexts(ps, parent)
err := templates.ExecuteTemplate(w, "cert", ps)
handleError(w, r, err)
return
}
if parent != "" {
cacert := FindCert(parent)
if handleError(w, r, err) {
return
}
_, err = GenCert(cacert, cs.Name.CommonName, cs.Duration)
if handleError(w, r, err) {
return
}
} else {
_, err := GenCACert(cs.Name, cs.Duration)
if handleError(w, r, err) {
return
}
}
http.Redirect(w, r, "/", 302)
}
// certControl allows the web user to manage a certificate
func certControl(w http.ResponseWriter, r *http.Request) {
ps := newLoggedPage(w, r)
if ps == nil {
return
}
cert := r.FormValue("cert")
if cert != "" {
c, err := FindCertOrFail(cert)
if handleError(w, r, err) {
return
}
ps["Cert"] = c
}
err := templates.ExecuteTemplate(w, "certControl", ps)
handleError(w, r, err)
}
// renew the certificate requested
func renew(w http.ResponseWriter, r *http.Request) {
ps := newLoggedPage(w, r)
if ps == nil {
return
}
cert := r.FormValue("cert")
if cert != "" {
c, err := FindCertOrFail(cert)
if handleError(w, r, err) {
return
}
c, err = RenewCert(c)
if handleError(w, r, err) {
return
}
ps["Cert"] = c
}
err := templates.ExecuteTemplate(w, "certControl", ps)
handleError(w, r, err)
}
// clone the certificate requested
func clone(w http.ResponseWriter, r *http.Request) {
ps := newLoggedPage(w, r)
if ps == nil {
return
}
cert := r.FormValue("cert")
var err error
if cert != "" {
c, err := FindCertOrFail(cert)
if handleError(w, r, err) {
return
}
c = CloneCert(c, tr("clone of %v", c.Crt.Subject.CommonName))
ps["Cert"] = c
ps["parent"] = c.Parent.Crt.Subject.CommonName
ps["Cert"] = &CertSetup{Name: c.Crt.Subject}
setCertPageTexts(ps, c.Parent.Crt.Subject.CommonName)
err = templates.ExecuteTemplate(w, "cert", ps)
} else {
err = fmt.Errorf("%s", tr("Nothing to clone!"))
}
handleError(w, r, err)
}
// del will try to remove the requested certificate if possible
func del(w http.ResponseWriter, r *http.Request) {
ps := newLoggedPage(w, r)
if ps == nil {
return
}
cert := r.FormValue("cert")
var err error
if cert != "" {
c, err := FindCertOrFail(cert)
if handleError(w, r, err) {
return
}
ps["Cert"] = c
if c.Childs == nil || len(c.Childs) == 0 {
DeleteCert(c)
index(w, r)
return
} else if c.Crt.IsCA {
ps["PendingConfirmation"] = true
ps["Childs"] = c.Childs
}
}
err = templates.ExecuteTemplate(w, "certControl", ps)
handleError(w, r, err)
}
// newLoggedPage returns a page with a LOGGEDUSER attribute set to the current logged user
func newLoggedPage(w http.ResponseWriter, r *http.Request) PageStatus {
s, err := SessionFor(w, r)
if handleError(w, r, err) {
return nil
}
ps := newPageStatus(r)
ps[LOGGEDUSER] = s[LOGGEDUSER]
return ps
}
// accessControl invokes handler h ONLY IF we are logged in, otherwise the login page
func accessControl(f func(w http.ResponseWriter, r *http.Request)) http.Handler {
return accessControlHandler(http.HandlerFunc(f))
}
// accessControlHandler invokes handler h ONLY IF we are logged in, otherwise the login page
func accessControlHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
s, err := SessionFor(w, r)
if handleError(w, r, err) {
return
}
if s[LOGGEDUSER] == nil {
if fakedLogin {
s[LOGGEDUSER] = User{"fuser", "Faked User", "****", "[email protected]"}
s.Save()
h.ServeHTTP(w, r)
return
}
ps := newPageStatus(r)
ps[SESSIONID] = s.Id()
err := templates.ExecuteTemplate(w, "login", ps)
handleError(w, r, err)
return
}
h.ServeHTTP(w, r)
})
}
// login handles login action
func login(w http.ResponseWriter, r *http.Request) {
Username := r.FormValue("Username")
Password := crypt(r.FormValue("Password"))
cfg := LoadConfig()
u := cfg.getUser(Username)
if u.Password != Password {
ps := newPageStatus(r)
ps["Error"] = tr("Access Denied")
err := templates.ExecuteTemplate(w, "login", ps)
handleError(w, r, err)
return
} else {
s, err := SessionFor(w, r)
if handleError(w, r, err) {
return
}
s[LOGGEDUSER] = u
s.Save()
targetUrl := r.FormValue("URL")
if targetUrl == "" {
targetUrl = "/"
}
http.Redirect(w, r, targetUrl, 302)
}
}
// newPageStatus generates a new PageStatus including the Request
func newPageStatus(r *http.Request) PageStatus {
ps := PageStatus{}
ps[REQUEST] = r
return ps
}
// fakeLogin fakes the login process
func FakeLogin() {
fakedLogin = true
}
// FindCertOrFail fainds the certifcate or fails with an error
func FindCertOrFail(certname string) (*Cert, error) {
cert := FindCert(certname)
if cert != nil {
return cert, nil
}
return nil, fmt.Errorf(tr("%v certificate not found!", cert))
}
// handleError displays err (if not nil) on Stderr and (if possible) displays a web error page
// it also returns true if the error was found and handled and false if err was nil
func handleError(w http.ResponseWriter, r *http.Request, err error) bool {
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return true
}
return false
}