2019-08-01 05:20:19 +08:00
|
|
|
// Package proxy implements a programmable proxy for rclone serve
|
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-11-05 23:18:51 +08:00
|
|
|
"context"
|
2020-01-12 19:36:39 +08:00
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/subtle"
|
2019-08-01 05:20:19 +08:00
|
|
|
"encoding/json"
|
2021-11-04 18:12:57 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-08-01 05:20:19 +08:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/cache"
|
|
|
|
"github.com/rclone/rclone/fs/config/configmap"
|
|
|
|
"github.com/rclone/rclone/fs/config/obscure"
|
|
|
|
libcache "github.com/rclone/rclone/lib/cache"
|
|
|
|
"github.com/rclone/rclone/vfs"
|
|
|
|
"github.com/rclone/rclone/vfs/vfsflags"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Help contains text describing how to use the proxy
|
|
|
|
var Help = strings.Replace(`
|
|
|
|
### Auth Proxy
|
|
|
|
|
|
|
|
If you supply the parameter |--auth-proxy /path/to/program| then
|
|
|
|
rclone will use that program to generate backends on the fly which
|
|
|
|
then are used to authenticate incoming requests. This uses a simple
|
Spelling fixes
Fix spelling of: above, already, anonymous, associated,
authentication, bandwidth, because, between, blocks, calculate,
candidates, cautious, changelog, cleaner, clipboard, command,
completely, concurrently, considered, constructs, corrupt, current,
daemon, dependencies, deprecated, directory, dispatcher, download,
eligible, ellipsis, encrypter, endpoint, entrieslist, essentially,
existing writers, existing, expires, filesystem, flushing, frequently,
hierarchy, however, implementation, implements, inaccurate,
individually, insensitive, longer, maximum, metadata, modified,
multipart, namedirfirst, nextcloud, obscured, opened, optional,
owncloud, pacific, passphrase, password, permanently, persimmon,
positive, potato, protocol, quota, receiving, recommends, referring,
requires, revisited, satisfied, satisfies, satisfy, semver,
serialized, session, storage, strategies, stringlist, successful,
supported, surprise, temporarily, temporary, transactions, unneeded,
update, uploads, wrapped
Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
2020-10-09 08:17:24 +08:00
|
|
|
JSON based protocol with input on STDIN and output on STDOUT.
|
2019-08-01 05:20:19 +08:00
|
|
|
|
2020-02-10 22:31:59 +08:00
|
|
|
**PLEASE NOTE:** |--auth-proxy| and |--authorized-keys| cannot be used
|
|
|
|
together, if |--auth-proxy| is set the authorized keys option will be
|
|
|
|
ignored.
|
|
|
|
|
2019-08-01 05:20:19 +08:00
|
|
|
There is an example program
|
|
|
|
[bin/test_proxy.py](https://github.com/rclone/rclone/blob/master/test_proxy.py)
|
|
|
|
in the rclone source code.
|
|
|
|
|
|
|
|
The program's job is to take a |user| and |pass| on the input and turn
|
|
|
|
those into the config for a backend on STDOUT in JSON format. This
|
|
|
|
config will have any default parameters for the backend added, but it
|
|
|
|
won't use configuration from environment variables or command line
|
|
|
|
options - it is the job of the proxy program to make a complete
|
|
|
|
config.
|
|
|
|
|
|
|
|
This config generated must have this extra parameter
|
|
|
|
- |_root| - root to use for the backend
|
|
|
|
|
|
|
|
And it may have this parameter
|
|
|
|
- |_obscure| - comma separated strings for parameters to obscure
|
|
|
|
|
2020-02-10 22:31:59 +08:00
|
|
|
If password authentication was used by the client, input to the proxy
|
|
|
|
process (on STDIN) would look similar to this:
|
2019-08-01 05:20:19 +08:00
|
|
|
|
|
|
|
|||
|
|
|
|
{
|
|
|
|
"user": "me",
|
|
|
|
"pass": "mypassword"
|
|
|
|
}
|
|
|
|
|||
|
|
|
|
|
2020-02-10 22:31:59 +08:00
|
|
|
If public-key authentication was used by the client, input to the
|
|
|
|
proxy process (on STDIN) would look similar to this:
|
|
|
|
|
|
|
|
|||
|
|
|
|
{
|
|
|
|
"user": "me",
|
|
|
|
"public_key": "AAAAB3NzaC1yc2EAAAADAQABAAABAQDuwESFdAe14hVS6omeyX7edc...JQdf"
|
|
|
|
}
|
|
|
|
|||
|
|
|
|
|
|
|
|
And as an example return this on STDOUT
|
2019-08-01 05:20:19 +08:00
|
|
|
|
|
|
|
|||
|
|
|
|
{
|
|
|
|
"type": "sftp",
|
|
|
|
"_root": "",
|
|
|
|
"_obscure": "pass",
|
|
|
|
"user": "me",
|
|
|
|
"pass": "mypassword",
|
|
|
|
"host": "sftp.example.com"
|
|
|
|
}
|
|
|
|
|||
|
|
|
|
|
|
|
|
This would mean that an SFTP backend would be created on the fly for
|
2020-02-10 22:31:59 +08:00
|
|
|
the |user| and |pass|/|public_key| returned in the output to the host given. Note
|
2019-08-01 05:20:19 +08:00
|
|
|
that since |_obscure| is set to |pass|, rclone will obscure the |pass|
|
|
|
|
parameter before creating the backend (which is required for sftp
|
|
|
|
backends).
|
|
|
|
|
2020-05-20 18:39:20 +08:00
|
|
|
The program can manipulate the supplied |user| in any way, for example
|
2019-08-01 05:20:19 +08:00
|
|
|
to make proxy to many different sftp backends, you could make the
|
|
|
|
|user| be |user@example.com| and then set the |host| to |example.com|
|
|
|
|
in the output and the user to |user|. For security you'd probably want
|
|
|
|
to restrict the |host| to a limited list.
|
|
|
|
|
|
|
|
Note that an internal cache is keyed on |user| so only use that for
|
2020-02-10 22:31:59 +08:00
|
|
|
configuration, don't use |pass| or |public_key|. This also means that if a user's
|
|
|
|
password or public-key is changed the cache will need to expire (which takes 5 mins)
|
2019-08-01 05:20:19 +08:00
|
|
|
before it takes effect.
|
|
|
|
|
|
|
|
This can be used to build general purpose proxies to any kind of
|
|
|
|
backend that rclone supports.
|
|
|
|
`, "|", "`", -1)
|
|
|
|
|
|
|
|
// Options is options for creating the proxy
|
|
|
|
type Options struct {
|
|
|
|
AuthProxy string
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultOpt is the default values uses for Opt
|
|
|
|
var DefaultOpt = Options{
|
|
|
|
AuthProxy: "",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Proxy represents a proxy to turn auth requests into a VFS
|
|
|
|
type Proxy struct {
|
|
|
|
cmdLine []string // broken down command line
|
|
|
|
vfsCache *libcache.Cache
|
2020-11-05 23:18:51 +08:00
|
|
|
ctx context.Context // for global config
|
2019-08-01 05:20:19 +08:00
|
|
|
Opt Options
|
|
|
|
}
|
|
|
|
|
|
|
|
// cacheEntry is what is stored in the vfsCache
|
|
|
|
type cacheEntry struct {
|
2020-01-12 19:36:39 +08:00
|
|
|
vfs *vfs.VFS // stored VFS
|
|
|
|
pwHash [sha256.Size]byte // sha256 hash of the password/publicKey
|
2019-08-01 05:20:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new proxy with the Options passed in
|
2020-11-05 23:18:51 +08:00
|
|
|
func New(ctx context.Context, opt *Options) *Proxy {
|
2019-08-01 05:20:19 +08:00
|
|
|
return &Proxy{
|
2020-11-05 23:18:51 +08:00
|
|
|
ctx: ctx,
|
2019-08-01 05:20:19 +08:00
|
|
|
Opt: *opt,
|
|
|
|
cmdLine: strings.Fields(opt.AuthProxy),
|
|
|
|
vfsCache: libcache.New(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// run the proxy command returning a config map
|
|
|
|
func (p *Proxy) run(in map[string]string) (config configmap.Simple, err error) {
|
|
|
|
cmd := exec.Command(p.cmdLine[0], p.cmdLine[1:]...)
|
|
|
|
inBytes, err := json.MarshalIndent(in, "", "\t")
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return nil, fmt.Errorf("proxy: failed to marshal input: %w", err)
|
2019-08-01 05:20:19 +08:00
|
|
|
}
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
cmd.Stdin = bytes.NewBuffer(inBytes)
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
start := time.Now()
|
|
|
|
err = cmd.Run()
|
|
|
|
fs.Debugf(nil, "Calling proxy %v", p.cmdLine)
|
|
|
|
duration := time.Since(start)
|
|
|
|
if err != nil {
|
2022-06-09 04:25:17 +08:00
|
|
|
return nil, fmt.Errorf("proxy: failed on %v: %q: %w", p.cmdLine, strings.TrimSpace(stderr.String()), err)
|
2019-08-01 05:20:19 +08:00
|
|
|
}
|
|
|
|
err = json.Unmarshal(stdout.Bytes(), &config)
|
|
|
|
if err != nil {
|
2022-06-09 04:25:17 +08:00
|
|
|
return nil, fmt.Errorf("proxy: failed to read output: %q: %w", stdout.String(), err)
|
2019-08-01 05:20:19 +08:00
|
|
|
}
|
|
|
|
fs.Debugf(nil, "Proxy returned in %v", duration)
|
|
|
|
|
|
|
|
// Obscure any values in the config map that need it
|
|
|
|
obscureFields, ok := config.Get("_obscure")
|
|
|
|
if ok {
|
|
|
|
for _, key := range strings.Split(obscureFields, ",") {
|
|
|
|
value, ok := config.Get(key)
|
|
|
|
if ok {
|
|
|
|
obscuredValue, err := obscure.Obscure(value)
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return nil, fmt.Errorf("proxy: %w", err)
|
2019-08-01 05:20:19 +08:00
|
|
|
}
|
|
|
|
config.Set(key, obscuredValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// call runs the auth proxy and returns a cacheEntry and an error
|
2019-12-25 01:34:35 +08:00
|
|
|
func (p *Proxy) call(user, auth string, isPublicKey bool) (value interface{}, err error) {
|
|
|
|
var config configmap.Simple
|
2019-08-01 05:20:19 +08:00
|
|
|
// Contact the proxy
|
2019-12-25 01:34:35 +08:00
|
|
|
if isPublicKey {
|
|
|
|
config, err = p.run(map[string]string{
|
|
|
|
"user": user,
|
|
|
|
"public_key": auth,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
config, err = p.run(map[string]string{
|
|
|
|
"user": user,
|
|
|
|
"pass": auth,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-01 05:20:19 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for required fields in the answer
|
|
|
|
fsName, ok := config.Get("type")
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("proxy: type not set in result")
|
|
|
|
}
|
|
|
|
root, ok := config.Get("_root")
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("proxy: _root not set in result")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the backend
|
|
|
|
fsInfo, err := fs.Find(fsName)
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return nil, fmt.Errorf("proxy: couldn't find backend for %q: %w", fsName, err)
|
2019-08-01 05:20:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// base name of config on user name. This may appear in logs
|
|
|
|
name := "proxy-" + user
|
|
|
|
fsString := name + ":" + root
|
|
|
|
|
|
|
|
// Look for fs in the VFS cache
|
|
|
|
value, err = p.vfsCache.Get(user, func(key string) (value interface{}, ok bool, err error) {
|
|
|
|
// Create the Fs from the cache
|
2020-11-05 23:18:51 +08:00
|
|
|
f, err := cache.GetFn(p.ctx, fsString, func(ctx context.Context, fsString string) (fs.Fs, error) {
|
2019-08-01 05:20:19 +08:00
|
|
|
// Update the config with the default values
|
|
|
|
for i := range fsInfo.Options {
|
|
|
|
o := &fsInfo.Options[i]
|
|
|
|
if _, found := config.Get(o.Name); !found && o.Default != nil && o.String() != "" {
|
|
|
|
config.Set(o.Name, o.String())
|
|
|
|
}
|
|
|
|
}
|
2020-11-05 23:18:51 +08:00
|
|
|
return fsInfo.NewFs(ctx, name, root, config)
|
2019-08-01 05:20:19 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
2019-12-25 01:34:35 +08:00
|
|
|
|
2020-01-12 19:36:39 +08:00
|
|
|
// We hash the auth here so we don't copy the auth more than we
|
|
|
|
// need to in memory. An attacker would find it easier to go
|
|
|
|
// after the unencrypted password in memory most likely.
|
2019-08-01 05:20:19 +08:00
|
|
|
entry := cacheEntry{
|
|
|
|
vfs: vfs.New(f, &vfsflags.Opt),
|
2020-01-12 19:36:39 +08:00
|
|
|
pwHash: sha256.Sum256([]byte(auth)),
|
2019-08-01 05:20:19 +08:00
|
|
|
}
|
|
|
|
return entry, true, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return nil, fmt.Errorf("proxy: failed to create backend: %w", err)
|
2019-08-01 05:20:19 +08:00
|
|
|
}
|
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
|
2019-12-25 01:34:35 +08:00
|
|
|
// Call runs the auth proxy with the username and password/public key provided
|
|
|
|
// returning a *vfs.VFS and the key used in the VFS cache.
|
|
|
|
func (p *Proxy) Call(user, auth string, isPublicKey bool) (VFS *vfs.VFS, vfsKey string, err error) {
|
2019-08-01 05:20:19 +08:00
|
|
|
// Look in the cache first
|
|
|
|
value, ok := p.vfsCache.GetMaybe(user)
|
|
|
|
|
|
|
|
// If not found then call the proxy for a fresh answer
|
|
|
|
if !ok {
|
2019-12-25 01:34:35 +08:00
|
|
|
value, err = p.call(user, auth, isPublicKey)
|
2019-08-01 05:20:19 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check we got what we were expecting
|
|
|
|
entry, ok := value.(cacheEntry)
|
|
|
|
if !ok {
|
2021-11-04 18:12:57 +08:00
|
|
|
return nil, "", fmt.Errorf("proxy: value is not cache entry: %#v", value)
|
2019-08-01 05:20:19 +08:00
|
|
|
}
|
|
|
|
|
2019-12-25 01:34:35 +08:00
|
|
|
// Check the password / public key is correct in the cached entry. This
|
2019-08-01 05:20:19 +08:00
|
|
|
// prevents an attack where subsequent requests for the same
|
|
|
|
// user don't have their auth checked. It does mean that if
|
|
|
|
// the password is changed, the user will have to wait for
|
|
|
|
// cache expiry (5m) before trying again.
|
2020-01-12 19:36:39 +08:00
|
|
|
authHash := sha256.Sum256([]byte(auth))
|
|
|
|
if subtle.ConstantTimeCompare(authHash[:], entry.pwHash[:]) != 1 {
|
|
|
|
if isPublicKey {
|
|
|
|
return nil, "", errors.New("proxy: incorrect public key")
|
|
|
|
}
|
|
|
|
return nil, "", errors.New("proxy: incorrect password")
|
2019-08-01 05:20:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return entry.vfs, user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get VFS from the cache using key - returns nil if not found
|
|
|
|
func (p *Proxy) Get(key string) *vfs.VFS {
|
|
|
|
value, ok := p.vfsCache.GetMaybe(key)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
entry := value.(cacheEntry)
|
|
|
|
return entry.vfs
|
|
|
|
}
|