]> Cypherpunks.ru repositories - gocheese.git/blob - upload.go
59c18404396905d6bb13b0f539e74fd211ea4014
[gocheese.git] / upload.go
1 /*
2 GoCheese -- Python private package repository and caching proxy
3 Copyright (C) 2019-2020 Sergey Matveev <stargrave@stargrave.org>
4               2019-2020 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 package main
20
21 import (
22         "bufio"
23         "bytes"
24         "crypto/sha256"
25         "encoding/hex"
26         "io"
27         "io/ioutil"
28         "log"
29         "net/http"
30         "os"
31         "path/filepath"
32 )
33
34 func serveUpload(w http.ResponseWriter, r *http.Request) {
35         // Authentication
36         username, password, ok := r.BasicAuth()
37         if !ok {
38                 log.Println(r.RemoteAddr, "unauthenticated", username)
39                 http.Error(w, "unauthenticated", http.StatusUnauthorized)
40                 return
41         }
42         auther, ok := passwords[username]
43         if !ok || !auther.Auth(password) {
44                 log.Println(r.RemoteAddr, "unauthenticated", username)
45                 http.Error(w, "unauthenticated", http.StatusUnauthorized)
46                 return
47         }
48
49         // Form parsing
50         var err error
51         if err = r.ParseMultipartForm(1 << 20); err != nil {
52                 http.Error(w, err.Error(), http.StatusBadRequest)
53                 return
54         }
55         pkgNames, exists := r.MultipartForm.Value["name"]
56         if !exists || len(pkgNames) != 1 {
57                 http.Error(w, "single name is expected in request", http.StatusBadRequest)
58                 return
59         }
60         pkgName := normalizationRe.ReplaceAllString(pkgNames[0], "-")
61         dirPath := filepath.Join(*root, pkgName)
62         var digestExpected []byte
63         if digestExpectedHex, exists := r.MultipartForm.Value["sha256_digest"]; exists {
64                 digestExpected, err = hex.DecodeString(digestExpectedHex[0])
65                 if err != nil {
66                         http.Error(w, "bad sha256_digest: "+err.Error(), http.StatusBadRequest)
67                         return
68                 }
69         }
70         gpgSigsExpected := make(map[string]struct{})
71
72         // Checking is it internal package
73         if _, err = os.Stat(filepath.Join(dirPath, InternalFlag)); err != nil {
74                 log.Println(r.RemoteAddr, "non-internal package", pkgName)
75                 http.Error(w, "unknown internal package", http.StatusUnauthorized)
76                 return
77         }
78
79         for _, file := range r.MultipartForm.File["content"] {
80                 filename := file.Filename
81                 gpgSigsExpected[filename+GPGSigExt] = struct{}{}
82                 log.Println(r.RemoteAddr, "put", filename, "by", username)
83                 path := filepath.Join(dirPath, filename)
84                 if _, err = os.Stat(path); err == nil {
85                         log.Println(r.RemoteAddr, filename, "already exists")
86                         http.Error(w, "already exists", http.StatusBadRequest)
87                         return
88                 }
89                 if !mkdirForPkg(w, r, pkgName) {
90                         return
91                 }
92                 src, err := file.Open()
93                 defer src.Close()
94                 if err != nil {
95                         log.Println("error", r.RemoteAddr, filename, err)
96                         http.Error(w, err.Error(), http.StatusInternalServerError)
97                         return
98                 }
99                 dst, err := TempFile(dirPath)
100                 if err != nil {
101                         log.Println("error", r.RemoteAddr, filename, err)
102                         http.Error(w, err.Error(), http.StatusInternalServerError)
103                         return
104                 }
105                 dstBuf := bufio.NewWriter(dst)
106                 hasher := sha256.New()
107                 wr := io.MultiWriter(hasher, dst)
108                 if _, err = io.Copy(wr, src); err != nil {
109                         log.Println("error", r.RemoteAddr, filename, err)
110                         os.Remove(dst.Name())
111                         dst.Close()
112                         http.Error(w, err.Error(), http.StatusInternalServerError)
113                         return
114                 }
115                 if err = dstBuf.Flush(); err != nil {
116                         log.Println("error", r.RemoteAddr, filename, err)
117                         os.Remove(dst.Name())
118                         dst.Close()
119                         http.Error(w, err.Error(), http.StatusInternalServerError)
120                         return
121                 }
122                 if err = dst.Sync(); err != nil {
123                         log.Println("error", r.RemoteAddr, filename, err)
124                         os.Remove(dst.Name())
125                         dst.Close()
126                         http.Error(w, err.Error(), http.StatusInternalServerError)
127                         return
128                 }
129                 dst.Close()
130                 digest := hasher.Sum(nil)
131                 if digestExpected != nil {
132                         if bytes.Compare(digestExpected, digest) == 0 {
133                                 log.Println(r.RemoteAddr, filename, "good checksum received")
134                         } else {
135                                 log.Println(r.RemoteAddr, filename, "bad checksum received")
136                                 http.Error(w, "bad checksum", http.StatusBadRequest)
137                                 os.Remove(dst.Name())
138                                 return
139                         }
140                 }
141                 if err = os.Rename(dst.Name(), path); err != nil {
142                         log.Println("error", r.RemoteAddr, path, err)
143                         http.Error(w, err.Error(), http.StatusInternalServerError)
144                         return
145                 }
146                 if err = DirSync(dirPath); err != nil {
147                         log.Println("error", r.RemoteAddr, dirPath, err)
148                         http.Error(w, err.Error(), http.StatusInternalServerError)
149                         return
150                 }
151                 if err = WriteFileSync(dirPath, path+"."+HashAlgoSHA256, digest); err != nil {
152                         log.Println("error", r.RemoteAddr, path+"."+HashAlgoSHA256, err)
153                         http.Error(w, err.Error(), http.StatusInternalServerError)
154                         return
155                 }
156         }
157         for _, file := range r.MultipartForm.File["gpg_signature"] {
158                 filename := file.Filename
159                 if _, exists := gpgSigsExpected[filename]; !exists {
160                         log.Println(r.RemoteAddr, filename, "unexpected GPG signature filename")
161                         http.Error(w, "unexpected GPG signature filename", http.StatusBadRequest)
162                         return
163                 }
164                 delete(gpgSigsExpected, filename)
165                 log.Println(r.RemoteAddr, "put", filename, "by", username)
166                 path := filepath.Join(dirPath, filename)
167                 if _, err = os.Stat(path); err == nil {
168                         log.Println(r.RemoteAddr, filename, "already exists")
169                         http.Error(w, "already exists", http.StatusBadRequest)
170                         return
171                 }
172                 src, err := file.Open()
173                 if err != nil {
174                         log.Println("error", r.RemoteAddr, filename, err)
175                         http.Error(w, err.Error(), http.StatusInternalServerError)
176                         return
177                 }
178                 sig, err := ioutil.ReadAll(src)
179                 src.Close()
180                 if err != nil {
181                         log.Println("error", r.RemoteAddr, filename, err)
182                         http.Error(w, err.Error(), http.StatusInternalServerError)
183                         return
184                 }
185                 if err = WriteFileSync(dirPath, path, sig); err != nil {
186                         log.Println("error", r.RemoteAddr, path, err)
187                         http.Error(w, err.Error(), http.StatusInternalServerError)
188                         return
189                 }
190         }
191 }