From 2a9da4b8e54736c3a8f7ecc41f3c75fdcfb70f9b Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 22 Apr 2014 03:41:47 -0400 Subject: [PATCH 01/29] Fix #92 --- gogs.go | 2 +- modules/base/base_redis.go | 1 + routers/repo/release.go | 1 - 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gogs.go b/gogs.go index 73555adab..b06c80064 100644 --- a/gogs.go +++ b/gogs.go @@ -19,7 +19,7 @@ import ( // Test that go1.2 tag above is included in builds. main.go refers to this definition. const go12tag = true -const APP_VER = "0.3.0.0421 Alpha" +const APP_VER = "0.3.0.0422 Alpha" func init() { base.AppVer = APP_VER diff --git a/modules/base/base_redis.go b/modules/base/base_redis.go index 327af8415..42430df77 100644 --- a/modules/base/base_redis.go +++ b/modules/base/base_redis.go @@ -4,6 +4,7 @@ package base import ( _ "github.com/gogits/cache/redis" + _ "github.com/gogits/session/redis" ) func init() { diff --git a/routers/repo/release.go b/routers/repo/release.go index a4baa4792..b386bac80 100644 --- a/routers/repo/release.go +++ b/routers/repo/release.go @@ -87,7 +87,6 @@ func Releases(ctx *middleware.Context) { return } tags.rels[i].NumCommitsBehind = commitsCount - tags.rels[i].NumCommits - tags.rels[i].Created = commit.Author.When } } From 8bc502a1ead4d59e1940946c74ff48c8e6457633 Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 22 Apr 2014 22:34:49 -0400 Subject: [PATCH 02/29] New err check style --- models/repo.go | 24 ++++++++---------------- routers/repo/repo.go | 1 - 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/models/repo.go b/models/repo.go index 19fe6e288..2011ed7de 100644 --- a/models/repo.go +++ b/models/repo.go @@ -47,16 +47,16 @@ func NewRepoContext() { zip.Verbose = false // Check if server has basic git setting. - stdout, _, err := com.ExecCmd("git", "config", "--get", "user.name") - if err != nil { - fmt.Printf("repo.init(fail to get git user.name): %v", err) + stdout, stderr, err := com.ExecCmd("git", "config", "--get", "user.name") + if strings.Contains(stderr, "fatal:") { + fmt.Printf("repo.NewRepoContext(fail to get git user.name): %s", stderr) os.Exit(2) - } else if len(stdout) == 0 { - if _, _, err = com.ExecCmd("git", "config", "--global", "user.email", "gogitservice@gmail.com"); err != nil { - fmt.Printf("repo.init(fail to set git user.email): %v", err) + } else if err != nil || len(strings.TrimSpace(stdout)) == 0 { + if _, stderr, err = com.ExecCmd("git", "config", "--global", "user.email", "gogitservice@gmail.com"); err != nil { + fmt.Printf("repo.NewRepoContext(fail to set git user.email): %s", stderr) os.Exit(2) - } else if _, _, err = com.ExecCmd("git", "config", "--global", "user.name", "Gogs"); err != nil { - fmt.Printf("repo.init(fail to set git user.name): %v", err) + } else if _, stderr, err = com.ExecCmd("git", "config", "--global", "user.name", "Gogs"); err != nil { + fmt.Printf("repo.NewRepoContext(fail to set git user.name): %s", stderr) os.Exit(2) } } @@ -352,7 +352,6 @@ func CreateRepository(user *User, name, desc, lang, license string, private, mir func extractGitBareZip(repoPath string) error { z, err := zip.Open("conf/content/git-bare.zip") if err != nil { - fmt.Println("shi?") return err } defer z.Close() @@ -364,21 +363,14 @@ func extractGitBareZip(repoPath string) error { func initRepoCommit(tmpPath string, sig *git.Signature) (err error) { var stderr string if _, stderr, err = com.ExecCmdDir(tmpPath, "git", "add", "--all"); err != nil { - return err - } else if strings.Contains(stderr, "fatal:") { return errors.New("git add: " + stderr) } - if _, stderr, err = com.ExecCmdDir(tmpPath, "git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", "Init commit"); err != nil { - return err - } else if strings.Contains(stderr, "fatal:") { return errors.New("git commit: " + stderr) } if _, stderr, err = com.ExecCmdDir(tmpPath, "git", "push", "origin", "master"); err != nil { - return err - } else if strings.Contains(stderr, "fatal:") { return errors.New("git push: " + stderr) } return nil diff --git a/routers/repo/repo.go b/routers/repo/repo.go index f733378b3..76964dff1 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -134,7 +134,6 @@ func Single(ctx *middleware.Context, params martini.Params) { } entry, err := ctx.Repo.Commit.GetTreeEntryByPath(treename) - if err != nil && err != git.ErrNotExist { ctx.Handle(404, "repo.Single(GetTreeEntryByPath)", err) return From e554e49c1691df4a8229b6ec2d17a0250661698d Mon Sep 17 00:00:00 2001 From: laofo Date: Wed, 23 Apr 2014 16:18:46 +0800 Subject: [PATCH 03/29] add the installation guide on ubuntu, including binary install and source install --- doc/install_gogs_from_binary_on_ubuntu.md | 22 +++++++++++ doc/install_gogs_from_source_on_ubuntu.md | 48 +++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 doc/install_gogs_from_binary_on_ubuntu.md create mode 100644 doc/install_gogs_from_source_on_ubuntu.md diff --git a/doc/install_gogs_from_binary_on_ubuntu.md b/doc/install_gogs_from_binary_on_ubuntu.md new file mode 100644 index 000000000..3edbb6a4a --- /dev/null +++ b/doc/install_gogs_from_binary_on_ubuntu.md @@ -0,0 +1,22 @@ +sudo apt-get update +sudo apt-get upgrade + +sudo apt-get install git +sudo apt-get install golang +sudo apt-get install mysql-server +$mysql -u root -p +mysql> SET GLOBAL storage_engine = 'InnoDB'; +mysql> CREATE DATABASE gogs CHARACTER SET utf8 COLLATE utf8_bin; +mysql> GRANT ALL PRIVILEGES ON gogs.* TO 'root'@'localhost' IDENTIFIED BY 'password'; +mysql> FLUSH PRIVILEGES; +mysql> QUIT + + +mkdir gogs +cd gogs +curl -L http://gobuild.io/github.com/gogits/gogs/v0.2.0/linux/amd64 -o v0.2.0.zip +unzip v0.2.0.zip +./start.sh + +> The up-to-date binary could be found at +> http://gobuild.io/download/github.com/gogits/gogs diff --git a/doc/install_gogs_from_source_on_ubuntu.md b/doc/install_gogs_from_source_on_ubuntu.md new file mode 100644 index 000000000..020c9f051 --- /dev/null +++ b/doc/install_gogs_from_source_on_ubuntu.md @@ -0,0 +1,48 @@ +#Install gogs under ubuntu 14.04 LTS 32bit from source code + +##Requirements +### Go Programming Language: Version >= 1.2 +### git(bash): Version >= 1.6.6(both server and client) +### MySQL: Version >= 5.1 or PostgreSQL or NOTHING. + +## Create the user which will run git +sudo adduser git +su git + +## Install git and Mysql-server +sudo apt-get install git +sudo apt-get install mysql-server + +## Create database +$mysql -u root -p +mysql> SET GLOBAL storage_engine = 'InnoDB'; +mysql> CREATE DATABASE gogs CHARACTER SET utf8 COLLATE utf8_bin; +mysql> GRANT ALL PRIVILEGES ON gogs.* TO 'root'@'localhost' IDENTIFIED BY 'pasword'; +mysql> FLUSH PRIVILEGES; +mysql> QUIT + +## install go from source +sudo apt-get install build-essential +sudo apt-get install mercurial +hg clone -r release https://go.googlecode.com/hg/ /home/git/golang/ + + +echo export GOROOT=/home/git/golang >>.bashrc +echo export GOARCH=386 >>.bashrc +echo export GOOS=linux >>.bashrc +echo export GOBIN= /home/git/golang/bin >>.bashrc +echo export GOPATH=$HOME/app/Go >>.bashrc +echo PATH=${PATH}: /$HOME/golang/bin >>.bashrc +cd $GOROOT/src +./make.bash + +## Download and install dependencies +$ go get -u github.com/gogits/gogs + +## Build main program +$ cd $GOPATH/src/github.com/gogits/gogs +$ go build +$ ./start.sh + +## At present, you could access gogs from http://localhost:3000 + From 2a95bc1395ec0cef34c969b048c4aacac90a42ea Mon Sep 17 00:00:00 2001 From: laofo Date: Wed, 23 Apr 2014 16:20:53 +0800 Subject: [PATCH 04/29] use the dedicated user, git, to run gogs --- doc/install_gogs_from_binary_on_ubuntu.md | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/install_gogs_from_binary_on_ubuntu.md b/doc/install_gogs_from_binary_on_ubuntu.md index 3edbb6a4a..857d8fdbf 100644 --- a/doc/install_gogs_from_binary_on_ubuntu.md +++ b/doc/install_gogs_from_binary_on_ubuntu.md @@ -1,3 +1,4 @@ +sudo adduser git sudo apt-get update sudo apt-get upgrade From d7956b3fb8410e4dae3a6d99b021db267a7f5fbf Mon Sep 17 00:00:00 2001 From: laofo Date: Wed, 23 Apr 2014 16:30:05 +0800 Subject: [PATCH 05/29] format the doc --- doc/install_gogs_from_binary_on_ubuntu.md | 36 +++++----- doc/install_gogs_from_source_on_ubuntu.md | 84 +++++++++++------------ 2 files changed, 61 insertions(+), 59 deletions(-) diff --git a/doc/install_gogs_from_binary_on_ubuntu.md b/doc/install_gogs_from_binary_on_ubuntu.md index 857d8fdbf..c2b237f54 100644 --- a/doc/install_gogs_from_binary_on_ubuntu.md +++ b/doc/install_gogs_from_binary_on_ubuntu.md @@ -1,23 +1,25 @@ -sudo adduser git -sudo apt-get update -sudo apt-get upgrade +### Binary install gogs on ubuntu 14.04 LTS -sudo apt-get install git -sudo apt-get install golang -sudo apt-get install mysql-server -$mysql -u root -p -mysql> SET GLOBAL storage_engine = 'InnoDB'; -mysql> CREATE DATABASE gogs CHARACTER SET utf8 COLLATE utf8_bin; -mysql> GRANT ALL PRIVILEGES ON gogs.* TO 'root'@'localhost' IDENTIFIED BY 'password'; -mysql> FLUSH PRIVILEGES; -mysql> QUIT +- sudo adduser git +- sudo apt-get update +- sudo apt-get upgrade +- sudo apt-get install git +- sudo apt-get install golang +- sudo apt-get install mysql-server +- $mysql -u root -p +- mysql> SET GLOBAL storage_engine = 'InnoDB'; +- mysql> CREATE DATABASE gogs CHARACTER SET utf8 COLLATE utf8_bin; +- mysql> GRANT ALL PRIVILEGES ON gogs.* TO 'root'@'localhost' IDENTIFIED BY 'password'; +- mysql> FLUSH PRIVILEGES; +- mysql> QUIT -mkdir gogs -cd gogs -curl -L http://gobuild.io/github.com/gogits/gogs/v0.2.0/linux/amd64 -o v0.2.0.zip -unzip v0.2.0.zip -./start.sh + +- mkdir gogs +- cd gogs +- curl -L http://gobuild.io/github.com/gogits/gogs/v0.2.0/linux/amd64 -o v0.2.0.zip +- unzip v0.2.0.zip +- ./start.sh > The up-to-date binary could be found at > http://gobuild.io/download/github.com/gogits/gogs diff --git a/doc/install_gogs_from_source_on_ubuntu.md b/doc/install_gogs_from_source_on_ubuntu.md index 020c9f051..b8ae6fc79 100644 --- a/doc/install_gogs_from_source_on_ubuntu.md +++ b/doc/install_gogs_from_source_on_ubuntu.md @@ -1,48 +1,48 @@ -#Install gogs under ubuntu 14.04 LTS 32bit from source code - -##Requirements -### Go Programming Language: Version >= 1.2 -### git(bash): Version >= 1.6.6(both server and client) -### MySQL: Version >= 5.1 or PostgreSQL or NOTHING. - -## Create the user which will run git -sudo adduser git -su git - -## Install git and Mysql-server -sudo apt-get install git -sudo apt-get install mysql-server - -## Create database -$mysql -u root -p -mysql> SET GLOBAL storage_engine = 'InnoDB'; -mysql> CREATE DATABASE gogs CHARACTER SET utf8 COLLATE utf8_bin; -mysql> GRANT ALL PRIVILEGES ON gogs.* TO 'root'@'localhost' IDENTIFIED BY 'pasword'; -mysql> FLUSH PRIVILEGES; -mysql> QUIT - -## install go from source -sudo apt-get install build-essential -sudo apt-get install mercurial -hg clone -r release https://go.googlecode.com/hg/ /home/git/golang/ +##Install gogs under ubuntu 14.04 LTS 32bit from source code + +###Requirements +- Go Programming Language: Version >= 1.2 +- git(bash): Version >= 1.6.6(both server and client) +- MySQL: Version >= 5.1 or PostgreSQL or NOTHING. + +### Create the user which will run git +- sudo adduser git +- su git + +### Install git and Mysql-server +- sudo apt-get install git +- sudo apt-get install mysql-server + +### Create database +- $ mysql -u root -p +- mysql> SET GLOBAL storage_engine = 'InnoDB'; +- mysql> CREATE DATABASE gogs CHARACTER SET utf8 COLLATE utf8_bin; +- mysql> GRANT ALL PRIVILEGES ON gogs.* TO 'root'@'localhost' IDENTIFIED BY 'pasword'; +- mysql> FLUSH PRIVILEGES; +- mysql> QUIT + +### install go from source +- sudo apt-get install build-essential +- sudo apt-get install mercurial +- hg clone -r release https://go.googlecode.com/hg/ /home/git/golang/ -echo export GOROOT=/home/git/golang >>.bashrc -echo export GOARCH=386 >>.bashrc -echo export GOOS=linux >>.bashrc -echo export GOBIN= /home/git/golang/bin >>.bashrc -echo export GOPATH=$HOME/app/Go >>.bashrc -echo PATH=${PATH}: /$HOME/golang/bin >>.bashrc -cd $GOROOT/src -./make.bash +- echo export GOROOT=/home/git/golang >>.bashrc +- echo export GOARCH=386 >>.bashrc +- echo export GOOS=linux >>.bashrc +- echo export GOBIN= /home/git/golang/bin >>.bashrc +- echo export GOPATH=$HOME/app/Go >>.bashrc +- echo PATH=${PATH}: /$HOME/golang/bin >>.bashrc +- cd $GOROOT/src +- ./make.bash -## Download and install dependencies -$ go get -u github.com/gogits/gogs +### Download and install dependencies +- $ go get -u github.com/gogits/gogs -## Build main program -$ cd $GOPATH/src/github.com/gogits/gogs -$ go build -$ ./start.sh +### Build main program +- $ cd $GOPATH/src/github.com/gogits/gogs +- $ go build +- $ ./start.sh -## At present, you could access gogs from http://localhost:3000 +### At present, you could access gogs from http://localhost:3000 From 649d0e1681001d1e100a646bd378e8d87dd73b21 Mon Sep 17 00:00:00 2001 From: laofo Date: Wed, 23 Apr 2014 16:33:03 +0800 Subject: [PATCH 06/29] format the doc and add comments --- doc/install_gogs_from_binary_on_ubuntu.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/doc/install_gogs_from_binary_on_ubuntu.md b/doc/install_gogs_from_binary_on_ubuntu.md index c2b237f54..8079fd1ab 100644 --- a/doc/install_gogs_from_binary_on_ubuntu.md +++ b/doc/install_gogs_from_binary_on_ubuntu.md @@ -1,12 +1,14 @@ ### Binary install gogs on ubuntu 14.04 LTS +### create user and install denpendency - sudo adduser git - sudo apt-get update - sudo apt-get upgrade - - sudo apt-get install git - sudo apt-get install golang - sudo apt-get install mysql-server + +### create the database - $mysql -u root -p - mysql> SET GLOBAL storage_engine = 'InnoDB'; - mysql> CREATE DATABASE gogs CHARACTER SET utf8 COLLATE utf8_bin; @@ -14,7 +16,7 @@ - mysql> FLUSH PRIVILEGES; - mysql> QUIT - +### install the gogs - mkdir gogs - cd gogs - curl -L http://gobuild.io/github.com/gogits/gogs/v0.2.0/linux/amd64 -o v0.2.0.zip From 683e58878a3f1a36db29a252dd6ba0551210b1c1 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 23 Apr 2014 17:12:50 +0800 Subject: [PATCH 07/29] bug fixed --- serve.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/serve.go b/serve.go index 321227957..7f8c267c7 100644 --- a/serve.go +++ b/serve.go @@ -159,7 +159,7 @@ func runServ(k *cli.Context) { qlog.Fatal(err) } if !has { - has, err = models.HasAccess(user.Name, repoPath, models.AU_WRITABLE) + has, err = models.HasAccess(user.Name, path.Join(repoUserName, repoName), models.AU_WRITABLE) if err != nil { println("Internal error") qlog.Fatal(err) From ff690840d42fa069dcda7a55cda602bf11b40b06 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 23 Apr 2014 19:24:26 +0800 Subject: [PATCH 08/29] doc improvements --- doc/install_gogs_from_binary_on_ubuntu.md | 1 - update.go | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/install_gogs_from_binary_on_ubuntu.md b/doc/install_gogs_from_binary_on_ubuntu.md index 8079fd1ab..6d004106c 100644 --- a/doc/install_gogs_from_binary_on_ubuntu.md +++ b/doc/install_gogs_from_binary_on_ubuntu.md @@ -5,7 +5,6 @@ - sudo apt-get update - sudo apt-get upgrade - sudo apt-get install git -- sudo apt-get install golang - sudo apt-get install mysql-server ### create the database diff --git a/update.go b/update.go index 196ed3e63..b8f595827 100644 --- a/update.go +++ b/update.go @@ -20,7 +20,7 @@ var CmdUpdate = cli.Command{ Name: "update", Usage: "This command just should be called by ssh shell", Description: ` -gogs serv provide access auth for repositories`, +gogs update get pushed info and insert into database`, Action: runUpdate, Flags: []cli.Flag{}, } From 4b58c016038be87fa8d50c73cc064da216f10ec2 Mon Sep 17 00:00:00 2001 From: James Anderson Date: Wed, 23 Apr 2014 15:30:18 -0400 Subject: [PATCH 09/29] Changed setting url to settings --- templates/base/navbar.tmpl | 2 +- templates/user/setting.tmpl | 2 +- templates/user/setting_nav.tmpl | 10 +++++----- web.go | 6 +++--- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/templates/base/navbar.tmpl b/templates/base/navbar.tmpl index e5b22192f..932cae36d 100644 --- a/templates/base/navbar.tmpl +++ b/templates/base/navbar.tmpl @@ -21,7 +21,7 @@ user-avatar - + {{if .IsAdmin}}{{end}} diff --git a/templates/user/setting_nav.tmpl b/templates/user/setting_nav.tmpl index 9c7ae5208..387fe950b 100644 --- a/templates/user/setting_nav.tmpl +++ b/templates/user/setting_nav.tmpl @@ -1,11 +1,11 @@
-

Account Setting

+

Account Settings

diff --git a/web.go b/web.go index d8d5aba10..23fa10796 100644 --- a/web.go +++ b/web.go @@ -94,8 +94,8 @@ func runWeb(*cli.Context) { m.Group("/user", func(r martini.Router) { r.Get("/delete", user.Delete) r.Post("/delete", user.DeletePost) - r.Get("/setting", user.Setting) - r.Post("/setting", bindIgnErr(auth.UpdateProfileForm{}), user.SettingPost) + r.Get("/settings", user.Setting) + r.Post("/settings", bindIgnErr(auth.UpdateProfileForm{}), user.SettingPost) }, reqSignIn) m.Group("/user", func(r martini.Router) { r.Get("/feeds", middleware.Bind(auth.FeedsForm{}), user.Feeds) @@ -105,7 +105,7 @@ func runWeb(*cli.Context) { r.Post("/forget_password", user.ForgotPasswdPost) r.Get("/logout", user.SignOut) }) - m.Group("/user/setting", func(r martini.Router) { + m.Group("/user/settings", func(r martini.Router) { r.Get("/social", user.SettingSocial) r.Get("/password", user.SettingPassword) r.Post("/password", bindIgnErr(auth.UpdatePasswdForm{}), user.SettingPasswordPost) From 32ae6896fa77f31f5626b41260743ef84c132b1e Mon Sep 17 00:00:00 2001 From: James Anderson Date: Wed, 23 Apr 2014 15:39:37 -0400 Subject: [PATCH 10/29] Close to Closed --- templates/issue/user.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/issue/user.tmpl b/templates/issue/user.tmpl index 1d49395cb..812d9d8e2 100644 --- a/templates/issue/user.tmpl +++ b/templates/issue/user.tmpl @@ -29,7 +29,7 @@
From efc05ea1dec5a60c95763fc5158d60b45ef46d8f Mon Sep 17 00:00:00 2001 From: juju2013 Date: Tue, 22 Apr 2014 18:55:27 +0200 Subject: [PATCH 11/29] initial support for LDAP authentication/MSAD --- .gitignore | 1 + models/ldap.go | 38 ++++++++++++++++ models/user.go | 1 + modules/auth/ldap/README.md | 43 +++++++++++++++++++ modules/auth/ldap/ldap.go | 86 +++++++++++++++++++++++++++++++++++++ modules/base/conf.go | 44 ++++++++++++++++--- routers/user/user.go | 11 ++++- 7 files changed, 216 insertions(+), 8 deletions(-) create mode 100644 models/ldap.go create mode 100644 modules/auth/ldap/README.md create mode 100644 modules/auth/ldap/ldap.go diff --git a/.gitignore b/.gitignore index f8d8a2869..65252f8c2 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ public/img/avatar/ *.o *.a *.so +dev # Folders _obj diff --git a/models/ldap.go b/models/ldap.go new file mode 100644 index 000000000..cc9058765 --- /dev/null +++ b/models/ldap.go @@ -0,0 +1,38 @@ +// Copyright github.com/juju2013. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package models + +import ( + "strings" + + "github.com/gogits/gogs/modules/auth/ldap" + "github.com/gogits/gogs/modules/log" +) + +// Query if name/passwd can login against the LDAP direcotry pool +// Create a local user if success +// Return the same LoginUserPlain semantic +func LoginUserLdap(name, passwd string) (*User, error) { + mail, logged := ldap.LoginUser(name, passwd) + if !logged { + // user not in LDAP, do nothing + return nil, ErrUserNotExist + } + // fake a local user creation + user := User{ + LowerName: strings.ToLower(name), + Name: strings.ToLower(name), + LoginType: 389, + IsActive: true, + Passwd: passwd, + Email: mail} + _, err := RegisterUser(&user) + if err != nil { + log.Debug("LDAP local user %s fond (%s) ", name, err) + } + // simulate local user login + localUser, err2 := GetUserByName(user.Name) + return localUser, err2 +} diff --git a/models/user.go b/models/user.go index ab43df7a1..df1eb985c 100644 --- a/models/user.go +++ b/models/user.go @@ -125,6 +125,7 @@ func GetUserSalt() string { // RegisterUser creates record of a new user. func RegisterUser(user *User) (*User, error) { + if !IsLegalName(user.Name) { return nil, ErrUserNameIllegal } diff --git a/modules/auth/ldap/README.md b/modules/auth/ldap/README.md new file mode 100644 index 000000000..8b508e0fe --- /dev/null +++ b/modules/auth/ldap/README.md @@ -0,0 +1,43 @@ +LDAP authentication +=================== + +## Goal + +Authenticat user against LDAP directories + +It will bind with the user's login/pasword and query attributs ("mail" for instance) in a pool of directory servers + +The first OK wins. + +If there's connection error, the server will be disabled and won't be checked again + +## Usage + +In the [security] section, set +> LDAP_AUTH = true + +then for each LDAP source, set + +> [LdapSource-someuniquename] +> name=canonicalName +> host=hostname-or-ip +> port=3268 # or regular LDAP port +> # the following settings depend highly how you've configured your AD +> basedn=dc=ACME,dc=COM +> MSADSAFORMAT=%s@ACME.COM +> filter=(&(objectClass=user)(sAMAccountName=%s)) + +### Limitation + +Only tested on an MS 2008R2 DC, using global catalog (TCP/3268) + +This MSAD is a mess. + +The way how one checks the directory (CN, DN etc...) may be highly depending local custom configuration + +### Todo +* Define a timeout per server +* Check servers marked as "Disabled" when they'll come back online +* Find a more flexible way to define filter/MSADSAFORMAT/Attributes etc... maybe text/template ? +* Check OpenLDAP server +* SSL support ? \ No newline at end of file diff --git a/modules/auth/ldap/ldap.go b/modules/auth/ldap/ldap.go new file mode 100644 index 000000000..29773cda5 --- /dev/null +++ b/modules/auth/ldap/ldap.go @@ -0,0 +1,86 @@ +// Copyright github.com/juju2013. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +// package ldap provide functions & structure to query a LDAP ldap directory +// For now, it's mainly tested again an MS Active Directory service, see README.md for more information +package ldap + +import ( + "fmt" + "github.com/gogits/gogs/modules/log" + goldap "github.com/juju2013/goldap" +) + +// Basic LDAP authentication service +type ldapsource struct { + Name string // canonical name (ie. corporate.ad) + Host string // LDAP host + Port int // port number + BaseDN string // Base DN + Attributes string // Attribut to search + Filter string // Query filter to validate entry + MsAdSAFormat string // in the case of MS AD Simple Authen, the format to use (see: http://msdn.microsoft.com/en-us/library/cc223499.aspx) + Enabled bool // if this source is disabled +} + +//Global LDAP directory pool +var ( + Authensource []ldapsource +) + +// Add a new source (LDAP directory) to the global pool +func AddSource(name string, host string, port int, basedn string, attributes string, filter string, msadsaformat string) { + ldaphost := ldapsource{name, host, port, basedn, attributes, filter, msadsaformat, true} + Authensource = append(Authensource, ldaphost) +} + +//LoginUser : try to login an user to LDAP sources, return requested (attribut,true) if ok, ("",false) other wise +//First match wins +//Returns first attribute if exists +func LoginUser(name, passwd string) (a string, r bool) { + r = false + for _, ls := range Authensource { + a, r = ls.searchEntry(name, passwd) + if r { + return + } + } + return +} + +// searchEntry : search an LDAP source if an entry (name, passwd) is valide and in the specific filter +func (ls ldapsource) searchEntry(name, passwd string) (string, bool) { + l, err := goldap.Dial("tcp", fmt.Sprintf("%s:%d", ls.Host, ls.Port)) + if err != nil { + log.Debug("LDAP Connect error, disabled source %s", ls.Host) + ls.Enabled = false + return "", false + } + defer l.Close() + + nx := fmt.Sprintf(ls.MsAdSAFormat, name) + err = l.Bind(nx, passwd) + if err != nil { + log.Debug("LDAP Authan failed for %s, reason: %s", nx, err.Error()) + return "", false + } + + search := goldap.NewSearchRequest( + ls.BaseDN, + goldap.ScopeWholeSubtree, goldap.NeverDerefAliases, 0, 0, false, + fmt.Sprintf(ls.Filter, name), + []string{ls.Attributes}, + nil) + sr, err := l.Search(search) + if err != nil { + log.Debug("LDAP Authen OK but not in filter %s", name) + return "", false + } + log.Debug("LDAP Authen OK: %s", name) + if len(sr.Entries) > 0 { + r := sr.Entries[0].GetAttributeValue(ls.Attributes) + return r, true + } + return "", true +} diff --git a/modules/base/conf.go b/modules/base/conf.go index abb67f6d8..572450450 100644 --- a/modules/base/conf.go +++ b/modules/base/conf.go @@ -10,6 +10,7 @@ import ( "os/exec" "path" "path/filepath" + "regexp" "strings" "github.com/Unknwon/com" @@ -19,6 +20,7 @@ import ( "github.com/gogits/cache" "github.com/gogits/session" + "github.com/gogits/gogs/modules/auth/ldap" "github.com/gogits/gogs/modules/log" ) @@ -51,6 +53,7 @@ var ( Domain string SecretKey string RunUser string + LdapAuth bool RepoRootPath string ScriptType string @@ -83,13 +86,13 @@ var ( ) var Service struct { - RegisterEmailConfirm bool - DisableRegistration bool - RequireSignInView bool - EnableCacheAvatar bool - NotifyMail bool - ActiveCodeLives int - ResetPwdCodeLives int + RegisterEmailConfirm bool + DisableRegistration bool + RequireSignInView bool + EnableCacheAvatar bool + NotifyMail bool + ActiveCodeLives int + ResetPwdCodeLives int } func ExecDir() (string, error) { @@ -310,6 +313,33 @@ func NewConfigContext() { CookieUserName = Cfg.MustValue("security", "COOKIE_USERNAME") CookieRememberName = Cfg.MustValue("security", "COOKIE_REMEMBER_NAME") + // load LDAP authentication configuration if present + LdapAuth = Cfg.MustBool("security", "LDAP_AUTH", false) + if LdapAuth { + log.Debug("LDAP AUTHENTICATION activated") + nbsrc := 0 + for _, v := range Cfg.GetSectionList() { + if matched, _ := regexp.MatchString("(?i)^LDAPSOURCE.*", v); matched { + ldapname := Cfg.MustValue(v, "name", v) + ldaphost := Cfg.MustValue(v, "host") + ldapport := Cfg.MustInt(v, "port", 389) + ldapbasedn := Cfg.MustValue(v, "basedn", "dc=*,dc=*") + ldapattribute := Cfg.MustValue(v, "attribute", "mail") + ldapfilter := Cfg.MustValue(v, "filter", "(*)") + ldapmsadsaformat := Cfg.MustValue(v, "MSADSAFORMAT", "%s") + ldap.AddSource(ldapname, ldaphost, ldapport, ldapbasedn, ldapattribute, ldapfilter, ldapmsadsaformat) + nbsrc += 1 + log.Debug("%s added as LDAP source", ldapname) + } + } + if nbsrc == 0 { + log.Debug("No valide LDAP found, LDAP AUTHENTICATION NOT activated") + LdapAuth = false + } + } else { + log.Debug("LDAP AUTHENTICATION NOT activated") + } + PictureService = Cfg.MustValue("picture", "SERVICE") // Determine and create root git reposiroty path. diff --git a/routers/user/user.go b/routers/user/user.go index 7decd72d4..75314237d 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -89,7 +89,16 @@ func SignInPost(ctx *middleware.Context, form auth.LogInForm) { return } - user, err := models.LoginUserPlain(form.UserName, form.Password) + var user *models.User + var err error + // try to login against LDAP if defined + if base.LdapAuth { + user, err = models.LoginUserLdap(form.UserName, form.Password) + } + // try local if not LDAP or it's failed + if (!base.LdapAuth) || (err != nil) { + user, err = models.LoginUserPlain(form.UserName, form.Password) + } if err != nil { if err == models.ErrUserNotExist { log.Trace("%s Log in failed: %s/%s", ctx.Req.RequestURI, form.UserName, form.Password) From 184f1ae135d37ea33795f5140047467d718af545 Mon Sep 17 00:00:00 2001 From: Michael Dyrynda Date: Thu, 24 Apr 2014 08:20:23 +0930 Subject: [PATCH 12/29] fixed typo --- dockerfiles/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dockerfiles/README.md b/dockerfiles/README.md index cbf582d0d..a869c5f00 100644 --- a/dockerfiles/README.md +++ b/dockerfiles/README.md @@ -1,6 +1,6 @@ ### Install Gogs With Docker -Deplying gogs in [Docker](http://www.docker.io/) is just as easy as eating a pie, what you do is just open the `dockerfiles/build.sh` file, replace the confis: +Deplying gogs in [Docker](http://www.docker.io/) is just as easy as eating a pie, what you do is just open the `dockerfiles/build.sh` file, replace the configs: ``` DB_TYPE="YOUR_DB_TYPE" # type of database, support 'mysql' and 'postgres' @@ -37,4 +37,4 @@ http://YOUR_HOST_IP:YOUR_HOST_PORT ``` Let's 'gogs'! -Ouya~ \ No newline at end of file +Ouya~ From eb4691cb2f11665fe39b64a860145a7ec73674e1 Mon Sep 17 00:00:00 2001 From: Michael Dyrynda Date: Thu, 24 Apr 2014 09:12:34 +0930 Subject: [PATCH 13/29] Missed one typo while fixing another... on the same line. Deplorable effort on my part! @bradleyfalzon --- dockerfiles/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dockerfiles/README.md b/dockerfiles/README.md index a869c5f00..fc27bdf4f 100644 --- a/dockerfiles/README.md +++ b/dockerfiles/README.md @@ -1,6 +1,6 @@ ### Install Gogs With Docker -Deplying gogs in [Docker](http://www.docker.io/) is just as easy as eating a pie, what you do is just open the `dockerfiles/build.sh` file, replace the configs: +Deploying gogs in [Docker](http://www.docker.io/) is just as easy as eating a pie, what you do is just open the `dockerfiles/build.sh` file, replace the configs: ``` DB_TYPE="YOUR_DB_TYPE" # type of database, support 'mysql' and 'postgres' From e5af34a0787f2b5fd8a4da8571659af545da54a4 Mon Sep 17 00:00:00 2001 From: James Anderson Date: Thu, 24 Apr 2014 14:50:24 -0400 Subject: [PATCH 14/29] fixed some broken forms that were introduced in james-anderson@521c5f0e10 --- routers/user/setting.go | 6 +++--- templates/user/password.tmpl | 2 +- templates/user/publickey.tmpl | 2 +- templates/user/setting.tmpl | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/routers/user/setting.go b/routers/user/setting.go index a8fdc116c..019044c53 100644 --- a/routers/user/setting.go +++ b/routers/user/setting.go @@ -66,7 +66,7 @@ func SettingPost(ctx *middleware.Context, form auth.UpdateProfileForm) { log.Trace("%s User setting updated: %s", ctx.Req.RequestURI, ctx.User.LowerName) ctx.Flash.Success("Your profile has been successfully updated.") - ctx.Redirect("/user/setting") + ctx.Redirect("/user/settings") } func SettingSocial(ctx *middleware.Context) { @@ -122,7 +122,7 @@ func SettingPasswordPost(ctx *middleware.Context, form auth.UpdatePasswdForm) { ctx.Flash.Success("Password is changed successfully. You can now sign in via new password.") } - ctx.Redirect("/user/setting/password") + ctx.Redirect("/user/settings/password") } func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) { @@ -181,7 +181,7 @@ func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) { } else { log.Trace("%s User SSH key added: %s", ctx.Req.RequestURI, ctx.User.LowerName) ctx.Flash.Success("New SSH Key has been added!") - ctx.Redirect("/user/setting/ssh") + ctx.Redirect("/user/settings/ssh") return } } diff --git a/templates/user/password.tmpl b/templates/user/password.tmpl index cba9cce0c..c55d27fa1 100644 --- a/templates/user/password.tmpl +++ b/templates/user/password.tmpl @@ -5,7 +5,7 @@

Password

-
+ {{.CsrfTokenHtml}} {{template "base/alert" .}}
diff --git a/templates/user/publickey.tmpl b/templates/user/publickey.tmpl index 8eb7be487..bf9aac113 100644 --- a/templates/user/publickey.tmpl +++ b/templates/user/publickey.tmpl @@ -21,7 +21,7 @@