summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2015-04-24 16:43:58 +0800
committerLunny Xiao <xiaolunwen@gmail.com>2015-04-24 16:43:58 +0800
commitf92bdf875b4ccb2a8eadaa571d112ebf653986d6 (patch)
tree7bdcb2774039667c0ff01deabd283840cd2aaa74
parent5de0b0c5cef74e762eb499b6517d0416f2c6bace (diff)
parent182003aa417ba67661c6743e0fabe6e1f67efd1c (diff)
downloadgitea-f92bdf875b4ccb2a8eadaa571d112ebf653986d6.tar.gz
gitea-f92bdf875b4ccb2a8eadaa571d112ebf653986d6.zip
Merge pull request #1226 from nice-software/wip/pam
Add PAM authentication
-rw-r--r--.travis.yml6
-rw-r--r--conf/locale/locale_en-US.ini1
-rw-r--r--models/login.go61
-rw-r--r--modules/auth/auth_form.go1
-rw-r--r--modules/auth/pam/pam.go35
-rw-r--r--modules/auth/pam/pam_stub.go15
-rw-r--r--public/ng/js/gogs.js7
-rw-r--r--routers/admin/auths.go8
-rw-r--r--templates/admin/auth/edit.tmpl6
-rw-r--r--templates/admin/auth/new.tmpl6
10 files changed, 144 insertions, 2 deletions
diff --git a/.travis.yml b/.travis.yml
index 4149e17316..113773d697 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -6,11 +6,13 @@ go:
- 1.4
- tip
-sudo: false
+before_install:
+ - sudo apt-get update -qq
+ - sudo apt-get install -y libpam-dev
script: go build -v
notifications:
email:
- u@gogs.io
- slack: gophercn:o5pSanyTeNhnfYc3QnG0X7Wx \ No newline at end of file
+ slack: gophercn:o5pSanyTeNhnfYc3QnG0X7Wx
diff --git a/conf/locale/locale_en-US.ini b/conf/locale/locale_en-US.ini
index 3fbc71cb14..8e768ae6ec 100644
--- a/conf/locale/locale_en-US.ini
+++ b/conf/locale/locale_en-US.ini
@@ -619,6 +619,7 @@ auths.smtp_auth = SMTP Authorization Type
auths.smtphost = SMTP Host
auths.smtpport = SMTP Port
auths.enable_tls = Enable TLS Encryption
+auths.pam_service_name = PAM Service Name
auths.enable_auto_register = Enable Auto Registration
auths.tips = Tips
auths.edit = Edit Authorization Setting
diff --git a/models/login.go b/models/login.go
index 73d112568b..10f782beec 100644
--- a/models/login.go
+++ b/models/login.go
@@ -17,6 +17,7 @@ import (
"github.com/go-xorm/xorm"
"github.com/gogits/gogs/modules/auth/ldap"
+ "github.com/gogits/gogs/modules/auth/pam"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/uuid"
)
@@ -28,6 +29,7 @@ const (
PLAIN
LDAP
SMTP
+ PAM
)
var (
@@ -39,12 +41,14 @@ var (
var LoginTypes = map[LoginType]string{
LDAP: "LDAP",
SMTP: "SMTP",
+ PAM: "PAM",
}
// Ensure structs implemented interface.
var (
_ core.Conversion = &LDAPConfig{}
_ core.Conversion = &SMTPConfig{}
+ _ core.Conversion = &PAMConfig{}
)
type LDAPConfig struct {
@@ -74,6 +78,18 @@ func (cfg *SMTPConfig) ToDB() ([]byte, error) {
return json.Marshal(cfg)
}
+type PAMConfig struct {
+ ServiceName string // pam service (e.g. system-auth)
+}
+
+func (cfg *PAMConfig) FromDB(bs []byte) error {
+ return json.Unmarshal(bs, &cfg)
+}
+
+func (cfg *PAMConfig) ToDB() ([]byte, error) {
+ return json.Marshal(cfg)
+}
+
type LoginSource struct {
Id int64
Type LoginType
@@ -97,6 +113,10 @@ func (source *LoginSource) SMTP() *SMTPConfig {
return source.Cfg.(*SMTPConfig)
}
+func (source *LoginSource) PAM() *PAMConfig {
+ return source.Cfg.(*PAMConfig)
+}
+
func (source *LoginSource) BeforeSet(colName string, val xorm.Cell) {
if colName == "type" {
ty := (*val).(int64)
@@ -105,6 +125,8 @@ func (source *LoginSource) BeforeSet(colName string, val xorm.Cell) {
source.Cfg = new(LDAPConfig)
case SMTP:
source.Cfg = new(SMTPConfig)
+ case PAM:
+ source.Cfg = new(PAMConfig)
}
}
}
@@ -197,6 +219,13 @@ func UserSignIn(uname, passwd string) (*User, error) {
return u, nil
}
log.Warn("Fail to login(%s) by SMTP(%s): %v", uname, source.Name, err)
+ } else if source.Type == PAM {
+ u, err := LoginUserPAMSource(nil, uname, passwd,
+ source.Id, source.Cfg.(*PAMConfig), true)
+ if err == nil {
+ return u, nil
+ }
+ log.Warn("Fail to login(%s) by PAM(%s): %v", uname, source.Name, err)
}
}
@@ -218,6 +247,8 @@ func UserSignIn(uname, passwd string) (*User, error) {
return LoginUserLdapSource(u, u.LoginName, passwd, source.Id, source.Cfg.(*LDAPConfig), false)
case SMTP:
return LoginUserSMTPSource(u, u.LoginName, passwd, source.Id, source.Cfg.(*SMTPConfig), false)
+ case PAM:
+ return LoginUserPAMSource(u, u.LoginName, passwd, source.Id, source.Cfg.(*PAMConfig), false)
}
return nil, ErrUnsupportedLoginType
}
@@ -359,3 +390,33 @@ func LoginUserSMTPSource(u *User, name, passwd string, sourceId int64, cfg *SMTP
err := CreateUser(u)
return u, err
}
+
+// Query if name/passwd can login against PAM
+// Create a local user if success
+// Return the same LoginUserPlain semantic
+func LoginUserPAMSource(u *User, name, passwd string, sourceId int64, cfg *PAMConfig, autoRegister bool) (*User, error) {
+ if err := pam.PAMAuth(cfg.ServiceName, name, passwd); err != nil {
+ if strings.Contains(err.Error(), "Authentication failure") {
+ return nil, ErrUserNotExist
+ }
+ return nil, err
+ }
+
+ if !autoRegister {
+ return u, nil
+ }
+
+ // fake a local user creation
+ u = &User{
+ LowerName: strings.ToLower(name),
+ Name: strings.ToLower(name),
+ LoginType: PAM,
+ LoginSource: sourceId,
+ LoginName: name,
+ IsActive: true,
+ Passwd: passwd,
+ Email: name,
+ }
+ err := CreateUser(u)
+ return u, err
+}
diff --git a/modules/auth/auth_form.go b/modules/auth/auth_form.go
index 7d45999914..1102dc3492 100644
--- a/modules/auth/auth_form.go
+++ b/modules/auth/auth_form.go
@@ -30,6 +30,7 @@ type AuthenticationForm struct {
SMTPPort int `form:"smtp_port"`
TLS bool `form:"tls"`
AllowAutoRegister bool `form:"allowautoregister"`
+ PAMServiceName string
}
func (f *AuthenticationForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
diff --git a/modules/auth/pam/pam.go b/modules/auth/pam/pam.go
new file mode 100644
index 0000000000..7d150b1c0b
--- /dev/null
+++ b/modules/auth/pam/pam.go
@@ -0,0 +1,35 @@
+// +build !windows
+
+// Copyright 2014 The Gogs Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package pam
+
+import (
+ "errors"
+
+ "github.com/msteinert/pam"
+)
+
+func PAMAuth(serviceName, userName, passwd string) error {
+ t, err := pam.StartFunc(serviceName, userName, func(s pam.Style, msg string) (string, error) {
+ switch s {
+ case pam.PromptEchoOff:
+ return passwd, nil
+ case pam.PromptEchoOn, pam.ErrorMsg, pam.TextInfo:
+ return "", nil
+ }
+ return "", errors.New("Unrecognized PAM message style")
+ })
+
+ if err != nil {
+ return err
+ }
+
+ if err = t.Authenticate(0); err != nil {
+ return err
+ }
+
+ return nil
+}
diff --git a/modules/auth/pam/pam_stub.go b/modules/auth/pam/pam_stub.go
new file mode 100644
index 0000000000..2f210bf6e7
--- /dev/null
+++ b/modules/auth/pam/pam_stub.go
@@ -0,0 +1,15 @@
+// +build windows
+
+// Copyright 2014 The Gogs Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package pam
+
+import (
+ "errors"
+)
+
+func PAMAuth(serviceName, userName, passwd string) error {
+ return errors.New("PAM not supported")
+}
diff --git a/public/ng/js/gogs.js b/public/ng/js/gogs.js
index c5fd719c32..7ffef8af8b 100644
--- a/public/ng/js/gogs.js
+++ b/public/ng/js/gogs.js
@@ -753,10 +753,17 @@ function initAdmin() {
if (v == 2) {
$('.ldap').toggleShow();
$('.smtp').toggleHide();
+ $('.pam').toggleHide();
}
if (v == 3) {
$('.smtp').toggleShow();
$('.ldap').toggleHide();
+ $('.pam').toggleHide();
+ }
+ if (v == 4) {
+ $('.pam').toggleShow();
+ $('.smtp').toggleHide();
+ $('.ldap').toggleHide();
}
});
diff --git a/routers/admin/auths.go b/routers/admin/auths.go
index b13b0bd134..2bec7da46c 100644
--- a/routers/admin/auths.go
+++ b/routers/admin/auths.go
@@ -84,6 +84,10 @@ func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
Port: form.SMTPPort,
TLS: form.TLS,
}
+ case models.PAM:
+ u = &models.PAMConfig{
+ ServiceName: form.PAMServiceName,
+ }
default:
ctx.Error(400)
return
@@ -166,6 +170,10 @@ func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
Port: form.SMTPPort,
TLS: form.TLS,
}
+ case models.PAM:
+ config = &models.PAMConfig{
+ ServiceName: form.PAMServiceName,
+ }
default:
ctx.Error(400)
return
diff --git a/templates/admin/auth/edit.tmpl b/templates/admin/auth/edit.tmpl
index a178b71756..12d1d1f8f2 100644
--- a/templates/admin/auth/edit.tmpl
+++ b/templates/admin/auth/edit.tmpl
@@ -91,6 +91,12 @@
<label class="req" for="smtp_port">{{.i18n.Tr "admin.auths.smtpport"}}</label>
<input class="ipt ipt-large ipt-radius {{if .Err_SmtpPort}}ipt-error{{end}}" id="smtp_port" name="smtp_port" value="{{.Source.SMTP.Port}}" />
</div>
+
+ {{else if eq $type 4}}
+ <div class="field">
+ <label class="req" for="pam_service_name">{{.i18n.Tr "admin.auths.pam_service_name"}}</label>
+ <input class="ipt ipt-large ipt-radius {{if .Err_PAMServiceName}}ipt-error{{end}}" id="pam_service_name" name="pam_service_name" value="{{.Source.PAM.ServiceName}}" />
+ </div>
{{end}}
<div class="field">
diff --git a/templates/admin/auth/new.tmpl b/templates/admin/auth/new.tmpl
index 0d1f2ab417..36b90cfb48 100644
--- a/templates/admin/auth/new.tmpl
+++ b/templates/admin/auth/new.tmpl
@@ -86,6 +86,12 @@
<input class="ipt ipt-large ipt-radius {{if .Err_SmtpPort}}ipt-error{{end}}" id="smtp_port" name="smtp_port" value="{{.smtp_port}}" />
</div>
</div>
+ <div class="pam hidden">
+ <div class="field">
+ <label class="req" for="pam_service_name">{{.i18n.Tr "admin.auths.pam_service_name"}}</label>
+ <input class="ipt ipt-large ipt-radius {{if .Err_PAMServiceName}}ipt-error{{end}}" id="pam_service_name" name="pam_service_name" value="{{.pam_service_name}}" />
+ </div>
+ </div>
<div class="field">
<div class="smtp hidden">
<label></label>