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>
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.
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.
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/>.
34 func serveUpload(w http.ResponseWriter, r *http.Request) {
36 username, password, ok := r.BasicAuth()
38 log.Println(r.RemoteAddr, "unauthenticated", username)
39 http.Error(w, "unauthenticated", http.StatusUnauthorized)
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)
51 if err = r.ParseMultipartForm(1 << 20); err != nil {
52 http.Error(w, err.Error(), http.StatusBadRequest)
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)
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])
66 http.Error(w, "bad sha256_digest: "+err.Error(), http.StatusBadRequest)
70 gpgSigsExpected := make(map[string]struct{})
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)
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, "already exists", filename)
86 http.Error(w, "already exists", http.StatusBadRequest)
89 if !mkdirForPkg(w, r, pkgName) {
92 src, err := file.Open()
95 http.Error(w, err.Error(), http.StatusInternalServerError)
98 dst, err := TempFile(dirPath)
100 http.Error(w, err.Error(), http.StatusInternalServerError)
103 dstBuf := bufio.NewWriter(dst)
104 hasher := sha256.New()
105 wr := io.MultiWriter(hasher, dst)
106 if _, err = io.Copy(wr, src); err != nil {
107 os.Remove(dst.Name())
109 http.Error(w, err.Error(), http.StatusInternalServerError)
112 if err = dstBuf.Flush(); err != nil {
113 os.Remove(dst.Name())
115 http.Error(w, err.Error(), http.StatusInternalServerError)
118 if err = dst.Sync(); err != nil {
119 os.Remove(dst.Name())
121 http.Error(w, err.Error(), http.StatusInternalServerError)
125 digest := hasher.Sum(nil)
126 if digestExpected != nil {
127 if bytes.Compare(digestExpected, digest) == 0 {
128 log.Println(r.RemoteAddr, filename, "good checksum received")
130 log.Println(r.RemoteAddr, filename, "bad checksum received")
131 http.Error(w, "bad checksum", http.StatusBadRequest)
132 os.Remove(dst.Name())
136 if err = os.Rename(dst.Name(), path); err != nil {
137 http.Error(w, err.Error(), http.StatusInternalServerError)
140 if err = DirSync(dirPath); err != nil {
141 http.Error(w, err.Error(), http.StatusInternalServerError)
144 if err = WriteFileSync(dirPath, path+"."+HashAlgoSHA256, digest); err != nil {
145 http.Error(w, err.Error(), http.StatusInternalServerError)
149 for _, file := range r.MultipartForm.File["gpg_signature"] {
150 filename := file.Filename
151 if _, exists := gpgSigsExpected[filename]; !exists {
152 http.Error(w, "unexpected GPG signature filename", http.StatusBadRequest)
155 delete(gpgSigsExpected, filename)
156 log.Println(r.RemoteAddr, "put", filename, "by", username)
157 path := filepath.Join(dirPath, filename)
158 if _, err = os.Stat(path); err == nil {
159 log.Println(r.RemoteAddr, "already exists", filename)
160 http.Error(w, "already exists", http.StatusBadRequest)
163 src, err := file.Open()
165 http.Error(w, err.Error(), http.StatusInternalServerError)
168 sig, err := ioutil.ReadAll(src)
171 http.Error(w, err.Error(), http.StatusInternalServerError)
174 if err = WriteFileSync(dirPath, path, sig); err != nil {
175 http.Error(w, err.Error(), http.StatusInternalServerError)