]> Cypherpunks.ru repositories - gocheese.git/blob - gocheese.go
Move everything related to passwords out
[gocheese.git] / gocheese.go
1 /*
2 GoCheese -- Python private package repository and caching proxy
3 Copyright (C) 2019 Sergey Matveev <stargrave@stargrave.org>
4               2019 Elena Balakhonova <balakhonova_e@riseup.net>
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, version 3 of the License.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 // Python private package repository and caching proxy
20 package main
21
22 import (
23         "bytes"
24         "context"
25         "crypto/sha256"
26         "encoding/hex"
27         "flag"
28         "fmt"
29         "io"
30         "io/ioutil"
31         "log"
32         "net"
33         "net/http"
34         "net/url"
35         "os"
36         "os/signal"
37         "path/filepath"
38         "regexp"
39         "runtime"
40         "strings"
41         "syscall"
42         "time"
43
44         "golang.org/x/net/netutil"
45 )
46
47 const (
48         HTMLBegin    = "<!DOCTYPE html><html><head><title>Links for %s</title></head><body><h1>Links for %s</h1>\n"
49         HTMLEnd      = "</body></html>"
50         HTMLElement  = "<a href='%s'>%s</a><br/>\n"
51         SHA256Prefix = "sha256="
52         SHA256Ext    = ".sha256"
53         InternalFlag = ".internal"
54
55         Warranty = `This program is free software: you can redistribute it and/or modify
56 it under the terms of the GNU General Public License as published by
57 the Free Software Foundation, version 3 of the License.
58
59 This program is distributed in the hope that it will be useful,
60 but WITHOUT ANY WARRANTY; without even the implied warranty of
61 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
62 GNU General Public License for more details.
63
64 You should have received a copy of the GNU General Public License
65 along with this program.  If not, see <http://www.gnu.org/licenses/>.`
66 )
67
68 var (
69         pkgPyPI        = regexp.MustCompile(`^.*<a href="([^"]+)"[^>]*>(.+)</a><br/>.*$`)
70         Version string = "UNKNOWN"
71
72         root             = flag.String("root", "./packages", "Path to packages directory")
73         bind             = flag.String("bind", "[::]:8080", "Address to bind to")
74         tlsCert          = flag.String("tls-cert", "", "Path to TLS X.509 certificate")
75         tlsKey           = flag.String("tls-key", "", "Path to TLS X.509 private key")
76         norefreshURLPath = flag.String("norefresh", "/norefresh/", "Non-refreshing URL path")
77         refreshURLPath   = flag.String("refresh", "/simple/", "Auto-refreshing URL path")
78         pypiURL          = flag.String("pypi", "https://pypi.org/simple/", "Upstream PyPI URL")
79         passwdPath       = flag.String("passwd", "passwd", "Path to file with authenticators")
80         passwdCheck      = flag.Bool("passwd-check", false, "Test the -passwd file for syntax errors and exit")
81         fsck             = flag.Bool("fsck", false, "Check integrity of all packages")
82         maxClients       = flag.Int("maxclients", 128, "Maximal amount of simultaneous clients")
83         version          = flag.Bool("version", false, "Print version information")
84         warranty         = flag.Bool("warranty", false, "Print warranty information")
85 )
86
87 func mkdirForPkg(w http.ResponseWriter, r *http.Request, dir string) bool {
88         path := filepath.Join(*root, dir)
89         if _, err := os.Stat(path); os.IsNotExist(err) {
90                 if err = os.Mkdir(path, os.FileMode(0777)); err != nil {
91                         http.Error(w, err.Error(), http.StatusInternalServerError)
92                         return false
93                 }
94                 log.Println(r.RemoteAddr, "mkdir", dir)
95         }
96         return true
97 }
98
99 func refreshDir(w http.ResponseWriter, r *http.Request, dir, filenameGet string) bool {
100         if _, err := os.Stat(filepath.Join(*root, dir, InternalFlag)); err == nil {
101                 log.Println(r.RemoteAddr, "pypi refresh skip, internal package", dir)
102                 return true
103         }
104         log.Println(r.RemoteAddr, "pypi refresh", dir)
105         resp, err := http.Get(*pypiURL + dir + "/")
106         if err != nil {
107                 http.Error(w, err.Error(), http.StatusBadGateway)
108                 return false
109         }
110         defer resp.Body.Close()
111         body, err := ioutil.ReadAll(resp.Body)
112         if err != nil {
113                 http.Error(w, err.Error(), http.StatusBadGateway)
114                 return false
115         }
116         if !mkdirForPkg(w, r, dir) {
117                 return false
118         }
119         var submatches []string
120         var uri string
121         var filename string
122         var path string
123         var pkgURL *url.URL
124         var digest []byte
125         for _, lineRaw := range bytes.Split(body, []byte("\n")) {
126                 submatches = pkgPyPI.FindStringSubmatch(string(lineRaw))
127                 if len(submatches) == 0 {
128                         continue
129                 }
130                 uri = submatches[1]
131                 filename = submatches[2]
132                 if pkgURL, err = url.Parse(uri); err != nil {
133                         http.Error(w, err.Error(), http.StatusInternalServerError)
134                         return false
135                 }
136                 digest, err = hex.DecodeString(strings.TrimPrefix(pkgURL.Fragment, SHA256Prefix))
137                 if err != nil {
138                         http.Error(w, err.Error(), http.StatusBadGateway)
139                         return false
140                 }
141                 if filename == filenameGet {
142                         log.Println(r.RemoteAddr, "pypi download", filename)
143                         path = filepath.Join(*root, dir, filename)
144                         resp, err = http.Get(uri)
145                         if err != nil {
146                                 http.Error(w, err.Error(), http.StatusBadGateway)
147                                 return false
148                         }
149                         defer resp.Body.Close()
150                         hasher := sha256.New()
151                         dst, err := TempFile(filepath.Join(*root, dir))
152                         if err != nil {
153                                 http.Error(w, err.Error(), http.StatusInternalServerError)
154                                 return false
155                         }
156                         wr := io.MultiWriter(hasher, dst)
157                         if _, err = io.Copy(wr, resp.Body); err != nil {
158                                 os.Remove(dst.Name())
159                                 dst.Close()
160                                 http.Error(w, err.Error(), http.StatusInternalServerError)
161                                 return false
162                         }
163                         if bytes.Compare(hasher.Sum(nil), digest) != 0 {
164                                 log.Println(r.RemoteAddr, "pypi", filename, "digest mismatch")
165                                 os.Remove(dst.Name())
166                                 dst.Close()
167                                 http.Error(w, err.Error(), http.StatusBadGateway)
168                                 return false
169                         }
170                         if err = dst.Sync(); err != nil {
171                                 os.Remove(dst.Name())
172                                 dst.Close()
173                                 http.Error(w, err.Error(), http.StatusInternalServerError)
174                                 return false
175                         }
176                         dst.Close()
177                         if err = os.Rename(dst.Name(), path); err != nil {
178                                 http.Error(w, err.Error(), http.StatusInternalServerError)
179                                 return false
180                         }
181                 }
182                 path = filepath.Join(*root, dir, filename+SHA256Ext)
183                 _, err = os.Stat(path)
184                 if err == nil {
185                         continue
186                 } else {
187                         if !os.IsNotExist(err) {
188                                 http.Error(w, err.Error(), http.StatusInternalServerError)
189                                 return false
190                         }
191                 }
192                 log.Println(r.RemoteAddr, "pypi touch", filename)
193                 if err = ioutil.WriteFile(path, digest, os.FileMode(0666)); err != nil {
194                         http.Error(w, err.Error(), http.StatusInternalServerError)
195                         return false
196                 }
197         }
198         return true
199 }
200
201 func listRoot(w http.ResponseWriter, r *http.Request) {
202         log.Println(r.RemoteAddr, "root")
203         files, err := ioutil.ReadDir(*root)
204         if err != nil {
205                 http.Error(w, err.Error(), http.StatusInternalServerError)
206                 return
207         }
208         w.Write([]byte(fmt.Sprintf(HTMLBegin, "root", "root")))
209         for _, file := range files {
210                 if file.Mode().IsDir() {
211                         w.Write([]byte(fmt.Sprintf(
212                                 HTMLElement,
213                                 *refreshURLPath+file.Name()+"/",
214                                 file.Name(),
215                         )))
216                 }
217         }
218         w.Write([]byte(HTMLEnd))
219 }
220
221 func listDir(w http.ResponseWriter, r *http.Request, dir string, autorefresh bool) {
222         log.Println(r.RemoteAddr, "dir", dir)
223         dirPath := filepath.Join(*root, dir)
224         if autorefresh {
225                 if !refreshDir(w, r, dir, "") {
226                         return
227                 }
228         } else if _, err := os.Stat(dirPath); os.IsNotExist(err) && !refreshDir(w, r, dir, "") {
229                 return
230         }
231         files, err := ioutil.ReadDir(dirPath)
232         if err != nil {
233                 http.Error(w, err.Error(), http.StatusInternalServerError)
234                 return
235         }
236         w.Write([]byte(fmt.Sprintf(HTMLBegin, dir, dir)))
237         var data []byte
238         var filenameClean string
239         for _, file := range files {
240                 if !strings.HasSuffix(file.Name(), SHA256Ext) {
241                         continue
242                 }
243                 data, err = ioutil.ReadFile(filepath.Join(dirPath, file.Name()))
244                 if err != nil {
245                         http.Error(w, err.Error(), http.StatusInternalServerError)
246                         return
247                 }
248                 filenameClean = strings.TrimSuffix(file.Name(), SHA256Ext)
249                 w.Write([]byte(fmt.Sprintf(
250                         HTMLElement,
251                         strings.Join([]string{
252                                 *refreshURLPath, dir, "/",
253                                 filenameClean, "#", SHA256Prefix, string(data),
254                         }, ""),
255                         filenameClean,
256                 )))
257         }
258         w.Write([]byte(HTMLEnd))
259 }
260
261 func servePkg(w http.ResponseWriter, r *http.Request, dir, filename string) {
262         log.Println(r.RemoteAddr, "pkg", filename)
263         path := filepath.Join(*root, dir, filename)
264         if _, err := os.Stat(path); os.IsNotExist(err) {
265                 if !refreshDir(w, r, dir, filename) {
266                         return
267                 }
268         }
269         http.ServeFile(w, r, path)
270 }
271
272 func serveUpload(w http.ResponseWriter, r *http.Request) {
273         username, password, ok := r.BasicAuth()
274         if !ok {
275                 log.Println(r.RemoteAddr, "unauthenticated", username)
276                 http.Error(w, "unauthenticated", http.StatusUnauthorized)
277                 return
278         }
279         auther, ok := passwords[username]
280         if !ok || !auther.Auth(password) {
281                 log.Println(r.RemoteAddr, "unauthenticated", username)
282                 http.Error(w, "unauthenticated", http.StatusUnauthorized)
283                 return
284         }
285         var err error
286         if err = r.ParseMultipartForm(1 << 20); err != nil {
287                 http.Error(w, err.Error(), http.StatusBadRequest)
288                 return
289         }
290         for _, file := range r.MultipartForm.File["content"] {
291                 filename := file.Filename
292                 log.Println(r.RemoteAddr, "upload", filename, "by", username)
293                 dir := filename[:strings.LastIndex(filename, "-")]
294                 dirPath := filepath.Join(*root, dir)
295                 path := filepath.Join(dirPath, filename)
296                 if _, err = os.Stat(path); err == nil {
297                         log.Println(r.RemoteAddr, "already exists", filename)
298                         http.Error(w, "Already exists", http.StatusBadRequest)
299                         return
300                 }
301                 if !mkdirForPkg(w, r, dir) {
302                         return
303                 }
304                 internalPath := filepath.Join(dirPath, InternalFlag)
305                 var dst *os.File
306                 if _, err = os.Stat(internalPath); os.IsNotExist(err) {
307                         if dst, err = os.Create(internalPath); err != nil {
308                                 http.Error(w, err.Error(), http.StatusInternalServerError)
309                                 return
310                         }
311                         dst.Close()
312                 }
313                 src, err := file.Open()
314                 defer src.Close()
315                 if err != nil {
316                         http.Error(w, err.Error(), http.StatusInternalServerError)
317                         return
318                 }
319                 dst, err = TempFile(dirPath)
320                 if err != nil {
321                         http.Error(w, err.Error(), http.StatusInternalServerError)
322                         return
323                 }
324                 hasher := sha256.New()
325                 wr := io.MultiWriter(hasher, dst)
326                 if _, err = io.Copy(wr, src); err != nil {
327                         os.Remove(dst.Name())
328                         dst.Close()
329                         http.Error(w, err.Error(), http.StatusInternalServerError)
330                         return
331                 }
332                 if err = dst.Sync(); err != nil {
333                         os.Remove(dst.Name())
334                         dst.Close()
335                         http.Error(w, err.Error(), http.StatusInternalServerError)
336                         return
337                 }
338                 dst.Close()
339                 if err = os.Rename(dst.Name(), path); err != nil {
340                         http.Error(w, err.Error(), http.StatusInternalServerError)
341                         return
342                 }
343                 if err = ioutil.WriteFile(path+SHA256Ext, hasher.Sum(nil), os.FileMode(0666)); err != nil {
344                         http.Error(w, err.Error(), http.StatusInternalServerError)
345                         return
346                 }
347         }
348 }
349
350 func handler(w http.ResponseWriter, r *http.Request) {
351         if r.Method == "GET" {
352                 var path string
353                 var autorefresh bool
354                 if strings.HasPrefix(r.URL.Path, *norefreshURLPath) {
355                         path = strings.TrimPrefix(r.URL.Path, *norefreshURLPath)
356                         autorefresh = false
357                 } else {
358                         path = strings.TrimPrefix(r.URL.Path, *refreshURLPath)
359                         autorefresh = true
360                 }
361                 parts := strings.Split(strings.TrimSuffix(path, "/"), "/")
362                 if len(parts) > 2 {
363                         http.Error(w, "invalid path", http.StatusBadRequest)
364                         return
365                 }
366                 if len(parts) == 1 {
367                         if parts[0] == "" {
368                                 listRoot(w, r)
369                         } else {
370                                 listDir(w, r, parts[0], autorefresh)
371                         }
372                 } else {
373                         servePkg(w, r, parts[0], parts[1])
374                 }
375         } else if r.Method == "POST" {
376                 serveUpload(w, r)
377         }
378 }
379
380 func goodIntegrity() bool {
381         dirs, err := ioutil.ReadDir(*root)
382         if err != nil {
383                 log.Fatal(err)
384         }
385         hasher := sha256.New()
386         digest := make([]byte, sha256.Size)
387         isGood := true
388         var data []byte
389         var pkgName string
390         for _, dir := range dirs {
391                 files, err := ioutil.ReadDir(filepath.Join(*root, dir.Name()))
392                 if err != nil {
393                         log.Fatal(err)
394                 }
395                 for _, file := range files {
396                         if !strings.HasSuffix(file.Name(), SHA256Ext) {
397                                 continue
398                         }
399                         pkgName = strings.TrimSuffix(file.Name(), SHA256Ext)
400                         data, err = ioutil.ReadFile(filepath.Join(*root, dir.Name(), pkgName))
401                         if err != nil {
402                                 if os.IsNotExist(err) {
403                                         continue
404                                 }
405                                 log.Fatal(err)
406                         }
407                         hasher.Write(data)
408                         data, err = ioutil.ReadFile(filepath.Join(*root, dir.Name(), file.Name()))
409                         if err != nil {
410                                 log.Fatal(err)
411                         }
412                         if bytes.Compare(hasher.Sum(digest[:0]), data) == 0 {
413                                 log.Println(pkgName, "GOOD")
414                         } else {
415                                 isGood = false
416                                 log.Println(pkgName, "BAD")
417                         }
418                         hasher.Reset()
419                 }
420         }
421         return isGood
422 }
423
424 func main() {
425         flag.Parse()
426         if *warranty {
427                 fmt.Println(Warranty)
428                 return
429         }
430         if *version {
431                 fmt.Println("GoCheese version " + Version + " built with " + runtime.Version())
432                 return
433         }
434         if *fsck {
435                 if !goodIntegrity() {
436                         os.Exit(1)
437                 }
438                 return
439         }
440         if *passwdCheck {
441                 refreshPasswd()
442                 return
443         }
444         if (*tlsCert != "" && *tlsKey == "") || (*tlsCert == "" && *tlsKey != "") {
445                 log.Fatalln("Both -tls-cert and -tls-key are required")
446         }
447         refreshPasswd()
448         log.Println("root:", *root, "bind:", *bind)
449
450         ln, err := net.Listen("tcp", *bind)
451         if err != nil {
452                 log.Fatal(err)
453         }
454         ln = netutil.LimitListener(ln, *maxClients)
455         server := &http.Server{
456                 ReadTimeout:  time.Minute,
457                 WriteTimeout: time.Minute,
458         }
459         http.HandleFunc(*norefreshURLPath, handler)
460         http.HandleFunc(*refreshURLPath, handler)
461
462         needsRefreshPasswd := make(chan os.Signal, 0)
463         needsShutdown := make(chan os.Signal, 0)
464         killed := make(chan error, 0)
465         signal.Notify(needsRefreshPasswd, syscall.SIGHUP)
466         signal.Notify(needsShutdown, syscall.SIGTERM, syscall.SIGINT)
467         go func() {
468                 for range needsRefreshPasswd {
469                         log.Println("Refreshing passwords")
470                         refreshPasswd()
471                 }
472         }()
473         go func(s *http.Server) {
474                 <-needsShutdown
475                 log.Println("Shutting down")
476                 ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
477                 killed <- s.Shutdown(ctx)
478                 cancel()
479         }(server)
480
481         if *tlsCert == "" {
482                 err = server.Serve(ln)
483         } else {
484                 err = server.ServeTLS(ln, *tlsCert, *tlsKey)
485         }
486         if err != http.ErrServerClosed {
487                 log.Fatal(err)
488         }
489         if err := <-killed; err != nil {
490                 log.Fatal(err)
491         }
492 }