2022-07-17 18:16:00 +00:00
|
|
|
// Copyright (C) 2019-2022 Nicola Murino
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
package httpd
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
import (
|
2021-01-17 21:29:08 +00:00
|
|
|
"context"
|
2020-09-01 14:10:26 +00:00
|
|
|
"fmt"
|
2019-07-20 10:26:52 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2022-11-05 17:01:24 +00:00
|
|
|
"time"
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
"github.com/go-chi/render"
|
2022-01-06 10:54:43 +00:00
|
|
|
"github.com/sftpgo/sdk"
|
2020-05-06 17:36:34 +00:00
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/common"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/kms"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/smtp"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/vfs"
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func getUsers(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-02-01 18:04:15 +00:00
|
|
|
limit, offset, order, err := getSearchFilters(w, r)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2022-11-16 18:04:50 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
|
2022-11-16 18:04:50 +00:00
|
|
|
users, err := dataprovider.GetUsers(limit, offset, order, claims.Role)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err == nil {
|
|
|
|
render.JSON(w, r, users)
|
|
|
|
} else {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func getUserByUsername(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2022-11-16 18:04:50 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
username := getURLParam(r, "username")
|
2022-11-16 18:04:50 +00:00
|
|
|
renderUser(w, r, username, claims.Role, http.StatusOK)
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 18:04:50 +00:00
|
|
|
func renderUser(w http.ResponseWriter, r *http.Request, username, role string, status int) {
|
|
|
|
user, err := dataprovider.UserExists(username, role)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2021-01-17 21:29:08 +00:00
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
2019-07-20 10:26:52 +00:00
|
|
|
return
|
|
|
|
}
|
2021-03-22 18:03:25 +00:00
|
|
|
user.PrepareForRendering()
|
2021-01-17 21:29:08 +00:00
|
|
|
if status != http.StatusOK {
|
2021-02-01 18:04:15 +00:00
|
|
|
ctx := context.WithValue(r.Context(), render.StatusCtxKey, status)
|
2021-01-17 21:29:08 +00:00
|
|
|
render.JSON(w, r.WithContext(ctx), user)
|
2019-07-20 10:26:52 +00:00
|
|
|
} else {
|
2021-01-17 21:29:08 +00:00
|
|
|
render.JSON(w, r, user)
|
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)
|
2021-10-10 11:08:05 +00:00
|
|
|
|
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2022-11-05 17:01:24 +00:00
|
|
|
admin, err := dataprovider.AdminExists(claims.Username)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
var user dataprovider.User
|
2022-11-05 17:01:24 +00:00
|
|
|
if admin.Filters.Preferences.DefaultUsersExpiration > 0 {
|
|
|
|
user.ExpirationDate = util.GetTimeAsMsSinceEpoch(time.Now().Add(24 * time.Hour * time.Duration(admin.Filters.Preferences.DefaultUsersExpiration)))
|
|
|
|
}
|
2021-10-10 11:08:05 +00:00
|
|
|
err = render.DecodeJSON(r.Body, &user)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2022-11-16 18:04:50 +00:00
|
|
|
if claims.Role != "" {
|
|
|
|
user.Role = claims.Role
|
|
|
|
}
|
2022-12-11 16:15:34 +00:00
|
|
|
user.LastPasswordChange = 0
|
2022-11-16 18:04:50 +00:00
|
|
|
user.Filters.RecoveryCodes = nil
|
|
|
|
user.Filters.TOTPConfig = dataprovider.UserTOTPConfig{
|
|
|
|
Enabled: false,
|
|
|
|
}
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.AddUser(&user, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr), claims.Role)
|
2021-01-17 21:29:08 +00:00
|
|
|
if err != nil {
|
2019-07-20 10:26:52 +00:00
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
2021-01-17 21:29:08 +00:00
|
|
|
return
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2022-12-23 12:08:04 +00:00
|
|
|
w.Header().Add("Location", fmt.Sprintf("%s/%s", userPath, user.Username))
|
2022-11-16 18:04:50 +00:00
|
|
|
renderUser(w, r, user.Username, claims.Role, http.StatusCreated)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 10:11:04 +00:00
|
|
|
func disableUser2FA(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-10-10 11:08:05 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
username := getURLParam(r, "username")
|
2022-11-16 18:04:50 +00:00
|
|
|
user, err := dataprovider.UserExists(username, claims.Role)
|
2021-09-04 10:11:04 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
user.Filters.RecoveryCodes = nil
|
2022-01-06 09:11:47 +00:00
|
|
|
user.Filters.TOTPConfig = dataprovider.UserTOTPConfig{
|
2021-09-04 10:11:04 +00:00
|
|
|
Enabled: false,
|
|
|
|
}
|
2022-12-03 10:45:27 +00:00
|
|
|
if err := dataprovider.UpdateUser(&user, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr), claims.Role); err != nil {
|
2021-09-04 10:11:04 +00:00
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sendAPIResponse(w, r, nil, "2FA disabled", http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
func updateUser(w http.ResponseWriter, r *http.Request) {
|
2020-01-31 18:04:00 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-10-10 11:08:05 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
|
|
|
|
username := getURLParam(r, "username")
|
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
|
|
|
|
}
|
|
|
|
}
|
2022-11-16 18:04:50 +00:00
|
|
|
user, err := dataprovider.UserExists(username, claims.Role)
|
2020-06-20 10:38:04 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
2020-12-05 12:48:13 +00:00
|
|
|
|
2022-12-23 08:36:20 +00:00
|
|
|
var updatedUser dataprovider.User
|
|
|
|
updatedUser.Password = user.Password
|
|
|
|
err = render.DecodeJSON(r.Body, &updatedUser)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2022-12-23 08:36:20 +00:00
|
|
|
updatedUser.ID = user.ID
|
|
|
|
updatedUser.Username = user.Username
|
|
|
|
updatedUser.Filters.RecoveryCodes = user.Filters.RecoveryCodes
|
|
|
|
updatedUser.Filters.TOTPConfig = user.Filters.TOTPConfig
|
|
|
|
updatedUser.LastPasswordChange = user.LastPasswordChange
|
|
|
|
updatedUser.SetEmptySecretsIfNil()
|
|
|
|
updateEncryptedSecrets(&updatedUser.FsConfig, user.FsConfig.S3Config.AccessSecret, user.FsConfig.AzBlobConfig.AccountKey,
|
|
|
|
user.FsConfig.AzBlobConfig.SASURL, user.FsConfig.GCSConfig.Credentials, user.FsConfig.CryptConfig.Passphrase,
|
|
|
|
user.FsConfig.SFTPConfig.Password, user.FsConfig.SFTPConfig.PrivateKey, user.FsConfig.SFTPConfig.KeyPassphrase,
|
|
|
|
user.FsConfig.HTTPConfig.Password, user.FsConfig.HTTPConfig.APIKey)
|
2022-11-16 18:04:50 +00:00
|
|
|
if claims.Role != "" {
|
2022-12-23 08:36:20 +00:00
|
|
|
updatedUser.Role = claims.Role
|
2022-11-16 18:04:50 +00:00
|
|
|
}
|
2022-12-23 08:36:20 +00:00
|
|
|
err = dataprovider.UpdateUser(&updatedUser, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr), claims.Role)
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
2021-01-17 21:29:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
sendAPIResponse(w, r, err, "User updated", http.StatusOK)
|
|
|
|
if disconnect == 1 {
|
|
|
|
disconnectUser(user.Username)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteUser(w http.ResponseWriter, r *http.Request) {
|
2021-08-17 16:08:32 +00:00
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-10-10 11:08:05 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
username := getURLParam(r, "username")
|
2022-11-16 18:04:50 +00:00
|
|
|
err = dataprovider.DeleteUser(username, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr), claims.Role)
|
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-17 21:29:08 +00:00
|
|
|
sendAPIResponse(w, r, err, "User deleted", http.StatusOK)
|
2022-01-31 17:01:37 +00:00
|
|
|
disconnectUser(dataprovider.ConvertName(username))
|
2020-09-01 14:10:26 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 12:25:43 +00:00
|
|
|
func forgotUserPassword(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
|
|
|
|
if !smtp.IsEnabled() {
|
|
|
|
sendAPIResponse(w, r, nil, "No SMTP configuration", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := handleForgotPassword(r, getURLParam(r, "username"), false)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sendAPIResponse(w, r, err, "Check your email for the confirmation code", http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resetUserPassword(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
|
|
|
|
var req pwdReset
|
|
|
|
err := render.DecodeJSON(r.Body, &req)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, _, err = handleResetPassword(r, req.Code, req.Password, false)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sendAPIResponse(w, r, err, "Password reset successful", http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2020-09-01 14:10:26 +00:00
|
|
|
func disconnectUser(username string) {
|
2022-11-16 18:04:50 +00:00
|
|
|
for _, stat := range common.Connections.GetStats("") {
|
2020-09-01 14:10:26 +00:00
|
|
|
if stat.Username == username {
|
2022-11-16 18:04:50 +00:00
|
|
|
common.Connections.Close(stat.ConnectionID, "")
|
2020-09-01 14:10:26 +00:00
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
|
2021-06-11 20:27:36 +00:00
|
|
|
func updateEncryptedSecrets(fsConfig *vfs.Filesystem, currentS3AccessSecret, currentAzAccountKey, currentAzSASUrl,
|
2022-06-11 08:41:34 +00:00
|
|
|
currentGCSCredentials, currentCryptoPassphrase, currentSFTPPassword, currentSFTPKey, currentSFTPKeyPassphrase,
|
|
|
|
currentHTTPPassword, currentHTTPAPIKey *kms.Secret) {
|
2020-11-22 20:53:04 +00:00
|
|
|
// we use the new access secret if plain or empty, otherwise the old value
|
2021-03-21 18:15:47 +00:00
|
|
|
switch fsConfig.Provider {
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.S3FilesystemProvider:
|
2021-03-21 18:15:47 +00:00
|
|
|
if fsConfig.S3Config.AccessSecret.IsNotPlainAndNotEmpty() {
|
|
|
|
fsConfig.S3Config.AccessSecret = currentS3AccessSecret
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.AzureBlobFilesystemProvider:
|
2021-03-21 18:15:47 +00:00
|
|
|
if fsConfig.AzBlobConfig.AccountKey.IsNotPlainAndNotEmpty() {
|
|
|
|
fsConfig.AzBlobConfig.AccountKey = currentAzAccountKey
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
2021-06-11 20:27:36 +00:00
|
|
|
if fsConfig.AzBlobConfig.SASURL.IsNotPlainAndNotEmpty() {
|
|
|
|
fsConfig.AzBlobConfig.SASURL = currentAzSASUrl
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.GCSFilesystemProvider:
|
2021-11-15 18:12:58 +00:00
|
|
|
// for GCS credentials will be cleared if we enable automatic credentials
|
|
|
|
// so keep the old credentials here if no new credentials are provided
|
|
|
|
if !fsConfig.GCSConfig.Credentials.IsPlain() {
|
2021-03-21 18:15:47 +00:00
|
|
|
fsConfig.GCSConfig.Credentials = currentGCSCredentials
|
2020-11-22 20:53:04 +00:00
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.CryptedFilesystemProvider:
|
2021-03-21 18:15:47 +00:00
|
|
|
if fsConfig.CryptConfig.Passphrase.IsNotPlainAndNotEmpty() {
|
|
|
|
fsConfig.CryptConfig.Passphrase = currentCryptoPassphrase
|
2020-12-05 12:48:13 +00:00
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.SFTPFilesystemProvider:
|
2022-06-11 08:41:34 +00:00
|
|
|
updateSFTPFsEncryptedSecrets(fsConfig, currentSFTPPassword, currentSFTPKey, currentSFTPKeyPassphrase)
|
|
|
|
case sdk.HTTPFilesystemProvider:
|
|
|
|
updateHTTPFsEncryptedSecrets(fsConfig, currentHTTPPassword, currentHTTPAPIKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateSFTPFsEncryptedSecrets(fsConfig *vfs.Filesystem, currentSFTPPassword, currentSFTPKey,
|
|
|
|
currentSFTPKeyPassphrase *kms.Secret,
|
|
|
|
) {
|
|
|
|
if fsConfig.SFTPConfig.Password.IsNotPlainAndNotEmpty() {
|
|
|
|
fsConfig.SFTPConfig.Password = currentSFTPPassword
|
|
|
|
}
|
|
|
|
if fsConfig.SFTPConfig.PrivateKey.IsNotPlainAndNotEmpty() {
|
|
|
|
fsConfig.SFTPConfig.PrivateKey = currentSFTPKey
|
|
|
|
}
|
|
|
|
if fsConfig.SFTPConfig.KeyPassphrase.IsNotPlainAndNotEmpty() {
|
|
|
|
fsConfig.SFTPConfig.KeyPassphrase = currentSFTPKeyPassphrase
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateHTTPFsEncryptedSecrets(fsConfig *vfs.Filesystem, currentHTTPPassword, currentHTTPAPIKey *kms.Secret) {
|
|
|
|
if fsConfig.HTTPConfig.Password.IsNotPlainAndNotEmpty() {
|
|
|
|
fsConfig.HTTPConfig.Password = currentHTTPPassword
|
|
|
|
}
|
|
|
|
if fsConfig.HTTPConfig.APIKey.IsNotPlainAndNotEmpty() {
|
|
|
|
fsConfig.HTTPConfig.APIKey = currentHTTPAPIKey
|
2020-12-05 12:48:13 +00:00
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|