2023-06-20 20:08:48 +00:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package arch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2023-06-25 10:15:29 +00:00
|
|
|
"time"
|
2023-06-20 20:08:48 +00:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
arch_module "code.gitea.io/gitea/modules/packages/arch"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/routers/api/packages/helper"
|
2023-06-21 20:30:07 +00:00
|
|
|
arch_service "code.gitea.io/gitea/services/packages/arch"
|
2023-06-20 20:08:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Push new package to arch package registry.
|
|
|
|
func Push(ctx *context.Context) {
|
2023-06-21 20:30:07 +00:00
|
|
|
var (
|
2023-06-22 06:29:08 +00:00
|
|
|
owner = ctx.Params("username")
|
2023-06-21 20:30:07 +00:00
|
|
|
filename = ctx.Req.Header.Get("filename")
|
|
|
|
email = ctx.Req.Header.Get("email")
|
|
|
|
distro = ctx.Req.Header.Get("distro")
|
2023-06-28 19:12:01 +00:00
|
|
|
sendtime = ctx.Req.Header.Get("time")
|
|
|
|
pkgsign = ctx.Req.Header.Get("pkgsign")
|
|
|
|
metasign = ctx.Req.Header.Get("metasign")
|
2023-06-21 20:30:07 +00:00
|
|
|
)
|
2023-06-20 20:08:48 +00:00
|
|
|
|
|
|
|
// Decoding package signature.
|
2023-06-28 19:12:01 +00:00
|
|
|
sigdata, err := hex.DecodeString(pkgsign)
|
2023-06-20 20:08:48 +00:00
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-24 10:37:33 +00:00
|
|
|
// Read package to memory for signature validation.
|
2023-06-20 20:08:48 +00:00
|
|
|
pkgdata, err := io.ReadAll(ctx.Req.Body)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer ctx.Req.Body.Close()
|
|
|
|
|
2023-06-21 20:30:07 +00:00
|
|
|
// Get user and organization owning arch package.
|
|
|
|
user, org, err := arch_service.IdentifyOwner(ctx, owner, email)
|
2023-06-20 20:08:48 +00:00
|
|
|
if err != nil {
|
2023-06-21 20:30:07 +00:00
|
|
|
apiError(ctx, http.StatusUnauthorized, err)
|
2023-06-20 20:08:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-28 19:12:01 +00:00
|
|
|
// Decoding time when message was created.
|
|
|
|
t, err := time.Parse(time.RFC3339, sendtime)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if time.Since(t) > time.Hour {
|
|
|
|
apiError(ctx, http.StatusUnauthorized, "outdated message")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decoding signature related to metadata.
|
|
|
|
msigdata, err := hex.DecodeString(metasign)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validating metadata signature, to ensure that operation push operation
|
|
|
|
// is initiated by original package owner.
|
|
|
|
sendmetadata := []byte(owner + filename + sendtime)
|
|
|
|
err = arch_service.ValidateSignature(ctx, sendmetadata, msigdata, user)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-24 10:37:33 +00:00
|
|
|
// Validate package signature with any of user's GnuPG keys.
|
2023-06-25 10:15:29 +00:00
|
|
|
err = arch_service.ValidateSignature(ctx, pkgdata, sigdata, user)
|
2023-06-20 20:08:48 +00:00
|
|
|
if err != nil {
|
2023-06-21 20:30:07 +00:00
|
|
|
apiError(ctx, http.StatusUnauthorized, err)
|
2023-06-20 20:08:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse metadata contained in arch package archive.
|
|
|
|
md, err := arch_module.EjectMetadata(filename, setting.Domain, pkgdata)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-22 07:33:23 +00:00
|
|
|
// Save file related to arch package.
|
|
|
|
pkgid, err := arch_service.SaveFile(ctx, &arch_service.SaveFileParams{
|
|
|
|
Organization: org,
|
|
|
|
User: user,
|
|
|
|
Metadata: md,
|
|
|
|
Filename: filename,
|
|
|
|
Data: pkgdata,
|
|
|
|
Distro: distro,
|
|
|
|
})
|
2023-06-20 20:08:48 +00:00
|
|
|
if err != nil {
|
2023-07-01 14:13:21 +00:00
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
2023-06-20 20:08:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-22 07:33:23 +00:00
|
|
|
// Save file related to arch package signature.
|
|
|
|
_, err = arch_service.SaveFile(ctx, &arch_service.SaveFileParams{
|
|
|
|
Organization: org,
|
|
|
|
User: user,
|
|
|
|
Metadata: md,
|
|
|
|
Data: sigdata,
|
|
|
|
Filename: filename + ".sig",
|
|
|
|
Distro: distro,
|
|
|
|
})
|
2023-06-20 20:08:48 +00:00
|
|
|
if err != nil {
|
2023-07-01 14:13:21 +00:00
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
2023-06-20 20:08:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-01 14:13:21 +00:00
|
|
|
// Save file related to arch package description.
|
|
|
|
_, err = arch_service.SaveFile(ctx, &arch_service.SaveFileParams{
|
|
|
|
Organization: org,
|
|
|
|
User: user,
|
|
|
|
Metadata: md,
|
|
|
|
Data: []byte(md.GetDbDesc()),
|
|
|
|
Filename: filename + ".desc",
|
|
|
|
Distro: distro,
|
|
|
|
})
|
2023-06-20 20:08:48 +00:00
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-01 14:13:21 +00:00
|
|
|
// Automatically connect repository for provided package if name matched.
|
|
|
|
err = arch_service.RepositoryAutoconnect(ctx, owner, md.Name, pkgid)
|
2023-06-20 20:08:48 +00:00
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get file from arch package registry.
|
|
|
|
func Get(ctx *context.Context) {
|
2023-06-21 20:30:07 +00:00
|
|
|
var (
|
|
|
|
file = ctx.Params("file")
|
2023-06-24 15:08:18 +00:00
|
|
|
owner = ctx.Params("username")
|
2023-06-21 20:30:07 +00:00
|
|
|
distro = ctx.Params("distro")
|
|
|
|
arch = ctx.Params("arch")
|
|
|
|
)
|
2023-06-20 20:08:48 +00:00
|
|
|
|
2023-06-21 20:30:07 +00:00
|
|
|
// Packages are stored in different way from pacman databases, and loaded
|
|
|
|
// with LoadPackageFile function.
|
|
|
|
if strings.HasSuffix(file, "tar.zst") || strings.HasSuffix(file, "zst.sig") {
|
|
|
|
pkgdata, err := arch_service.LoadPackageFile(ctx, distro, file)
|
2023-06-20 20:08:48 +00:00
|
|
|
if err != nil {
|
2023-06-21 20:30:07 +00:00
|
|
|
apiError(ctx, http.StatusNotFound, err)
|
2023-06-20 20:08:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-21 20:30:07 +00:00
|
|
|
_, err = ctx.Resp.Write(pkgdata)
|
2023-06-20 20:08:48 +00:00
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Resp.WriteHeader(http.StatusOK)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-21 20:30:07 +00:00
|
|
|
// Pacman databases are stored directly in gitea file storage and could be
|
|
|
|
// loaded with name as a key.
|
|
|
|
if strings.HasSuffix(file, ".db.tar.gz") || strings.HasSuffix(file, ".db") {
|
|
|
|
data, err := arch_service.LoadPacmanDatabase(ctx, owner, distro, arch, file)
|
2023-06-20 21:15:56 +00:00
|
|
|
if err != nil {
|
2023-06-21 20:30:07 +00:00
|
|
|
apiError(ctx, http.StatusNotFound, err)
|
2023-06-20 21:15:56 +00:00
|
|
|
return
|
|
|
|
}
|
2023-06-20 20:08:48 +00:00
|
|
|
|
2023-06-20 21:15:56 +00:00
|
|
|
_, err = ctx.Resp.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2023-06-24 10:37:33 +00:00
|
|
|
|
2023-06-20 21:15:56 +00:00
|
|
|
ctx.Resp.WriteHeader(http.StatusOK)
|
2023-06-24 10:37:33 +00:00
|
|
|
return
|
2023-06-20 20:08:48 +00:00
|
|
|
}
|
2023-06-24 10:37:33 +00:00
|
|
|
|
2023-06-20 21:15:56 +00:00
|
|
|
ctx.Resp.WriteHeader(http.StatusNotFound)
|
2023-06-20 20:08:48 +00:00
|
|
|
}
|
|
|
|
|
2023-06-25 14:45:16 +00:00
|
|
|
// Remove specific package version, related files and pacman database entry.
|
2023-06-25 10:15:29 +00:00
|
|
|
func Remove(ctx *context.Context) {
|
|
|
|
var (
|
|
|
|
owner = ctx.Params("username")
|
|
|
|
email = ctx.Req.Header.Get("email")
|
|
|
|
distro = ctx.Req.Header.Get("distro")
|
|
|
|
target = ctx.Req.Header.Get("target")
|
|
|
|
stime = ctx.Req.Header.Get("time")
|
|
|
|
version = ctx.Req.Header.Get("version")
|
|
|
|
arch = strings.Split(ctx.Req.Header.Get("arch"), " ")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Parse sent time and check if it is within last minute.
|
|
|
|
t, err := time.Parse(time.RFC3339, stime)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if time.Since(t) > time.Minute {
|
|
|
|
apiError(ctx, http.StatusUnauthorized, "outdated message")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get user owning the package.
|
|
|
|
user, org, err := arch_service.IdentifyOwner(ctx, owner, email)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read signature data from request body.
|
|
|
|
sigdata, err := io.ReadAll(ctx.Req.Body)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer ctx.Req.Body.Close()
|
|
|
|
|
|
|
|
// Validate package signature with any of user's GnuPG keys.
|
2023-06-28 19:12:01 +00:00
|
|
|
mesdata := []byte(owner + target + stime)
|
2023-06-25 10:15:29 +00:00
|
|
|
err = arch_service.ValidateSignature(ctx, mesdata, sigdata, user)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-28 19:12:01 +00:00
|
|
|
// Remove pacman database entries related to package.
|
|
|
|
err = arch_service.RemoveDbEntry(ctx, arch, owner, distro, target, version)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-25 10:15:29 +00:00
|
|
|
// Remove package files and pacman database entry.
|
|
|
|
err = arch_service.RemovePackage(ctx, &arch_service.RemoveParameters{
|
|
|
|
User: user,
|
|
|
|
Organization: org,
|
|
|
|
Owner: owner,
|
|
|
|
Name: target,
|
|
|
|
Version: version,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Resp.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2023-06-20 20:08:48 +00:00
|
|
|
func apiError(ctx *context.Context, status int, obj interface{}) {
|
|
|
|
helper.LogAndProcessError(ctx, status, obj, func(message string) {
|
|
|
|
ctx.PlainText(status, message)
|
|
|
|
})
|
|
|
|
}
|