2014-03-16 14:54:13 +05:30
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2019-06-16 13:20:46 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2014-03-16 14:54:13 +05:30
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-02-17 21:27:23 +05:30
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-03-16 14:54:13 +05:30
|
|
|
"strings"
|
2014-02-17 21:27:23 +05:30
|
|
|
"time"
|
2014-03-03 01:55:09 +05:30
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-09-24 17:02:56 +05:30
|
|
|
"code.gitea.io/gitea/models/login"
|
2016-11-10 21:54:48 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-08-15 20:16:21 +05:30
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2020-08-12 01:35:34 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2018-09-13 08:03:48 +05:30
|
|
|
"golang.org/x/crypto/ssh"
|
2014-02-17 21:27:23 +05:30
|
|
|
|
2021-07-24 15:46:34 +05:30
|
|
|
"xorm.io/builder"
|
2014-03-17 23:33:58 +05:30
|
|
|
)
|
|
|
|
|
2016-11-26 06:06:03 +05:30
|
|
|
// KeyType specifies the key type
|
2015-08-06 20:18:11 +05:30
|
|
|
type KeyType int
|
|
|
|
|
|
|
|
const (
|
2016-11-26 06:06:03 +05:30
|
|
|
// KeyTypeUser specifies the user key
|
2016-11-07 22:23:22 +05:30
|
|
|
KeyTypeUser = iota + 1
|
2016-11-26 06:06:03 +05:30
|
|
|
// KeyTypeDeploy specifies the deploy key
|
2016-11-07 22:23:22 +05:30
|
|
|
KeyTypeDeploy
|
2020-10-11 06:08:09 +05:30
|
|
|
// KeyTypePrincipal specifies the authorized principal key
|
|
|
|
KeyTypePrincipal
|
2015-08-06 20:18:11 +05:30
|
|
|
)
|
|
|
|
|
2016-07-26 08:17:25 +05:30
|
|
|
// PublicKey represents a user or deploy SSH public key.
|
2014-02-17 21:27:23 +05:30
|
|
|
type PublicKey struct {
|
2018-05-24 10:29:02 +05:30
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
OwnerID int64 `xorm:"INDEX NOT NULL"`
|
|
|
|
Name string `xorm:"NOT NULL"`
|
2019-02-04 05:26:53 +05:30
|
|
|
Fingerprint string `xorm:"INDEX NOT NULL"`
|
2018-05-24 10:29:02 +05:30
|
|
|
Content string `xorm:"TEXT NOT NULL"`
|
|
|
|
Mode AccessMode `xorm:"NOT NULL DEFAULT 2"`
|
|
|
|
Type KeyType `xorm:"NOT NULL DEFAULT 1"`
|
|
|
|
LoginSourceID int64 `xorm:"NOT NULL DEFAULT 0"`
|
2016-03-10 06:23:30 +05:30
|
|
|
|
2019-08-15 20:16:21 +05:30
|
|
|
CreatedUnix timeutil.TimeStamp `xorm:"created"`
|
|
|
|
UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
|
|
|
|
HasRecentActivity bool `xorm:"-"`
|
|
|
|
HasUsed bool `xorm:"-"`
|
2015-08-06 20:18:11 +05:30
|
|
|
}
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(PublicKey))
|
|
|
|
}
|
|
|
|
|
2017-10-01 22:22:35 +05:30
|
|
|
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
|
|
|
|
func (key *PublicKey) AfterLoad() {
|
2017-12-11 10:07:04 +05:30
|
|
|
key.HasUsed = key.UpdatedUnix > key.CreatedUnix
|
2019-08-15 20:16:21 +05:30
|
|
|
key.HasRecentActivity = key.UpdatedUnix.AddDuration(7*24*time.Hour) > timeutil.TimeStampNow()
|
2014-02-17 21:27:23 +05:30
|
|
|
}
|
|
|
|
|
2016-07-26 08:17:25 +05:30
|
|
|
// OmitEmail returns content of public key without email address.
|
2016-11-26 06:06:03 +05:30
|
|
|
func (key *PublicKey) OmitEmail() string {
|
|
|
|
return strings.Join(strings.Split(key.Content, " ")[:2], " ")
|
2014-11-23 13:03:47 +05:30
|
|
|
}
|
|
|
|
|
2016-07-26 08:17:25 +05:30
|
|
|
// AuthorizedString returns formatted public key string for authorized_keys file.
|
2021-07-24 15:46:34 +05:30
|
|
|
//
|
|
|
|
// TODO: Consider dropping this function
|
2016-07-26 08:17:25 +05:30
|
|
|
func (key *PublicKey) AuthorizedString() string {
|
2021-07-24 15:46:34 +05:30
|
|
|
return AuthorizedStringForKey(key)
|
2019-06-16 13:20:46 +05:30
|
|
|
}
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
func addKey(e db.Engine, key *PublicKey) (err error) {
|
2018-10-21 02:55:14 +05:30
|
|
|
if len(key.Fingerprint) == 0 {
|
2017-02-14 11:42:52 +05:30
|
|
|
key.Fingerprint, err = calcFingerprint(key.Content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-03-16 15:46:03 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Save SSH key.
|
2015-08-06 20:18:11 +05:30
|
|
|
if _, err = e.Insert(key); err != nil {
|
2014-03-16 15:46:03 +05:30
|
|
|
return err
|
2015-08-06 20:18:11 +05:30
|
|
|
}
|
2015-12-11 15:32:33 +05:30
|
|
|
|
2016-07-26 08:17:25 +05:30
|
|
|
return appendAuthorizedKeysToFile(key)
|
2015-08-06 20:18:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// AddPublicKey adds new public key to database and authorized_keys file.
|
2019-06-13 01:11:28 +05:30
|
|
|
func AddPublicKey(ownerID int64, name, content string, loginSourceID int64) (*PublicKey, error) {
|
2016-02-17 03:31:56 +05:30
|
|
|
log.Trace(content)
|
2017-02-14 11:42:52 +05:30
|
|
|
|
|
|
|
fingerprint, err := calcFingerprint(content)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-23 21:15:36 +05:30
|
|
|
sess := db.NewSession(db.DefaultContext)
|
2019-02-04 05:26:53 +05:30
|
|
|
defer sess.Close()
|
|
|
|
if err = sess.Begin(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := checkKeyFingerprint(sess, fingerprint); err != nil {
|
2015-12-03 10:54:37 +05:30
|
|
|
return nil, err
|
2014-02-17 21:27:23 +05:30
|
|
|
}
|
|
|
|
|
2015-08-06 20:18:11 +05:30
|
|
|
// Key name of same user cannot be duplicated.
|
2019-02-04 05:26:53 +05:30
|
|
|
has, err := sess.
|
2016-11-10 20:46:32 +05:30
|
|
|
Where("owner_id = ? AND name = ?", ownerID, name).
|
|
|
|
Get(new(PublicKey))
|
2015-08-06 20:18:11 +05:30
|
|
|
if err != nil {
|
2015-12-03 10:54:37 +05:30
|
|
|
return nil, err
|
2015-08-06 20:18:11 +05:30
|
|
|
} else if has {
|
2015-12-03 10:54:37 +05:30
|
|
|
return nil, ErrKeyNameAlreadyUsed{ownerID, name}
|
2015-08-06 20:18:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
key := &PublicKey{
|
2018-05-24 10:29:02 +05:30
|
|
|
OwnerID: ownerID,
|
|
|
|
Name: name,
|
|
|
|
Fingerprint: fingerprint,
|
|
|
|
Content: content,
|
|
|
|
Mode: AccessModeWrite,
|
|
|
|
Type: KeyTypeUser,
|
2019-06-13 01:11:28 +05:30
|
|
|
LoginSourceID: loginSourceID,
|
2015-08-06 20:18:11 +05:30
|
|
|
}
|
|
|
|
if err = addKey(sess, key); err != nil {
|
2015-12-03 10:54:37 +05:30
|
|
|
return nil, fmt.Errorf("addKey: %v", err)
|
2015-08-06 20:18:11 +05:30
|
|
|
}
|
|
|
|
|
2015-12-03 10:54:37 +05:30
|
|
|
return key, sess.Commit()
|
2014-02-17 21:27:23 +05:30
|
|
|
}
|
|
|
|
|
2015-08-06 20:18:11 +05:30
|
|
|
// GetPublicKeyByID returns public key by given ID.
|
|
|
|
func GetPublicKeyByID(keyID int64) (*PublicKey, error) {
|
2014-08-10 04:10:10 +05:30
|
|
|
key := new(PublicKey)
|
2021-09-23 21:15:36 +05:30
|
|
|
has, err := db.GetEngine(db.DefaultContext).
|
2020-03-22 20:42:55 +05:30
|
|
|
ID(keyID).
|
2016-11-10 20:46:32 +05:30
|
|
|
Get(key)
|
2014-08-10 04:10:10 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
2015-08-06 20:18:11 +05:30
|
|
|
return nil, ErrKeyNotExist{keyID}
|
2014-08-10 04:10:10 +05:30
|
|
|
}
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
func searchPublicKeyByContentWithEngine(e db.Engine, content string) (*PublicKey, error) {
|
2015-11-09 03:29:56 +05:30
|
|
|
key := new(PublicKey)
|
2018-12-27 22:58:48 +05:30
|
|
|
has, err := e.
|
2016-11-10 20:46:32 +05:30
|
|
|
Where("content like ?", content+"%").
|
|
|
|
Get(key)
|
2015-11-09 03:29:56 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
|
|
|
return nil, ErrKeyNotExist{}
|
|
|
|
}
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
2018-12-27 22:58:48 +05:30
|
|
|
// SearchPublicKeyByContent searches content as prefix (leak e-mail part)
|
|
|
|
// and returns public key found.
|
|
|
|
func SearchPublicKeyByContent(content string) (*PublicKey, error) {
|
2021-09-23 21:15:36 +05:30
|
|
|
return searchPublicKeyByContentWithEngine(db.GetEngine(db.DefaultContext), content)
|
2018-12-27 22:58:48 +05:30
|
|
|
}
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
func searchPublicKeyByContentExactWithEngine(e db.Engine, content string) (*PublicKey, error) {
|
2020-10-11 06:08:09 +05:30
|
|
|
key := new(PublicKey)
|
|
|
|
has, err := e.
|
|
|
|
Where("content = ?", content).
|
|
|
|
Get(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
|
|
|
return nil, ErrKeyNotExist{}
|
|
|
|
}
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SearchPublicKeyByContentExact searches content
|
|
|
|
// and returns public key found.
|
|
|
|
func SearchPublicKeyByContentExact(content string) (*PublicKey, error) {
|
2021-09-23 21:15:36 +05:30
|
|
|
return searchPublicKeyByContentExactWithEngine(db.GetEngine(db.DefaultContext), content)
|
2020-10-11 06:08:09 +05:30
|
|
|
}
|
|
|
|
|
2018-11-01 09:10:49 +05:30
|
|
|
// SearchPublicKey returns a list of public keys matching the provided arguments.
|
|
|
|
func SearchPublicKey(uid int64, fingerprint string) ([]*PublicKey, error) {
|
|
|
|
keys := make([]*PublicKey, 0, 5)
|
|
|
|
cond := builder.NewCond()
|
|
|
|
if uid != 0 {
|
|
|
|
cond = cond.And(builder.Eq{"owner_id": uid})
|
|
|
|
}
|
|
|
|
if fingerprint != "" {
|
|
|
|
cond = cond.And(builder.Eq{"fingerprint": fingerprint})
|
|
|
|
}
|
2021-09-23 21:15:36 +05:30
|
|
|
return keys, db.GetEngine(db.DefaultContext).Where(cond).Find(&keys)
|
2018-11-01 09:10:49 +05:30
|
|
|
}
|
|
|
|
|
2014-11-12 17:18:50 +05:30
|
|
|
// ListPublicKeys returns a list of public keys belongs to given user.
|
2021-09-24 17:02:56 +05:30
|
|
|
func ListPublicKeys(uid int64, listOptions db.ListOptions) ([]*PublicKey, error) {
|
2021-09-23 21:15:36 +05:30
|
|
|
sess := db.GetEngine(db.DefaultContext).Where("owner_id = ? AND type != ?", uid, KeyTypePrincipal)
|
2020-01-25 00:30:29 +05:30
|
|
|
if listOptions.Page != 0 {
|
2021-09-24 17:02:56 +05:30
|
|
|
sess = db.SetSessionPagination(sess, &listOptions)
|
2020-01-25 00:30:29 +05:30
|
|
|
|
|
|
|
keys := make([]*PublicKey, 0, listOptions.PageSize)
|
|
|
|
return keys, sess.Find(&keys)
|
|
|
|
}
|
|
|
|
|
2014-07-26 09:54:27 +05:30
|
|
|
keys := make([]*PublicKey, 0, 5)
|
2020-01-25 00:30:29 +05:30
|
|
|
return keys, sess.Find(&keys)
|
2014-05-07 21:39:30 +05:30
|
|
|
}
|
|
|
|
|
2021-08-12 18:13:08 +05:30
|
|
|
// CountPublicKeys count public keys a user has
|
|
|
|
func CountPublicKeys(userID int64) (int64, error) {
|
2021-09-23 21:15:36 +05:30
|
|
|
sess := db.GetEngine(db.DefaultContext).Where("owner_id = ? AND type != ?", userID, KeyTypePrincipal)
|
2021-08-12 18:13:08 +05:30
|
|
|
return sess.Count(&PublicKey{})
|
|
|
|
}
|
|
|
|
|
2021-07-24 15:46:34 +05:30
|
|
|
// ListPublicKeysBySource returns a list of synchronized public keys for a given user and login source.
|
|
|
|
func ListPublicKeysBySource(uid, loginSourceID int64) ([]*PublicKey, error) {
|
2018-05-24 10:29:02 +05:30
|
|
|
keys := make([]*PublicKey, 0, 5)
|
2021-09-23 21:15:36 +05:30
|
|
|
return keys, db.GetEngine(db.DefaultContext).
|
2019-06-13 01:11:28 +05:30
|
|
|
Where("owner_id = ? AND login_source_id = ?", uid, loginSourceID).
|
2018-05-24 10:29:02 +05:30
|
|
|
Find(&keys)
|
|
|
|
}
|
|
|
|
|
2017-04-08 06:10:38 +05:30
|
|
|
// UpdatePublicKeyUpdated updates public key use time.
|
|
|
|
func UpdatePublicKeyUpdated(id int64) error {
|
2017-07-20 08:45:10 +05:30
|
|
|
// Check if key exists before update as affected rows count is unreliable
|
|
|
|
// and will return 0 affected rows if two updates are made at the same time
|
2021-09-23 21:15:36 +05:30
|
|
|
if cnt, err := db.GetEngine(db.DefaultContext).ID(id).Count(&PublicKey{}); err != nil {
|
2017-07-20 08:45:10 +05:30
|
|
|
return err
|
|
|
|
} else if cnt != 1 {
|
|
|
|
return ErrKeyNotExist{id}
|
|
|
|
}
|
|
|
|
|
2021-09-23 21:15:36 +05:30
|
|
|
_, err := db.GetEngine(db.DefaultContext).ID(id).Cols("updated_unix").Update(&PublicKey{
|
2019-08-15 20:16:21 +05:30
|
|
|
UpdatedUnix: timeutil.TimeStampNow(),
|
2017-04-08 06:10:38 +05:30
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-26 14:56:48 +05:30
|
|
|
// deletePublicKeys does the actual key deletion but does not update authorized_keys file.
|
2021-09-19 17:19:59 +05:30
|
|
|
func deletePublicKeys(e db.Engine, keyIDs ...int64) error {
|
2016-07-26 14:56:48 +05:30
|
|
|
if len(keyIDs) == 0 {
|
2015-08-06 20:18:11 +05:30
|
|
|
return nil
|
2014-03-22 23:57:03 +05:30
|
|
|
}
|
2014-05-07 01:58:52 +05:30
|
|
|
|
2016-11-12 13:59:18 +05:30
|
|
|
_, err := e.In("id", keyIDs).Delete(new(PublicKey))
|
2016-07-26 14:56:48 +05:30
|
|
|
return err
|
2014-02-17 21:27:23 +05:30
|
|
|
}
|
2014-12-31 23:37:51 +05:30
|
|
|
|
2020-12-26 09:54:47 +05:30
|
|
|
// PublicKeysAreExternallyManaged returns whether the provided KeyID represents an externally managed Key
|
|
|
|
func PublicKeysAreExternallyManaged(keys []*PublicKey) ([]bool, error) {
|
2021-09-24 17:02:56 +05:30
|
|
|
sources := make([]*login.Source, 0, 5)
|
2020-12-26 09:54:47 +05:30
|
|
|
externals := make([]bool, len(keys))
|
|
|
|
keyloop:
|
|
|
|
for i, key := range keys {
|
|
|
|
if key.LoginSourceID == 0 {
|
|
|
|
externals[i] = false
|
|
|
|
continue keyloop
|
|
|
|
}
|
|
|
|
|
2021-09-24 17:02:56 +05:30
|
|
|
var source *login.Source
|
2020-12-26 09:54:47 +05:30
|
|
|
|
|
|
|
sourceloop:
|
|
|
|
for _, s := range sources {
|
|
|
|
if s.ID == key.LoginSourceID {
|
|
|
|
source = s
|
|
|
|
break sourceloop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if source == nil {
|
|
|
|
var err error
|
2021-09-24 17:02:56 +05:30
|
|
|
source, err = login.GetSourceByID(key.LoginSourceID)
|
2020-12-26 09:54:47 +05:30
|
|
|
if err != nil {
|
2021-09-24 17:02:56 +05:30
|
|
|
if login.IsErrSourceNotExist(err) {
|
2020-12-26 09:54:47 +05:30
|
|
|
externals[i] = false
|
2021-09-24 17:02:56 +05:30
|
|
|
sources[i] = &login.Source{
|
2020-12-26 09:54:47 +05:30
|
|
|
ID: key.LoginSourceID,
|
|
|
|
}
|
|
|
|
continue keyloop
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-24 17:02:56 +05:30
|
|
|
if sshKeyProvider, ok := source.Cfg.(login.SSHKeyProvider); ok && sshKeyProvider.ProvidesSSHKeys() {
|
2020-12-26 09:54:47 +05:30
|
|
|
// Disable setting SSH keys for this user
|
|
|
|
externals[i] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return externals, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PublicKeyIsExternallyManaged returns whether the provided KeyID represents an externally managed Key
|
|
|
|
func PublicKeyIsExternallyManaged(id int64) (bool, error) {
|
|
|
|
key, err := GetPublicKeyByID(id)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if key.LoginSourceID == 0 {
|
|
|
|
return false, nil
|
|
|
|
}
|
2021-09-24 17:02:56 +05:30
|
|
|
source, err := login.GetSourceByID(key.LoginSourceID)
|
2020-12-26 09:54:47 +05:30
|
|
|
if err != nil {
|
2021-09-24 17:02:56 +05:30
|
|
|
if login.IsErrSourceNotExist(err) {
|
2020-12-26 09:54:47 +05:30
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
2021-09-24 17:02:56 +05:30
|
|
|
if sshKeyProvider, ok := source.Cfg.(login.SSHKeyProvider); ok && sshKeyProvider.ProvidesSSHKeys() {
|
2020-12-26 09:54:47 +05:30
|
|
|
// Disable setting SSH keys for this user
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2015-08-06 20:18:11 +05:30
|
|
|
// DeletePublicKey deletes SSH key information both in database and authorized_keys file.
|
2015-12-03 10:54:37 +05:30
|
|
|
func DeletePublicKey(doer *User, id int64) (err error) {
|
|
|
|
key, err := GetPublicKeyByID(id)
|
2015-08-20 14:41:29 +05:30
|
|
|
if err != nil {
|
2017-12-06 15:57:10 +05:30
|
|
|
return err
|
2015-12-03 10:54:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Check if user has access to delete this key.
|
2016-07-23 22:38:22 +05:30
|
|
|
if !doer.IsAdmin && doer.ID != key.OwnerID {
|
|
|
|
return ErrKeyAccessDenied{doer.ID, key.ID, "public"}
|
2015-08-20 14:41:29 +05:30
|
|
|
}
|
|
|
|
|
2021-09-23 21:15:36 +05:30
|
|
|
sess := db.NewSession(db.DefaultContext)
|
2017-06-21 06:27:05 +05:30
|
|
|
defer sess.Close()
|
2015-08-06 20:18:11 +05:30
|
|
|
if err = sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-26 14:56:48 +05:30
|
|
|
if err = deletePublicKeys(sess, id); err != nil {
|
2015-08-06 20:18:11 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-26 14:56:48 +05:30
|
|
|
if err = sess.Commit(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-18 21:56:26 +05:30
|
|
|
sess.Close()
|
2016-07-26 14:56:48 +05:30
|
|
|
|
2020-10-11 06:08:09 +05:30
|
|
|
if key.Type == KeyTypePrincipal {
|
|
|
|
return RewriteAllPrincipalKeys()
|
|
|
|
}
|
|
|
|
|
2016-07-26 14:56:48 +05:30
|
|
|
return RewriteAllPublicKeys()
|
2015-08-06 20:18:11 +05:30
|
|
|
}
|
|
|
|
|
2021-07-24 15:46:34 +05:30
|
|
|
// deleteKeysMarkedForDeletion returns true if ssh keys needs update
|
|
|
|
func deleteKeysMarkedForDeletion(keys []string) (bool, error) {
|
|
|
|
// Start session
|
2021-09-23 21:15:36 +05:30
|
|
|
sess := db.NewSession(db.DefaultContext)
|
2019-02-04 05:26:53 +05:30
|
|
|
defer sess.Close()
|
|
|
|
if err := sess.Begin(); err != nil {
|
2021-07-24 15:46:34 +05:30
|
|
|
return false, err
|
2015-12-03 10:54:37 +05:30
|
|
|
}
|
|
|
|
|
2021-07-24 15:46:34 +05:30
|
|
|
// Delete keys marked for deletion
|
|
|
|
var sshKeysNeedUpdate bool
|
|
|
|
for _, KeyToDelete := range keys {
|
|
|
|
key, err := searchPublicKeyByContentWithEngine(sess, KeyToDelete)
|
2015-12-06 03:43:13 +05:30
|
|
|
if err != nil {
|
2021-07-24 15:46:34 +05:30
|
|
|
log.Error("SearchPublicKeyByContent: %v", err)
|
|
|
|
continue
|
2015-08-06 20:18:11 +05:30
|
|
|
}
|
2021-07-24 15:46:34 +05:30
|
|
|
if err = deletePublicKeys(sess, key.ID); err != nil {
|
|
|
|
log.Error("deletePublicKeys: %v", err)
|
|
|
|
continue
|
2019-01-09 23:40:46 +05:30
|
|
|
}
|
2021-07-24 15:46:34 +05:30
|
|
|
sshKeysNeedUpdate = true
|
2015-08-06 20:18:11 +05:30
|
|
|
}
|
|
|
|
|
2021-07-24 15:46:34 +05:30
|
|
|
if err := sess.Commit(); err != nil {
|
|
|
|
return false, err
|
2020-10-11 06:08:09 +05:30
|
|
|
}
|
|
|
|
|
2021-07-24 15:46:34 +05:30
|
|
|
return sshKeysNeedUpdate, nil
|
2020-10-11 06:08:09 +05:30
|
|
|
}
|
|
|
|
|
2021-07-24 15:46:34 +05:30
|
|
|
// AddPublicKeysBySource add a users public keys. Returns true if there are changes.
|
2021-09-24 17:02:56 +05:30
|
|
|
func AddPublicKeysBySource(usr *User, s *login.Source, sshPublicKeys []string) bool {
|
2021-07-24 15:46:34 +05:30
|
|
|
var sshKeysNeedUpdate bool
|
|
|
|
for _, sshKey := range sshPublicKeys {
|
|
|
|
var err error
|
|
|
|
found := false
|
|
|
|
keys := []byte(sshKey)
|
|
|
|
loop:
|
|
|
|
for len(keys) > 0 && err == nil {
|
|
|
|
var out ssh.PublicKey
|
|
|
|
// We ignore options as they are not relevant to Gitea
|
|
|
|
out, _, _, keys, err = ssh.ParseAuthorizedKey(keys)
|
2020-10-11 06:08:09 +05:30
|
|
|
if err != nil {
|
2021-07-24 15:46:34 +05:30
|
|
|
break loop
|
2020-10-11 06:08:09 +05:30
|
|
|
}
|
2021-07-24 15:46:34 +05:30
|
|
|
found = true
|
|
|
|
marshalled := string(ssh.MarshalAuthorizedKey(out))
|
|
|
|
marshalled = marshalled[:len(marshalled)-1]
|
|
|
|
sshKeyName := fmt.Sprintf("%s-%s", s.Name, ssh.FingerprintSHA256(out))
|
|
|
|
|
|
|
|
if _, err := AddPublicKey(usr.ID, sshKeyName, marshalled, s.ID); err != nil {
|
|
|
|
if IsErrKeyAlreadyExist(err) {
|
|
|
|
log.Trace("AddPublicKeysBySource[%s]: Public SSH Key %s already exists for user", sshKeyName, usr.Name)
|
|
|
|
} else {
|
|
|
|
log.Error("AddPublicKeysBySource[%s]: Error adding Public SSH Key for user %s: %v", sshKeyName, usr.Name, err)
|
2020-10-11 06:08:09 +05:30
|
|
|
}
|
2021-07-24 15:46:34 +05:30
|
|
|
} else {
|
|
|
|
log.Trace("AddPublicKeysBySource[%s]: Added Public SSH Key for user %s", sshKeyName, usr.Name)
|
|
|
|
sshKeysNeedUpdate = true
|
2020-10-11 06:08:09 +05:30
|
|
|
}
|
2021-07-24 15:46:34 +05:30
|
|
|
}
|
|
|
|
if !found && err != nil {
|
|
|
|
log.Warn("AddPublicKeysBySource[%s]: Skipping invalid Public SSH Key for user %s: %v", s.Name, usr.Name, sshKey)
|
2020-10-11 06:08:09 +05:30
|
|
|
}
|
|
|
|
}
|
2021-07-24 15:46:34 +05:30
|
|
|
return sshKeysNeedUpdate
|
2020-10-11 06:08:09 +05:30
|
|
|
}
|
|
|
|
|
2021-07-24 15:46:34 +05:30
|
|
|
// SynchronizePublicKeys updates a users public keys. Returns true if there are changes.
|
2021-09-24 17:02:56 +05:30
|
|
|
func SynchronizePublicKeys(usr *User, s *login.Source, sshPublicKeys []string) bool {
|
2021-07-24 15:46:34 +05:30
|
|
|
var sshKeysNeedUpdate bool
|
2020-10-11 06:08:09 +05:30
|
|
|
|
2021-07-24 15:46:34 +05:30
|
|
|
log.Trace("synchronizePublicKeys[%s]: Handling Public SSH Key synchronization for user %s", s.Name, usr.Name)
|
2020-10-11 06:08:09 +05:30
|
|
|
|
2021-07-24 15:46:34 +05:30
|
|
|
// Get Public Keys from DB with current LDAP source
|
|
|
|
var giteaKeys []string
|
|
|
|
keys, err := ListPublicKeysBySource(usr.ID, s.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("synchronizePublicKeys[%s]: Error listing Public SSH Keys for user %s: %v", s.Name, usr.Name, err)
|
2020-10-11 06:08:09 +05:30
|
|
|
}
|
|
|
|
|
2021-07-24 15:46:34 +05:30
|
|
|
for _, v := range keys {
|
|
|
|
giteaKeys = append(giteaKeys, v.OmitEmail())
|
2020-10-11 06:08:09 +05:30
|
|
|
}
|
|
|
|
|
2021-07-24 15:46:34 +05:30
|
|
|
// Process the provided keys to remove duplicates and name part
|
|
|
|
var providedKeys []string
|
|
|
|
for _, v := range sshPublicKeys {
|
|
|
|
sshKeySplit := strings.Split(v, " ")
|
|
|
|
if len(sshKeySplit) > 1 {
|
|
|
|
key := strings.Join(sshKeySplit[:2], " ")
|
|
|
|
if !util.ExistsInSlice(key, providedKeys) {
|
|
|
|
providedKeys = append(providedKeys, key)
|
2020-11-28 08:12:08 +05:30
|
|
|
}
|
|
|
|
}
|
2020-10-11 06:08:09 +05:30
|
|
|
}
|
|
|
|
|
2021-07-24 15:46:34 +05:30
|
|
|
// Check if Public Key sync is needed
|
|
|
|
if util.IsEqualSlice(giteaKeys, providedKeys) {
|
|
|
|
log.Trace("synchronizePublicKeys[%s]: Public Keys are already in sync for %s (Source:%v/DB:%v)", s.Name, usr.Name, len(providedKeys), len(giteaKeys))
|
|
|
|
return false
|
2020-10-11 06:08:09 +05:30
|
|
|
}
|
2021-07-24 15:46:34 +05:30
|
|
|
log.Trace("synchronizePublicKeys[%s]: Public Key needs update for user %s (Source:%v/DB:%v)", s.Name, usr.Name, len(providedKeys), len(giteaKeys))
|
2020-10-11 06:08:09 +05:30
|
|
|
|
2021-07-24 15:46:34 +05:30
|
|
|
// Add new Public SSH Keys that doesn't already exist in DB
|
|
|
|
var newKeys []string
|
|
|
|
for _, key := range providedKeys {
|
|
|
|
if !util.ExistsInSlice(key, giteaKeys) {
|
|
|
|
newKeys = append(newKeys, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if AddPublicKeysBySource(usr, s, newKeys) {
|
|
|
|
sshKeysNeedUpdate = true
|
2020-10-11 06:08:09 +05:30
|
|
|
}
|
|
|
|
|
2021-07-24 15:46:34 +05:30
|
|
|
// Mark keys from DB that no longer exist in the source for deletion
|
|
|
|
var giteaKeysToDelete []string
|
|
|
|
for _, giteaKey := range giteaKeys {
|
|
|
|
if !util.ExistsInSlice(giteaKey, providedKeys) {
|
|
|
|
log.Trace("synchronizePublicKeys[%s]: Marking Public SSH Key for deletion for user %s: %v", s.Name, usr.Name, giteaKey)
|
|
|
|
giteaKeysToDelete = append(giteaKeysToDelete, giteaKey)
|
|
|
|
}
|
2020-10-11 06:08:09 +05:30
|
|
|
}
|
|
|
|
|
2021-07-24 15:46:34 +05:30
|
|
|
// Delete keys from DB that no longer exist in the source
|
|
|
|
needUpd, err := deleteKeysMarkedForDeletion(giteaKeysToDelete)
|
2020-11-28 08:12:08 +05:30
|
|
|
if err != nil {
|
2021-07-24 15:46:34 +05:30
|
|
|
log.Error("synchronizePublicKeys[%s]: Error deleting Public Keys marked for deletion for user %s: %v", s.Name, usr.Name, err)
|
2020-11-28 08:12:08 +05:30
|
|
|
}
|
2021-07-24 15:46:34 +05:30
|
|
|
if needUpd {
|
|
|
|
sshKeysNeedUpdate = true
|
2020-10-11 06:08:09 +05:30
|
|
|
}
|
2021-07-24 15:46:34 +05:30
|
|
|
|
|
|
|
return sshKeysNeedUpdate
|
2020-10-11 06:08:09 +05:30
|
|
|
}
|