-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gTmhUzJpVqGitpd9LjV1ldLZiaTxImZNdcW15gfxjKA=
- Loading branch information
1 parent
92a53e6
commit 601d7ae
Showing
10 changed files
with
916 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package connector | ||
|
||
import ( | ||
"archive/tar" | ||
"crypto" | ||
"crypto/rsa" | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"encoding/json" | ||
|
||
dockerclient "github.com/docker/docker/client" | ||
gliderssh "github.com/gliderlabs/ssh" | ||
"github.com/shellhub-io/shellhub/pkg/agent/server/modes" | ||
"github.com/shellhub-io/shellhub/pkg/api/client" | ||
"github.com/shellhub-io/shellhub/pkg/models" | ||
gossh "golang.org/x/crypto/ssh" | ||
) | ||
|
||
// NOTICE: Ensures the Authenticator interface is implemented. | ||
var _ modes.Authenticator = (*Authenticator)(nil) | ||
|
||
// Authenticator implements the Authenticator interface when the server is running in connector mode. | ||
type Authenticator struct { | ||
// api is a client to communicate with the ShellHub's API. | ||
api client.Client | ||
// authData is the authentication data received from the API to authenticate the device. | ||
authData *models.DeviceAuthResponse | ||
// container is the device name. | ||
// | ||
// NOTICE: Uses a pointer for later assignment. | ||
container *string | ||
docker dockerclient.APIClient | ||
} | ||
|
||
// NewAuthenticator creates a new instance of Authenticator for the connector mode. | ||
func NewAuthenticator(api client.Client, docker dockerclient.APIClient, authData *models.DeviceAuthResponse, container *string) *Authenticator { | ||
return &Authenticator{ | ||
api: api, | ||
authData: authData, | ||
container: container, | ||
docker: docker, | ||
} | ||
} | ||
|
||
// Password handles the server's SSH password authentication when server is running in connector mode. | ||
func (a *Authenticator) Password(ctx gliderssh.Context, username string, password string) bool { | ||
passwdTar, _, _ := a.docker.CopyFromContainer(ctx, *a.container, "/etc/passwd") | ||
passwd := tar.NewReader(passwdTar) | ||
passwd.Next() //nolint:errcheck | ||
|
||
user, err := LookupUserDocker(username, passwd) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
if user.Password == "" { | ||
// NOTICE(r): when the user doesn't have password, we block the login. | ||
return false | ||
} | ||
|
||
shadowTar, _, _ := a.docker.CopyFromContainer(ctx, *a.container, "/etc/shadow") | ||
shadow := tar.NewReader(shadowTar) | ||
shadow.Next() //nolint:errcheck | ||
|
||
return authUser(username, password, shadow) | ||
} | ||
|
||
// PublicKey handles the server's SSH public key authentication when server is running in connector mode. | ||
func (a *Authenticator) PublicKey(ctx gliderssh.Context, username string, key gliderssh.PublicKey) bool { | ||
type Signature struct { | ||
Username string | ||
Namespace string | ||
} | ||
|
||
sig := &Signature{ | ||
Username: username, | ||
Namespace: *a.container, | ||
} | ||
|
||
sigBytes, err := json.Marshal(sig) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
sigHash := sha256.Sum256(sigBytes) | ||
|
||
res, err := a.api.AuthPublicKey(&models.PublicKeyAuthRequest{ | ||
Fingerprint: gossh.FingerprintLegacyMD5(key), | ||
Data: string(sigBytes), | ||
}, a.authData.Token) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
digest, err := base64.StdEncoding.DecodeString(res.Signature) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
cryptoKey, ok := key.(gossh.CryptoPublicKey) | ||
if !ok { | ||
return false | ||
} | ||
|
||
pubCrypto := cryptoKey.CryptoPublicKey() | ||
|
||
pubKey, ok := pubCrypto.(*rsa.PublicKey) | ||
if !ok { | ||
return false | ||
} | ||
|
||
if err = rsa.VerifyPKCS1v15(pubKey, crypto.SHA256, sigHash[:], digest); err != nil { | ||
return false | ||
} | ||
|
||
return true | ||
} |
Oops, something went wrong.