|
|
@ -5,7 +5,6 @@ |
|
|
|
package cmd |
|
|
|
package cmd |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
//"container/list"
|
|
|
|
|
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"os" |
|
|
|
"os" |
|
|
|
"os/exec" |
|
|
|
"os/exec" |
|
|
@ -16,24 +15,10 @@ import ( |
|
|
|
"github.com/codegangsta/cli" |
|
|
|
"github.com/codegangsta/cli" |
|
|
|
qlog "github.com/qiniu/log" |
|
|
|
qlog "github.com/qiniu/log" |
|
|
|
|
|
|
|
|
|
|
|
//"github.com/gogits/git"
|
|
|
|
|
|
|
|
"github.com/gogits/gogs/models" |
|
|
|
"github.com/gogits/gogs/models" |
|
|
|
"github.com/gogits/gogs/modules/base" |
|
|
|
"github.com/gogits/gogs/modules/base" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
|
|
|
|
COMMANDS_READONLY = map[string]int{ |
|
|
|
|
|
|
|
"git-upload-pack": models.AU_WRITABLE, |
|
|
|
|
|
|
|
"git upload-pack": models.AU_WRITABLE, |
|
|
|
|
|
|
|
"git-upload-archive": models.AU_WRITABLE, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
COMMANDS_WRITE = map[string]int{ |
|
|
|
|
|
|
|
"git-receive-pack": models.AU_READABLE, |
|
|
|
|
|
|
|
"git receive-pack": models.AU_READABLE, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var CmdServ = cli.Command{ |
|
|
|
var CmdServ = cli.Command{ |
|
|
|
Name: "serv", |
|
|
|
Name: "serv", |
|
|
|
Usage: "This command should only be called by SSH shell", |
|
|
|
Usage: "This command should only be called by SSH shell", |
|
|
@ -42,8 +27,7 @@ var CmdServ = cli.Command{ |
|
|
|
Flags: []cli.Flag{}, |
|
|
|
Flags: []cli.Flag{}, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func newLogger(execDir string) { |
|
|
|
func newLogger(logPath string) { |
|
|
|
logPath := execDir + "/log/serv.log" |
|
|
|
|
|
|
|
os.MkdirAll(path.Dir(logPath), os.ModePerm) |
|
|
|
os.MkdirAll(path.Dir(logPath), os.ModePerm) |
|
|
|
|
|
|
|
|
|
|
|
f, err := os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.ModePerm) |
|
|
|
f, err := os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.ModePerm) |
|
|
@ -56,6 +40,20 @@ func newLogger(execDir string) { |
|
|
|
qlog.Info("Start logging serv...") |
|
|
|
qlog.Info("Start logging serv...") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func setup(logPath string) { |
|
|
|
|
|
|
|
execDir, _ := base.ExecDir() |
|
|
|
|
|
|
|
newLogger(path.Join(execDir, logPath)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
base.NewConfigContext() |
|
|
|
|
|
|
|
models.LoadModelsConfig() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if models.UseSQLite3 { |
|
|
|
|
|
|
|
os.Chdir(execDir) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
models.SetEngine() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func parseCmd(cmd string) (string, string) { |
|
|
|
func parseCmd(cmd string) (string, string) { |
|
|
|
ss := strings.SplitN(cmd, " ", 2) |
|
|
|
ss := strings.SplitN(cmd, " ", 2) |
|
|
|
if len(ss) != 2 { |
|
|
|
if len(ss) != 2 { |
|
|
@ -71,39 +69,46 @@ func parseCmd(cmd string) (string, string) { |
|
|
|
return verb, strings.Replace(args, "'/", "'", 1) |
|
|
|
return verb, strings.Replace(args, "'/", "'", 1) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
|
|
|
|
COMMANDS_READONLY = map[string]int{ |
|
|
|
|
|
|
|
"git-upload-pack": models.AU_WRITABLE, |
|
|
|
|
|
|
|
"git upload-pack": models.AU_WRITABLE, |
|
|
|
|
|
|
|
"git-upload-archive": models.AU_WRITABLE, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
COMMANDS_WRITE = map[string]int{ |
|
|
|
|
|
|
|
"git-receive-pack": models.AU_READABLE, |
|
|
|
|
|
|
|
"git receive-pack": models.AU_READABLE, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func In(b string, sl map[string]int) bool { |
|
|
|
func In(b string, sl map[string]int) bool { |
|
|
|
_, e := sl[b] |
|
|
|
_, e := sl[b] |
|
|
|
return e |
|
|
|
return e |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func runServ(k *cli.Context) { |
|
|
|
func runServ(k *cli.Context) { |
|
|
|
execDir, _ := base.ExecDir() |
|
|
|
setup("log/serv.log") |
|
|
|
newLogger(execDir) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
base.NewConfigContext() |
|
|
|
|
|
|
|
models.LoadModelsConfig() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if models.UseSQLite3 { |
|
|
|
|
|
|
|
os.Chdir(execDir) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
models.SetEngine() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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("Gogs: auth file format error") |
|
|
|
qlog.Fatal("auth file format error") |
|
|
|
qlog.Fatal("Invalid auth file format: %s", os.Args[2]) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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("Gogs: auth file format error") |
|
|
|
qlog.Fatal("auth file format error", err) |
|
|
|
qlog.Fatalf("Invalid auth file format: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
user, err := models.GetUserByKeyId(keyId) |
|
|
|
user, err := models.GetUserByKeyId(keyId) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
println("You have no right to access") |
|
|
|
if err == models.ErrUserNotKeyOwner { |
|
|
|
qlog.Fatalf("SSH visit error: %v", err) |
|
|
|
println("Gogs: you are not the owner of SSH key") |
|
|
|
|
|
|
|
qlog.Fatalf("Invalid owner of SSH key: %d", keyId) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
println("Gogs: internal error:", err) |
|
|
|
|
|
|
|
qlog.Fatalf("Fail to get user by key ID(%d): %v", keyId, err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
cmd := os.Getenv("SSH_ORIGINAL_COMMAND") |
|
|
|
cmd := os.Getenv("SSH_ORIGINAL_COMMAND") |
|
|
@ -116,8 +121,8 @@ func runServ(k *cli.Context) { |
|
|
|
repoPath := strings.Trim(args, "'") |
|
|
|
repoPath := strings.Trim(args, "'") |
|
|
|
rr := strings.SplitN(repoPath, "/", 2) |
|
|
|
rr := strings.SplitN(repoPath, "/", 2) |
|
|
|
if len(rr) != 2 { |
|
|
|
if len(rr) != 2 { |
|
|
|
println("Unavailable repository", args) |
|
|
|
println("Gogs: unavailable repository", args) |
|
|
|
qlog.Fatalf("Unavailable repository %v", args) |
|
|
|
qlog.Fatalf("Unavailable repository: %v", args) |
|
|
|
} |
|
|
|
} |
|
|
|
repoUserName := rr[0] |
|
|
|
repoUserName := rr[0] |
|
|
|
repoName := strings.TrimSuffix(rr[1], ".git") |
|
|
|
repoName := strings.TrimSuffix(rr[1], ".git") |
|
|
@ -127,17 +132,21 @@ func runServ(k *cli.Context) { |
|
|
|
|
|
|
|
|
|
|
|
repoUser, err := models.GetUserByName(repoUserName) |
|
|
|
repoUser, err := models.GetUserByName(repoUserName) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
println("You have no right to access") |
|
|
|
if err == models.ErrUserNotExist { |
|
|
|
qlog.Fatalf("Get user failed: %v", err) |
|
|
|
println("Gogs: given repository owner are not registered") |
|
|
|
|
|
|
|
qlog.Fatalf("Unregistered owner: %s", repoUserName) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
println("Gogs: internal error:", err) |
|
|
|
|
|
|
|
qlog.Fatalf("Fail to get repository owner(%s): %v", repoUserName, err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// access check
|
|
|
|
// Access check.
|
|
|
|
switch { |
|
|
|
switch { |
|
|
|
case isWrite: |
|
|
|
case isWrite: |
|
|
|
has, err := models.HasAccess(user.LowerName, path.Join(repoUserName, repoName), models.AU_WRITABLE) |
|
|
|
has, err := models.HasAccess(user.Name, path.Join(repoUserName, repoName), models.AU_WRITABLE) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
println("Internal error:", err) |
|
|
|
println("Gogs: internal error:", err) |
|
|
|
qlog.Fatal(err) |
|
|
|
qlog.Fatal("Fail to check write access:", err) |
|
|
|
} else if !has { |
|
|
|
} else if !has { |
|
|
|
println("You have no right to write this repository") |
|
|
|
println("You have no right to write this repository") |
|
|
|
qlog.Fatalf("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) |
|
|
@ -145,8 +154,12 @@ func runServ(k *cli.Context) { |
|
|
|
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) |
|
|
|
if err == models.ErrRepoNotExist { |
|
|
|
qlog.Fatal("Get repository error: " + err.Error()) |
|
|
|
println("Gogs: given repository does not exist") |
|
|
|
|
|
|
|
qlog.Fatalf("Repository does not exist: %s/%s", repoUser.Name, repoName) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
println("Gogs: internal error:", err) |
|
|
|
|
|
|
|
qlog.Fatalf("Fail to get repository: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if !repo.IsPrivate { |
|
|
|
if !repo.IsPrivate { |
|
|
@ -155,23 +168,15 @@ func runServ(k *cli.Context) { |
|
|
|
|
|
|
|
|
|
|
|
has, err := models.HasAccess(user.Name, path.Join(repoUserName, repoName), models.AU_READABLE) |
|
|
|
has, err := models.HasAccess(user.Name, path.Join(repoUserName, repoName), models.AU_READABLE) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
println("Internal error") |
|
|
|
println("Gogs: internal error:", err) |
|
|
|
qlog.Fatal(err) |
|
|
|
qlog.Fatal("Fail to check read access:", err) |
|
|
|
} |
|
|
|
} else if !has { |
|
|
|
if !has { |
|
|
|
|
|
|
|
has, err = models.HasAccess(user.Name, path.Join(repoUserName, repoName), models.AU_WRITABLE) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
println("Internal error") |
|
|
|
|
|
|
|
qlog.Fatal(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if !has { |
|
|
|
|
|
|
|
println("You have no right to access this repository") |
|
|
|
println("You have no right to access this repository") |
|
|
|
qlog.Fatal("You have no right to access this repository") |
|
|
|
qlog.Fatalf("User %s has no right to read repository %s", user.Name, repoPath) |
|
|
|
} |
|
|
|
} |
|
|
|
default: |
|
|
|
default: |
|
|
|
println("Unknown command") |
|
|
|
println("Unknown command") |
|
|
|
qlog.Fatal("Unknown command") |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
models.SetRepoEnvs(user.Id, user.Name, repoName, repoUserName) |
|
|
|
models.SetRepoEnvs(user.Id, user.Name, repoName, repoUserName) |
|
|
@ -183,8 +188,8 @@ func runServ(k *cli.Context) { |
|
|
|
gitcmd.Stderr = os.Stderr |
|
|
|
gitcmd.Stderr = os.Stderr |
|
|
|
|
|
|
|
|
|
|
|
if err = gitcmd.Run(); err != nil { |
|
|
|
if err = gitcmd.Run(); err != nil { |
|
|
|
println("execute command error:", err.Error()) |
|
|
|
println("Gogs: internal error:", err) |
|
|
|
qlog.Fatal("execute command error: " + err.Error()) |
|
|
|
qlog.Fatalf("Fail to execute git command: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//refName := os.Getenv("refName")
|
|
|
|
//refName := os.Getenv("refName")
|
|
|
|