2019-10-07 16:19:01 +00:00
|
|
|
package httpd
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-09-01 14:10:26 +00:00
|
|
|
"fmt"
|
2019-07-20 10:26:52 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/go-chi/render"
|
2020-05-06 17:36:34 +00:00
|
|
|
|
2020-09-01 14:10:26 +00:00
|
|
|
"github.com/drakkan/sftpgo/common"
|
2020-05-06 17:36:34 +00:00
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
2020-11-30 20:46:34 +00:00
|
|
|
"github.com/drakkan/sftpgo/kms"
|
2020-11-22 20:53:04 +00:00
|
|
|
"github.com/drakkan/sftpgo/vfs"
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func getUsers(w http.ResponseWriter, r *http.Request) {
|
|
|
|
limit := 100
|
|
|
|
offset := 0
|
2020-06-07 21:30:18 +00:00
|
|
|
order := dataprovider.OrderASC
|
2019-07-20 10:26:52 +00:00
|
|
|
username := ""
|
|
|
|
var err error
|
|
|
|
if _, ok := r.URL.Query()["limit"]; ok {
|
|
|
|
limit, err = strconv.Atoi(r.URL.Query().Get("limit"))
|
|
|
|
if err != nil {
|
|
|
|
err = errors.New("Invalid limit")
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if limit > 500 {
|
|
|
|
limit = 500
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := r.URL.Query()["offset"]; ok {
|
|
|
|
offset, err = strconv.Atoi(r.URL.Query().Get("offset"))
|
|
|
|
if err != nil {
|
|
|
|
err = errors.New("Invalid offset")
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := r.URL.Query()["order"]; ok {
|
|
|
|
order = r.URL.Query().Get("order")
|
2020-06-07 21:30:18 +00:00
|
|
|
if order != dataprovider.OrderASC && order != dataprovider.OrderDESC {
|
2019-07-20 10:26:52 +00:00
|
|
|
err = errors.New("Invalid order")
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := r.URL.Query()["username"]; ok {
|
|
|
|
username = r.URL.Query().Get("username")
|
|
|
|
}
|
2020-07-08 17:59:31 +00:00
|
|
|
users, err := dataprovider.GetUsers(limit, offset, order, username)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err == nil {
|
|
|
|
render.JSON(w, r, users)
|
|
|
|
} else {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUserByID(w http.ResponseWriter, r *http.Request) {
|
|
|
|
userID, err := strconv.ParseInt(chi.URLParam(r, "userID"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.New("Invalid userID")
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2020-07-08 17:59:31 +00:00
|
|
|
user, err := dataprovider.GetUserByID(userID)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err == nil {
|
2020-11-22 20:53:04 +00:00
|
|
|
user.HideConfidentialData()
|
|
|
|
render.JSON(w, r, user)
|
2019-07-20 10:26:52 +00:00
|
|
|
} else {
|
2020-06-20 10:38:04 +00:00
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func addUser(w http.ResponseWriter, r *http.Request) {
|
2020-01-31 18:04:00 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2019-07-20 10:26:52 +00:00
|
|
|
var user dataprovider.User
|
|
|
|
err := render.DecodeJSON(r.Body, &user)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2020-11-30 20:46:34 +00:00
|
|
|
user.SetEmptySecretsIfNil()
|
2020-11-22 20:53:04 +00:00
|
|
|
switch user.FsConfig.Provider {
|
|
|
|
case dataprovider.S3FilesystemProvider:
|
|
|
|
if user.FsConfig.S3Config.AccessSecret.IsRedacted() {
|
|
|
|
sendAPIResponse(w, r, errors.New("invalid access_secret"), "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case dataprovider.GCSFilesystemProvider:
|
|
|
|
if user.FsConfig.GCSConfig.Credentials.IsRedacted() {
|
|
|
|
sendAPIResponse(w, r, errors.New("invalid credentials"), "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case dataprovider.AzureBlobFilesystemProvider:
|
|
|
|
if user.FsConfig.AzBlobConfig.AccountKey.IsRedacted() {
|
|
|
|
sendAPIResponse(w, r, errors.New("invalid account_key"), "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2020-12-05 12:48:13 +00:00
|
|
|
case dataprovider.CryptedFilesystemProvider:
|
|
|
|
if user.FsConfig.CryptConfig.Passphrase.IsRedacted() {
|
|
|
|
sendAPIResponse(w, r, errors.New("invalid passphrase"), "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2020-12-12 09:31:09 +00:00
|
|
|
case dataprovider.SFTPFilesystemProvider:
|
|
|
|
if user.FsConfig.SFTPConfig.Password.IsRedacted() {
|
|
|
|
sendAPIResponse(w, r, errors.New("invalid SFTP password"), "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if user.FsConfig.SFTPConfig.PrivateKey.IsRedacted() {
|
|
|
|
sendAPIResponse(w, r, errors.New("invalid SFTP private key"), "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2020-11-22 20:53:04 +00:00
|
|
|
}
|
2021-01-05 08:50:22 +00:00
|
|
|
err = dataprovider.AddUser(&user)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err == nil {
|
2020-07-08 17:59:31 +00:00
|
|
|
user, err = dataprovider.UserExists(user.Username)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err == nil {
|
2020-11-22 20:53:04 +00:00
|
|
|
user.HideConfidentialData()
|
|
|
|
render.JSON(w, r, user)
|
2019-07-20 10:26:52 +00:00
|
|
|
} else {
|
2020-06-20 10:38:04 +00:00
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateUser(w http.ResponseWriter, r *http.Request) {
|
2020-01-31 18:04:00 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2019-07-20 10:26:52 +00:00
|
|
|
userID, err := strconv.ParseInt(chi.URLParam(r, "userID"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.New("Invalid userID")
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2020-09-01 14:10:26 +00:00
|
|
|
disconnect := 0
|
|
|
|
if _, ok := r.URL.Query()["disconnect"]; ok {
|
|
|
|
disconnect, err = strconv.Atoi(r.URL.Query().Get("disconnect"))
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("invalid disconnect parameter: %v", err)
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-07-08 17:59:31 +00:00
|
|
|
user, err := dataprovider.GetUserByID(userID)
|
2020-06-20 10:38:04 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
2020-01-31 18:04:00 +00:00
|
|
|
currentPermissions := user.Permissions
|
2020-12-12 09:31:09 +00:00
|
|
|
currentS3AccessSecret := user.FsConfig.S3Config.AccessSecret
|
|
|
|
currentAzAccountKey := user.FsConfig.AzBlobConfig.AccountKey
|
|
|
|
currentGCSCredentials := user.FsConfig.GCSConfig.Credentials
|
|
|
|
currentCryptoPassphrase := user.FsConfig.CryptConfig.Passphrase
|
|
|
|
currentSFTPPassword := user.FsConfig.SFTPConfig.Password
|
|
|
|
currentSFTPKey := user.FsConfig.SFTPConfig.PrivateKey
|
2020-12-05 12:48:13 +00:00
|
|
|
|
2019-12-25 17:20:19 +00:00
|
|
|
user.Permissions = make(map[string][]string)
|
2020-11-22 20:53:04 +00:00
|
|
|
user.FsConfig.S3Config = vfs.S3FsConfig{}
|
|
|
|
user.FsConfig.AzBlobConfig = vfs.AzBlobFsConfig{}
|
|
|
|
user.FsConfig.GCSConfig = vfs.GCSFsConfig{}
|
2020-12-05 12:48:13 +00:00
|
|
|
user.FsConfig.CryptConfig = vfs.CryptFsConfig{}
|
2020-12-12 09:31:09 +00:00
|
|
|
user.FsConfig.SFTPConfig = vfs.SFTPFsConfig{}
|
2019-07-20 10:26:52 +00:00
|
|
|
err = render.DecodeJSON(r.Body, &user)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2020-11-30 20:46:34 +00:00
|
|
|
user.SetEmptySecretsIfNil()
|
2019-12-25 17:20:19 +00:00
|
|
|
// we use new Permissions if passed otherwise the old ones
|
|
|
|
if len(user.Permissions) == 0 {
|
2020-01-31 18:04:00 +00:00
|
|
|
user.Permissions = currentPermissions
|
2019-12-25 17:20:19 +00:00
|
|
|
}
|
2020-12-12 09:31:09 +00:00
|
|
|
updateEncryptedSecrets(&user, currentS3AccessSecret, currentAzAccountKey, currentGCSCredentials, currentCryptoPassphrase,
|
|
|
|
currentSFTPPassword, currentSFTPKey)
|
2019-07-20 10:26:52 +00:00
|
|
|
if user.ID != userID {
|
|
|
|
sendAPIResponse(w, r, err, "user ID in request body does not match user ID in path parameter", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-01-05 08:50:22 +00:00
|
|
|
err = dataprovider.UpdateUser(&user)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
} else {
|
|
|
|
sendAPIResponse(w, r, err, "User updated", http.StatusOK)
|
2020-09-01 14:10:26 +00:00
|
|
|
if disconnect == 1 {
|
|
|
|
disconnectUser(user.Username)
|
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteUser(w http.ResponseWriter, r *http.Request) {
|
|
|
|
userID, err := strconv.ParseInt(chi.URLParam(r, "userID"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.New("Invalid userID")
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2020-07-08 17:59:31 +00:00
|
|
|
user, err := dataprovider.GetUserByID(userID)
|
2020-06-20 10:38:04 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
2019-07-20 10:26:52 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-05 08:50:22 +00:00
|
|
|
err = dataprovider.DeleteUser(&user)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusInternalServerError)
|
|
|
|
} else {
|
|
|
|
sendAPIResponse(w, r, err, "User deleted", http.StatusOK)
|
2020-09-01 14:10:26 +00:00
|
|
|
disconnectUser(user.Username)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func disconnectUser(username string) {
|
|
|
|
for _, stat := range common.Connections.GetStats() {
|
|
|
|
if stat.Username == username {
|
|
|
|
common.Connections.Close(stat.ConnectionID)
|
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
|
2020-12-05 12:48:13 +00:00
|
|
|
func updateEncryptedSecrets(user *dataprovider.User, currentS3AccessSecret, currentAzAccountKey,
|
2020-12-12 09:31:09 +00:00
|
|
|
currentGCSCredentials, currentCryptoPassphrase, currentSFTPPassword, currentSFTPKey *kms.Secret) {
|
2020-11-22 20:53:04 +00:00
|
|
|
// we use the new access secret if plain or empty, otherwise the old value
|
2020-12-12 09:31:09 +00:00
|
|
|
switch user.FsConfig.Provider {
|
|
|
|
case dataprovider.S3FilesystemProvider:
|
|
|
|
if user.FsConfig.S3Config.AccessSecret.IsNotPlainAndNotEmpty() {
|
2020-10-25 07:18:48 +00:00
|
|
|
user.FsConfig.S3Config.AccessSecret = currentS3AccessSecret
|
|
|
|
}
|
2020-12-12 09:31:09 +00:00
|
|
|
case dataprovider.AzureBlobFilesystemProvider:
|
|
|
|
if user.FsConfig.AzBlobConfig.AccountKey.IsNotPlainAndNotEmpty() {
|
2020-10-25 07:18:48 +00:00
|
|
|
user.FsConfig.AzBlobConfig.AccountKey = currentAzAccountKey
|
|
|
|
}
|
2020-12-12 09:31:09 +00:00
|
|
|
case dataprovider.GCSFilesystemProvider:
|
|
|
|
if user.FsConfig.GCSConfig.Credentials.IsNotPlainAndNotEmpty() {
|
2020-11-22 20:53:04 +00:00
|
|
|
user.FsConfig.GCSConfig.Credentials = currentGCSCredentials
|
|
|
|
}
|
2020-12-12 09:31:09 +00:00
|
|
|
case dataprovider.CryptedFilesystemProvider:
|
|
|
|
if user.FsConfig.CryptConfig.Passphrase.IsNotPlainAndNotEmpty() {
|
2020-12-05 12:48:13 +00:00
|
|
|
user.FsConfig.CryptConfig.Passphrase = currentCryptoPassphrase
|
|
|
|
}
|
2020-12-12 09:31:09 +00:00
|
|
|
case dataprovider.SFTPFilesystemProvider:
|
|
|
|
if user.FsConfig.SFTPConfig.Password.IsNotPlainAndNotEmpty() {
|
|
|
|
user.FsConfig.SFTPConfig.Password = currentSFTPPassword
|
|
|
|
}
|
|
|
|
if user.FsConfig.SFTPConfig.PrivateKey.IsNotPlainAndNotEmpty() {
|
|
|
|
user.FsConfig.SFTPConfig.PrivateKey = currentSFTPKey
|
|
|
|
}
|
2020-12-05 12:48:13 +00:00
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|