-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.go
More file actions
331 lines (277 loc) · 8.8 KB
/
main.go
File metadata and controls
331 lines (277 loc) · 8.8 KB
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
//
// This is a small HTTP server implementing the "photobackup server" endpoint
// documented here: https://github.com/PhotoBackup/api/
//
// Written because the existing servers make me a touch sad; go means we can
// avoid a pile of runtime dependencies. Build-time dependencies are being kept
// low; bcrypt, homedir and graceful are the only luxuries. Adding gorilla mux
// and, perhaps, negroni, would probably be overkill.
//
// We're trying to be compatible, so config file is INI-format: ~/.photobackup
// [photobackup]
// MediaRoot=<path to store files in>
// Password=<sha512 digest of password, no salt>
// Port=<port>
//
// In addition to these keys, I'm also supporting:
// BindAddress=<address to bind to>
// PasswordBcrypt=<bcrypt of sha512 digest of plain password>
// HTTPPrefix=<prefix to mount HTTP server at>
//
// The original server was intended to run over HTTP, I think, hence the client
// sending a SHA512'd password. We support this scheme, but the on-disc storage
// format is really better off being bcrypt(sha512(password)), so I've added
// that.
//
// Adding BindAddress and HTTPPrefix means that mounting this behind a HTTP
// reverse proxy is quite doable, and lets us offload HTTPS to that as well.
// That's how I'm intending to use it.
//
// I think the original servers are designed so you can connect to them using
// just HTTP; hence the sha512(password) scheme. This is short-sighted; the only
// thing it gets you is (weak) protection against sniffing if you happen to use
// the same password elsewhere. Sniffers in this scenario can still upload to
// your server and view your photos.
//
// At some point in the future I might add direct HTTPS support as well, but I
// don't need it.
//
// @author Nick Thomas <photobackup@ur.gs>
package main
import (
"errors"
"fmt"
"github.com/Unknwon/goconfig"
"github.com/mitchellh/go-homedir"
"golang.org/x/crypto/bcrypt"
"gopkg.in/tylerb/graceful.v1"
"io"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"time"
)
type Config struct {
MediaRoot string // mandatory
Port string // optional, defaults to 8420
BindAddress string // Optional, defaults to 127.0.0.1
PasswordBcrypt string // Either this or Password
HTTPPrefix string // Optional, defaults to "/"
}
// Implements the API defined here: https://github.com/PhotoBackup/api
type Handler struct {
Config *Config
}
func (h *Handler) BuildMux() http.Handler {
mux := http.NewServeMux()
prefix := h.Config.HTTPPrefix
if prefix == "/" {
prefix = ""
}
// We don't use http.StripPrefix because that leaves a path of "" for "/foo"
if len(prefix) > 0 {
mux.HandleFunc(prefix, h.RootHandler)
}
mux.HandleFunc(prefix+"/", h.RootHandler)
mux.HandleFunc(prefix+"/test", h.TestHandler)
mux.HandleFunc(prefix+"/test/", h.TestHandler)
return mux
}
// TODO: this lot could be refactored into smaller methods
func (h *Handler) RootHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
http.Redirect(w, r, "https://photobackup.github.io/", 302)
return
}
if r.Method != "POST" {
w.WriteHeader(405)
return
}
if !h.CheckPassword(w, r) {
return
}
filesize, ok := h.ReadFileSize(w, r)
if !ok {
return
}
upfile, upfileHdr, err := r.FormFile("upfile")
if upfile == nil || upfileHdr == nil || upfileHdr.Filename == "" || err != nil {
if err == nil {
err = errors.New("Unknown error")
}
// No idea why the spec demands 401 here
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Failed to read upfile parameter: " + err.Error()))
return
}
filename := filepath.Join(h.Config.MediaRoot, upfileHdr.Filename)
// This will always be vulnerable to races, but here we check if the
// file already exists, and return a 409 if it does. This isn't in the
// RAML, and the "official" Python API returns nothing AFAICT. This is
// definitely an improvement over that!
if _, err := os.Stat(filename); !os.IsNotExist(err) {
w.WriteHeader(http.StatusConflict)
}
file, err := os.Create(filename)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Error opening file: " + err.Error()))
return
}
// Try to read `filesize` bytes into the destination. An io.EOF says
// that filesize was too low; anything else was our fault.
_, err = io.CopyN(file, upfile, filesize)
if err != nil {
if err == io.EOF {
w.WriteHeader(http.StatusLengthRequired)
w.Write([]byte("filesize larger than upfile data"))
} else {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Error writing file: " + err.Error()))
}
// best-effort `rm' here.
_ = os.Remove(filename)
return
}
// Now we try to read one more byte. If that *doesn't* return EOF, then
// filesize was too low.
//
// What we don't do here is read the entire file before deciding that
// it was too big, as the Python server does.
tmp := make([]byte, 1)
if _, err := upfile.Read(tmp); err == nil {
w.WriteHeader(http.StatusLengthRequired)
w.Write([]byte("upfile data larger than filesize"))
// best-effort remove here too
_ = os.Remove(filename)
return
}
if err := file.Close(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Error closing written file: " + err.Error()))
return
}
w.WriteHeader(http.StatusOK)
}
func (h *Handler) TestHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(405)
return
}
if !h.CheckPassword(w, r) {
return
}
filename := filepath.Join(h.Config.MediaRoot, ".test_file_to_write")
_ = os.Remove(filename)
defer os.Remove(filename)
file, err := os.Create(filename)
if err == nil {
err = file.Close()
}
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Error opening file: " + err.Error()))
return
}
}
// The multipart/mime form-data "password" the hexdigested sha512'd password
// We bcrypt() it then check it against the password in the config file in a
// way that *isn't* vulnerable to timing attacks.
//
// Side effect: We write a 403 to w if the password is bad or missing
func (h *Handler) CheckPassword(w http.ResponseWriter, r *http.Request) bool {
// TODO: get rid of these conversions
actual := []byte(h.Config.PasswordBcrypt)
putative := []byte(r.FormValue("password"))
ok := (bcrypt.CompareHashAndPassword(actual, putative) == nil)
if !ok {
w.WriteHeader(http.StatusForbidden)
}
return ok
}
// Reads the filesize parameter from the request.
//
// Side effect: Writes a 400 if the filesize param isn't present or doesn't
// convert to a positive int64. The spec allows the client to specify a negative
// number but I'm not interested in that.
func (h *Handler) ReadFileSize(w http.ResponseWriter, r *http.Request) (int64, bool) {
str := r.FormValue("filesize")
if str == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("filesize not set"))
return -1, false
}
filesize, err := strconv.ParseInt(str, 10, 64)
if err != nil || filesize < 0 {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("filesize does not parse to a 64-bit int > 0"))
return -1, false
}
return filesize, true
}
func GetConfig() (*Config, error) {
var ConfigFile string
if len(os.Args) == 2 {
ConfigFile = os.Args[1]
} else {
if base, err := homedir.Dir(); err != nil {
return nil, err
} else {
ConfigFile = filepath.Join(base, ".photobackup")
}
}
cfg, err := goconfig.LoadConfigFile(ConfigFile)
if err != nil {
return nil, err
}
section, err := cfg.GetSection("photobackup")
if err != nil {
return nil, err
}
out := Config{
MediaRoot: section["MediaRoot"],
Port: section["Port"],
BindAddress: section["BindAddress"],
PasswordBcrypt: section["PasswordBcrypt"],
HTTPPrefix: section["HTTPPrefix"],
}
if out.MediaRoot == "" {
return nil, errors.New("MediaRoot not specified")
}
// TODO: check MediaRoot is a valid, writable path?
// If Password and PasswordBcrypt are both set, the latter wins
if pwd, ok := section["Password"]; ok && out.PasswordBcrypt == "" {
hash, err := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.DefaultCost)
if err != nil {
return nil, errors.New("Failed to convert Password to PasswordBcrypt: " + err.Error())
}
out.PasswordBcrypt = string(hash)
}
if out.PasswordBcrypt == "" {
return nil, errors.New("Neither Password nor PasswordBcrypt specified")
}
if out.BindAddress == "" {
out.BindAddress = "127.0.0.1"
}
if out.Port == "" {
out.Port = "8420"
}
return &out, nil
}
func main() {
if len(os.Args) > 2 {
fmt.Fprintf(os.Stderr, "Usage: %s [config-file]\n", os.Args[0])
os.Exit(1)
}
config, err := GetConfig()
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't read config: %s\n", err.Error())
os.Exit(1)
}
listenOn := net.JoinHostPort(config.BindAddress, config.Port)
api := &Handler{Config: config}
mux := api.BuildMux()
graceful.Run(listenOn, 10*time.Second, mux)
}