|
|
@ -14,7 +14,7 @@ import ( |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/codegangsta/cli" |
|
|
|
"github.com/codegangsta/cli" |
|
|
|
"github.com/gogits/gogs/modules/log" |
|
|
|
qlog "github.com/qiniu/log" |
|
|
|
|
|
|
|
|
|
|
|
//"github.com/gogits/git"
|
|
|
|
//"github.com/gogits/git"
|
|
|
|
"github.com/gogits/gogs/models" |
|
|
|
"github.com/gogits/gogs/models" |
|
|
@ -44,11 +44,15 @@ gogs serv provide access auth for repositories`, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func newLogger(execDir string) { |
|
|
|
func newLogger(execDir string) { |
|
|
|
level := "0" |
|
|
|
|
|
|
|
logPath := execDir + "/log/serv.log" |
|
|
|
logPath := execDir + "/log/serv.log" |
|
|
|
os.MkdirAll(path.Dir(logPath), os.ModePerm) |
|
|
|
os.MkdirAll(path.Dir(logPath), os.ModePerm) |
|
|
|
log.NewLogger(0, "file", fmt.Sprintf(`{"level":%s,"filename":"%s"}`, level, logPath)) |
|
|
|
f, err := os.Open(logPath) |
|
|
|
log.Trace("start logging...") |
|
|
|
if err != nil { |
|
|
|
|
|
|
|
qlog.Fatal(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
qlog.SetOutput(f) |
|
|
|
|
|
|
|
qlog.Info("Start logging serv...") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func parseCmd(cmd string) (string, string) { |
|
|
|
func parseCmd(cmd string) (string, string) { |
|
|
@ -87,21 +91,18 @@ func runServ(k *cli.Context) { |
|
|
|
keys := strings.Split(os.Args[2], "-") |
|
|
|
keys := strings.Split(os.Args[2], "-") |
|
|
|
if len(keys) != 2 { |
|
|
|
if len(keys) != 2 { |
|
|
|
println("auth file format error") |
|
|
|
println("auth file format error") |
|
|
|
log.Error("auth file format error") |
|
|
|
qlog.Fatal("auth file format error") |
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
keyId, err := strconv.ParseInt(keys[1], 10, 64) |
|
|
|
keyId, err := strconv.ParseInt(keys[1], 10, 64) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
println("auth file format error") |
|
|
|
println("auth file format error") |
|
|
|
log.Error("auth file format error", err) |
|
|
|
qlog.Fatal("auth file format error", err) |
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
user, err := models.GetUserByKeyId(keyId) |
|
|
|
user, err := models.GetUserByKeyId(keyId) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
println("You have no right to access") |
|
|
|
println("You have no right to access") |
|
|
|
log.Error("SSH visit error: %v", err) |
|
|
|
qlog.Fatalf("SSH visit error: %v", err) |
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
cmd := os.Getenv("SSH_ORIGINAL_COMMAND") |
|
|
|
cmd := os.Getenv("SSH_ORIGINAL_COMMAND") |
|
|
@ -115,8 +116,7 @@ func runServ(k *cli.Context) { |
|
|
|
rr := strings.SplitN(repoPath, "/", 2) |
|
|
|
rr := strings.SplitN(repoPath, "/", 2) |
|
|
|
if len(rr) != 2 { |
|
|
|
if len(rr) != 2 { |
|
|
|
println("Unavilable repository", args) |
|
|
|
println("Unavilable repository", args) |
|
|
|
log.Error("Unavilable repository %v", args) |
|
|
|
qlog.Fatalf("Unavilable repository %v", args) |
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
repoUserName := rr[0] |
|
|
|
repoUserName := rr[0] |
|
|
|
repoName := rr[1] |
|
|
|
repoName := rr[1] |
|
|
@ -129,9 +129,8 @@ func runServ(k *cli.Context) { |
|
|
|
|
|
|
|
|
|
|
|
repoUser, err := models.GetUserByName(repoUserName) |
|
|
|
repoUser, err := models.GetUserByName(repoUserName) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
fmt.Println("You have no right to access") |
|
|
|
println("You have no right to access") |
|
|
|
log.Error("Get user failed", err) |
|
|
|
qlog.Fatal("Get user failed", err) |
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// access check
|
|
|
|
// access check
|
|
|
@ -140,19 +139,16 @@ func runServ(k *cli.Context) { |
|
|
|
has, err := models.HasAccess(user.LowerName, path.Join(repoUserName, repoName), models.AU_WRITABLE) |
|
|
|
has, err := models.HasAccess(user.LowerName, path.Join(repoUserName, repoName), models.AU_WRITABLE) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
println("Inernel error:", err) |
|
|
|
println("Inernel error:", err) |
|
|
|
log.Error(err.Error()) |
|
|
|
qlog.Fatal(err) |
|
|
|
return |
|
|
|
|
|
|
|
} else if !has { |
|
|
|
} else if !has { |
|
|
|
println("You have no right to write this repository") |
|
|
|
println("You have no right to write this repository") |
|
|
|
log.Error("User %s has no right to write repository %s", user.Name, repoPath) |
|
|
|
qlog.Fatalf("User %s has no right to write repository %s", user.Name, repoPath) |
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
case isRead: |
|
|
|
case isRead: |
|
|
|
repo, err := models.GetRepositoryByName(repoUser.Id, repoName) |
|
|
|
repo, err := models.GetRepositoryByName(repoUser.Id, repoName) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
println("Get repository error:", err) |
|
|
|
println("Get repository error:", err) |
|
|
|
log.Error("Get repository error: " + err.Error()) |
|
|
|
qlog.Fatal("Get repository error: " + err.Error()) |
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if !repo.IsPrivate { |
|
|
|
if !repo.IsPrivate { |
|
|
@ -162,26 +158,22 @@ func runServ(k *cli.Context) { |
|
|
|
has, err := models.HasAccess(user.Name, repoPath, models.AU_READABLE) |
|
|
|
has, err := models.HasAccess(user.Name, repoPath, models.AU_READABLE) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
println("Inernel error") |
|
|
|
println("Inernel error") |
|
|
|
log.Error(err.Error()) |
|
|
|
qlog.Fatal(err) |
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if !has { |
|
|
|
if !has { |
|
|
|
has, err = models.HasAccess(user.Name, repoPath, models.AU_WRITABLE) |
|
|
|
has, err = models.HasAccess(user.Name, repoPath, models.AU_WRITABLE) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
println("Inernel error") |
|
|
|
println("Inernel error") |
|
|
|
log.Error(err.Error()) |
|
|
|
qlog.Fatal(err) |
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if !has { |
|
|
|
if !has { |
|
|
|
println("You have no right to access this repository") |
|
|
|
println("You have no right to access this repository") |
|
|
|
log.Error("You have no right to access this repository") |
|
|
|
qlog.Fatal("You have no right to access this repository") |
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
default: |
|
|
|
default: |
|
|
|
println("Unknown command") |
|
|
|
println("Unknown command") |
|
|
|
log.Error("Unknown command") |
|
|
|
qlog.Fatal("Unknown command") |
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// for update use
|
|
|
|
// for update use
|
|
|
@ -197,7 +189,6 @@ func runServ(k *cli.Context) { |
|
|
|
|
|
|
|
|
|
|
|
if err = gitcmd.Run(); err != nil { |
|
|
|
if err = gitcmd.Run(); err != nil { |
|
|
|
println("execute command error:", err.Error()) |
|
|
|
println("execute command error:", err.Error()) |
|
|
|
log.Error("execute command error: " + err.Error()) |
|
|
|
qlog.Fatal("execute command error: " + err.Error()) |
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|