1
0
mirror of https://github.com/go-gitea/gitea.git synced 2024-11-04 08:17:24 -05:00

models: code fix on #818

This commit is contained in:
Unknwon 2015-02-01 17:21:56 -05:00
parent cd6376f093
commit 0a2068d7fb
3 changed files with 37 additions and 21 deletions

View File

@ -164,7 +164,7 @@ func runFixLocation(ctx *cli.Context) {
fmt.Scanln() fmt.Scanln()
// Fix in authorized_keys file. // Fix in authorized_keys file.
sshPath := path.Join(models.SshPath, "authorized_keys") sshPath := path.Join(models.SSHPath, "authorized_keys")
if com.IsFile(sshPath) { if com.IsFile(sshPath) {
fmt.Printf("Fixing pathes in file: %s\n", sshPath) fmt.Printf("Fixing pathes in file: %s\n", sshPath)
if err := rewriteAuthorizedKeys(sshPath, oldPath, execPath); err != nil { if err := rewriteAuthorizedKeys(sshPath, oldPath, execPath); err != nil {

View File

@ -514,8 +514,8 @@ dashboard.delete_repo_archives = Delete all repositories archives
dashboard.delete_repo_archives_success = All repositories archives have been deleted successfully. dashboard.delete_repo_archives_success = All repositories archives have been deleted successfully.
dashboard.git_gc_repos = Do garbage collection on repositories dashboard.git_gc_repos = Do garbage collection on repositories
dashboard.git_gc_repos_success = All repositories have done garbage collection successfully. dashboard.git_gc_repos_success = All repositories have done garbage collection successfully.
dashboard.resync_all_sshkeys = Do resync .ssh/autorized_key file dashboard.resync_all_sshkeys = Rewrite '.ssh/autorized_key' file(caution: non-Gogs keys will be lost)
dashboard.resync_all_sshkeys_success = All keys are synced again. dashboard.resync_all_sshkeys_success = All public keys have been rewritten successfully.
dashboard.server_uptime = Server Uptime dashboard.server_uptime = Server Uptime
dashboard.current_goroutine = Current Goroutines dashboard.current_goroutine = Current Goroutines
dashboard.current_memory_usage = Current Memory Usage dashboard.current_memory_usage = Current Memory Usage

View File

@ -33,7 +33,7 @@ const (
) )
var ( var (
ErrKeyAlreadyExist = errors.New("Public key already exist") ErrKeyAlreadyExist = errors.New("Public key already exists")
ErrKeyNotExist = errors.New("Public key does not exist") ErrKeyNotExist = errors.New("Public key does not exist")
ErrKeyUnableVerify = errors.New("Unable to verify public key") ErrKeyUnableVerify = errors.New("Unable to verify public key")
) )
@ -41,7 +41,7 @@ var (
var sshOpLocker = sync.Mutex{} var sshOpLocker = sync.Mutex{}
var ( var (
SshPath string // SSH directory. SSHPath string // SSH directory.
appPath string // Execution(binary) path. appPath string // Execution(binary) path.
) )
@ -72,9 +72,9 @@ func init() {
appPath = strings.Replace(appPath, "\\", "/", -1) appPath = strings.Replace(appPath, "\\", "/", -1)
// Determine and create .ssh path. // Determine and create .ssh path.
SshPath = filepath.Join(homeDir(), ".ssh") SSHPath = filepath.Join(homeDir(), ".ssh")
if err = os.MkdirAll(SshPath, 0700); err != nil { if err = os.MkdirAll(SSHPath, 0700); err != nil {
log.Fatal(4, "fail to create SshPath(%s): %v\n", SshPath, err) log.Fatal(4, "fail to create '%s': %v", SSHPath, err)
} }
} }
@ -248,12 +248,13 @@ func saveAuthorizedKeyFile(keys ...*PublicKey) error {
sshOpLocker.Lock() sshOpLocker.Lock()
defer sshOpLocker.Unlock() defer sshOpLocker.Unlock()
fpath := filepath.Join(SshPath, "authorized_keys") fpath := filepath.Join(SSHPath, "authorized_keys")
f, err := os.OpenFile(fpath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600) f, err := os.OpenFile(fpath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
if err != nil { if err != nil {
return err return err
} }
defer f.Close() defer f.Close()
finfo, err := f.Stat() finfo, err := f.Stat()
if err != nil { if err != nil {
return err return err
@ -270,8 +271,7 @@ func saveAuthorizedKeyFile(keys ...*PublicKey) error {
} }
for _, key := range keys { for _, key := range keys {
_, err = f.WriteString(key.GetAuthorizedString()) if _, err = f.WriteString(key.GetAuthorizedString()); err != nil {
if err != nil {
return err return err
} }
} }
@ -418,8 +418,8 @@ func DeletePublicKey(key *PublicKey) error {
return err return err
} }
fpath := filepath.Join(SshPath, "authorized_keys") fpath := filepath.Join(SSHPath, "authorized_keys")
tmpPath := filepath.Join(SshPath, "authorized_keys.tmp") tmpPath := filepath.Join(SSHPath, "authorized_keys.tmp")
if err = rewriteAuthorizedKeys(key, fpath, tmpPath); err != nil { if err = rewriteAuthorizedKeys(key, fpath, tmpPath); err != nil {
return err return err
} else if err = os.Remove(fpath); err != nil { } else if err = os.Remove(fpath); err != nil {
@ -428,20 +428,36 @@ func DeletePublicKey(key *PublicKey) error {
return os.Rename(tmpPath, fpath) return os.Rename(tmpPath, fpath)
} }
// RewriteAllPublicKeys remove any authorized key and re-write all key from database again // RewriteAllPublicKeys removes any authorized key and rewrite all keys from database again.
func RewriteAllPublicKeys() error { func RewriteAllPublicKeys() error {
keys := make([]*PublicKey, 0, 5) sshOpLocker.Lock()
err := x.Find(&keys) defer sshOpLocker.Unlock()
tmpPath := filepath.Join(SSHPath, "authorized_keys.tmp")
f, err := os.Create(tmpPath)
if err != nil {
return err
}
defer os.Remove(tmpPath)
err = x.Iterate(new(PublicKey), func(idx int, bean interface{}) (err error) {
_, err = f.WriteString((bean.(*PublicKey)).GetAuthorizedString())
return err
})
f.Close()
if err != nil { if err != nil {
return err return err
} }
fpath := filepath.Join(SshPath, "authorized_keys") fpath := filepath.Join(SSHPath, "authorized_keys")
if _, err := os.Stat(fpath); os.IsNotExist(err) { if com.IsExist(fpath) {
return saveAuthorizedKeyFile(keys...) if err = os.Remove(fpath); err != nil {
return err
}
} }
if err := os.Remove(fpath); err != nil { if err = os.Rename(tmpPath, fpath); err != nil {
return err return err
} }
return saveAuthorizedKeyFile(keys...)
return nil
} }