This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·185 lines (158 loc) · 4.42 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
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
// Copyright 2014 gandalf authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/signal"
"runtime"
"syscall"
"time"
"github.com/gleez/gandalf/api"
"github.com/gleez/gandalf/config"
"github.com/gleez/gandalf/sshd"
)
const version = "0.5.2"
var (
// Build info, populated during linking by goxc
VERSION = "0.5.2"
BUILD_DATE = "undefined"
// Command line flags
help = flag.Bool("help", false, "Displays this help")
pidfile = flag.String("pidfile", "none", "Write our PID into the specified file")
logfile = flag.String("logfile", "stderr", "Write out log into the specified file")
configFile = flag.String("config", "/etc/gandalf.conf", "Path to the configuration file")
gVersion = flag.Bool("version", false, "Print version and exit")
// startTime is used to calculate uptime of gandalf
startTime = time.Now()
// The file we send log output to, will be nil for stderr or stdout
logf *os.File
// Server instances
sshServer *sshd.Server
)
func main() {
flag.Parse()
runtime.GOMAXPROCS(runtime.NumCPU())
if *gVersion {
fmt.Printf("gandalf version %s\n", version)
return
}
err := config.ReadAndWatchConfigFile(*configFile)
if err != nil {
msg := `Could not find gandalf config file. Searched on %s.
For an example conf check gandalf/etc/gandalf.conf file.\n %s`
log.Panicf(msg, *configFile, err)
}
sshbind, err := config.GetString("sshbind")
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse sshbind: %v\n", err)
os.Exit(1)
}
privateBytes, err := ioutil.ReadFile("etc/id_rsa")
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to load private key: %v\n", err)
os.Exit(1)
}
uid, err := config.GetString("uid")
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse uid: %v\n", err)
os.Exit(1)
}
// Setup signal handler
sigChan := make(chan os.Signal)
signal.Notify(sigChan, syscall.SIGHUP, syscall.SIGTERM)
go signalProcessor(sigChan)
if *logfile != "stderr" {
// stderr is the go logging default
if *logfile == "stdout" {
// set to stdout
log.SetOutput(os.Stdout)
} else {
err := openLogFile()
if err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
os.Exit(1)
}
defer closeLogFile()
// close std* streams
os.Stdout.Close()
os.Stderr.Close() // Warning: this will hide panic() output
os.Stdin.Close()
os.Stdout = logf
os.Stderr = logf
}
}
log.Printf("SSH %v (%v) starting... with pid %v", VERSION, BUILD_DATE, os.Getpid())
// Write pidfile if requested
// TODO: Probably supposed to remove pidfile during shutdown
if *pidfile != "none" {
pidf, err := os.Create(*pidfile)
if err != nil {
log.Printf("Failed to create %v: %v", *pidfile, err)
os.Exit(1)
}
defer pidf.Close()
fmt.Fprintf(pidf, "%v\n", os.Getpid())
}
// Start HTTP API server
go api.Start()
// Starts a SSH server listens on given port.
sshServer = sshd.NewServer(sshbind, uid, sshd.PrivateKey(privateBytes))
sshServer.Start()
// Wait for active connections to finish
sshServer.Drain()
}
// openLogFile creates or appends to the logfile passed on commandline
func openLogFile() error {
// use specified log file
var err error
logf, err = os.OpenFile(*logfile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
return fmt.Errorf("Failed to create %v: %v\n", *logfile, err)
}
log.SetOutput(logf)
log.Println("Opened new logfile")
return nil
}
// closeLogFile closes the current logfile
func closeLogFile() error {
log.Println("Closing logfile")
return logf.Close()
}
// signalProcessor is a goroutine that handles OS signals
func signalProcessor(c <-chan os.Signal) {
for {
sig := <-c
switch sig {
case syscall.SIGHUP:
// Rotate logs if configured
if logf != nil {
log.Println("Recieved SIGHUP, cycling logfile")
closeLogFile()
openLogFile()
} else {
log.Println("Ignoring SIGHUP, logfile not configured")
}
case syscall.SIGTERM:
// Initiate shutdown
log.Println("Received SIGTERM, shutting down")
go timedExit()
api.Stop()
if sshServer != nil {
sshServer.Stop()
} else {
log.Println("sshServer was nil during shutdown")
}
}
}
}
// timedExit is called as a goroutine during shutdown, it will force an exit after 15 seconds
func timedExit() {
time.Sleep(15 * time.Second)
log.Println("Smtpd clean shutdown timed out, forcing exit")
os.Exit(0)
}