Skip to content

Commit ab747f2

Browse files
committed
Fix delete SSH key in file
1 parent 8de9517 commit ab747f2

File tree

6 files changed

+138
-9
lines changed

6 files changed

+138
-9
lines changed

conf/app.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ RUN_USER = lunny
33

44
[repository]
55
ROOT = /Users/%(RUN_USER)s/git/gogs-repositories
6-
LANG_IGNS=Google Go|C|Python
7-
LICENSES=Apache v2 License|GPL v2|BSD (3-Clause) License
6+
LANG_IGNS=Google Go|C|Python|Ruby
7+
LICENSES=Apache v2 License|GPL v2|MIT License|BSD (3-Clause) License
88

99
[server]
1010
HTTP_ADDR =

conf/gitignore/Ruby

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
*.gem
2+
*.rbc
3+
.bundle
4+
.config
5+
coverage
6+
InstalledFiles
7+
lib/bundler/man
8+
pkg
9+
rdoc
10+
spec/reports
11+
test/tmp
12+
test/version_tmp
13+
tmp
14+
15+
# YARD artifacts
16+
.yardoc
17+
_yardoc
18+
doc/

conf/license/MIT License

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

models/publickey.go

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
1+
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
15
package models
26

37
import (
8+
"bufio"
49
"fmt"
10+
"io"
511
"os"
612
"os/exec"
713
"path/filepath"
14+
"strings"
15+
"sync"
816
"time"
917

1018
"github.com/Unknwon/com"
1119
)
1220

1321
var (
22+
sshOpLocker = sync.Mutex{}
1423
//publicKeyRootPath string
15-
sshPath string
16-
appPath string
17-
tmplPublicKey = "### autogenerated by gitgos, DO NOT EDIT\n" +
18-
"command=\"%s serv key-%d\",no-port-forwarding," +
24+
sshPath string
25+
appPath string
26+
// "### autogenerated by gitgos, DO NOT EDIT\n"
27+
tmplPublicKey = "command=\"%s serv key-%d\",no-port-forwarding," +
1928
"no-X11-forwarding,no-agent-forwarding,no-pty %s\n"
2029
)
2130

@@ -77,9 +86,69 @@ func AddPublicKey(key *PublicKey) error {
7786
return nil
7887
}
7988

80-
func DeletePublicKey(key *PublicKey) error {
81-
_, err := orm.Delete(key)
82-
return err
89+
// DeletePublicKey deletes SSH key information both in database and authorized_keys file.
90+
func DeletePublicKey(key *PublicKey) (err error) {
91+
if _, err = orm.Delete(key); err != nil {
92+
return err
93+
}
94+
95+
sshOpLocker.Lock()
96+
defer sshOpLocker.Unlock()
97+
98+
p := filepath.Join(sshPath, "authorized_keys")
99+
tmpP := filepath.Join(sshPath, "authorized_keys.tmp")
100+
fr, err := os.Open(p)
101+
if err != nil {
102+
return err
103+
}
104+
defer fr.Close()
105+
106+
fw, err := os.Create(tmpP)
107+
if err != nil {
108+
return err
109+
}
110+
defer fw.Close()
111+
112+
buf := bufio.NewReader(fr)
113+
for {
114+
line, errRead := buf.ReadString('\n')
115+
line = strings.TrimSpace(line)
116+
117+
if errRead != nil {
118+
if errRead != io.EOF {
119+
return errRead
120+
}
121+
122+
// Reached end of file, if nothing to read then break,
123+
// otherwise handle the last line.
124+
if len(line) == 0 {
125+
break
126+
}
127+
}
128+
129+
// Found the line and copy rest of file.
130+
if strings.Contains(line, key.Content) {
131+
if _, err = io.Copy(fw, fr); err != nil {
132+
return err
133+
}
134+
break
135+
}
136+
137+
// Still finding the line, copy the line that currently read.
138+
if _, err = fw.WriteString(line + "\n"); err != nil {
139+
return err
140+
}
141+
142+
if errRead == io.EOF {
143+
break
144+
}
145+
}
146+
147+
if err = os.Remove(p); err != nil {
148+
return err
149+
}
150+
151+
return os.Rename(tmpP, p)
83152
}
84153

85154
func ListPublicKey(userId int64) ([]PublicKey, error) {
@@ -89,11 +158,16 @@ func ListPublicKey(userId int64) ([]PublicKey, error) {
89158
}
90159

91160
func SaveAuthorizedKeyFile(key *PublicKey) error {
161+
sshOpLocker.Lock()
162+
defer sshOpLocker.Unlock()
163+
92164
p := filepath.Join(sshPath, "authorized_keys")
93165
f, err := os.OpenFile(p, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
94166
if err != nil {
95167
return err
96168
}
169+
defer f.Close()
170+
97171
//os.Chmod(p, 0600)
98172
_, err = f.WriteString(GenAuthorizedKey(key.Id, key.Content))
99173
return err

models/user.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ func UpdateUser(user *User) (err error) {
142142

143143
// DeleteUser completely deletes everything of the user.
144144
func DeleteUser(user *User) error {
145+
// Check ownership of repository.
145146
count, err := GetRepositoryCount(user)
146147
if err != nil {
147148
return errors.New("modesl.GetRepositories: " + err.Error())
@@ -151,6 +152,17 @@ func DeleteUser(user *User) error {
151152

152153
// TODO: check issues, other repos' commits
153154

155+
// Delete SSH keys.
156+
keys := make([]PublicKey, 0, 10)
157+
if err = orm.Find(&keys, &PublicKey{OwnerId: user.Id}); err != nil {
158+
return err
159+
}
160+
for _, key := range keys {
161+
if err = DeletePublicKey(&key); err != nil {
162+
return err
163+
}
164+
}
165+
154166
_, err = orm.Delete(user)
155167
// TODO: delete and update follower information.
156168
return err

routers/repo/single.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
15
package repo
26

37
import (

0 commit comments

Comments
 (0)