Skip to content

Commit d234d37

Browse files
authored
Restore PAM user autocreation functionality (#15825)
* Restore PAM user autocreation functionality PAM autoregistration of users currently fails due to email invalidity. This PR adds a new setting to PAM to allow an email domain to be set or just sets the email to the noreply address and if that fails falls back to uuid@localhost Fix #15702 Signed-off-by: Andrew Thornton <[email protected]> * As per KN4CKER Signed-off-by: Andrew Thornton <[email protected]>
1 parent 9545c34 commit d234d37

File tree

6 files changed

+24
-1
lines changed

6 files changed

+24
-1
lines changed

models/login_source.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"code.gitea.io/gitea/modules/setting"
2222
"code.gitea.io/gitea/modules/timeutil"
2323
"code.gitea.io/gitea/modules/util"
24+
gouuid "github.com/google/uuid"
2425
jsoniter "github.com/json-iterator/go"
2526

2627
"xorm.io/xorm"
@@ -116,6 +117,7 @@ func (cfg *SMTPConfig) ToDB() ([]byte, error) {
116117
// PAMConfig holds configuration for the PAM login source.
117118
type PAMConfig struct {
118119
ServiceName string // pam service (e.g. system-auth)
120+
EmailDomain string
119121
}
120122

121123
// FromDB fills up a PAMConfig from serialized format.
@@ -696,15 +698,26 @@ func LoginViaPAM(user *User, login, password string, sourceID int64, cfg *PAMCon
696698

697699
// Allow PAM sources with `@` in their name, like from Active Directory
698700
username := pamLogin
701+
email := pamLogin
699702
idx := strings.Index(pamLogin, "@")
700703
if idx > -1 {
701704
username = pamLogin[:idx]
702705
}
706+
if ValidateEmail(email) != nil {
707+
if cfg.EmailDomain != "" {
708+
email = fmt.Sprintf("%s@%s", username, cfg.EmailDomain)
709+
} else {
710+
email = fmt.Sprintf("%s@%s", username, setting.Service.NoReplyAddress)
711+
}
712+
if ValidateEmail(email) != nil {
713+
email = gouuid.New().String() + "@localhost"
714+
}
715+
}
703716

704717
user = &User{
705718
LowerName: strings.ToLower(username),
706719
Name: username,
707-
Email: pamLogin,
720+
Email: email,
708721
Passwd: password,
709722
LoginType: LoginPAM,
710723
LoginSource: sourceID,

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2313,6 +2313,7 @@ auths.allowed_domains_helper = Leave empty to allow all domains. Separate multip
23132313
auths.enable_tls = Enable TLS Encryption
23142314
auths.skip_tls_verify = Skip TLS Verify
23152315
auths.pam_service_name = PAM Service Name
2316+
auths.pam_email_domain = PAM Email Domain (optional)
23162317
auths.oauth2_provider = OAuth2 Provider
23172318
auths.oauth2_icon_url = Icon URL
23182319
auths.oauth2_clientID = Client ID (Key)

routers/admin/auths.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ func NewAuthSourcePost(ctx *context.Context) {
240240
case models.LoginPAM:
241241
config = &models.PAMConfig{
242242
ServiceName: form.PAMServiceName,
243+
EmailDomain: form.PAMEmailDomain,
243244
}
244245
case models.LoginOAuth2:
245246
config = parseOAuth2Config(form)
@@ -347,6 +348,7 @@ func EditAuthSourcePost(ctx *context.Context) {
347348
case models.LoginPAM:
348349
config = &models.PAMConfig{
349350
ServiceName: form.PAMServiceName,
351+
EmailDomain: form.PAMEmailDomain,
350352
}
351353
case models.LoginOAuth2:
352354
config = parseOAuth2Config(form)

services/forms/auth_form.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type AuthenticationForm struct {
5151
TLS bool
5252
SkipVerify bool
5353
PAMServiceName string
54+
PAMEmailDomain string
5455
Oauth2Provider string
5556
Oauth2Key string
5657
Oauth2Secret string

templates/admin/auth/edit.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@
188188
<label for="pam_service_name">{{.i18n.Tr "admin.auths.pam_service_name"}}</label>
189189
<input id="pam_service_name" name="pam_service_name" value="{{$cfg.ServiceName}}" required>
190190
</div>
191+
<div class="field">
192+
<label for="pam_email_domain">{{.i18n.Tr "admin.auths.pam_email_domain"}}</label>
193+
<input id="pam_email_domain" name="pam_email_domain" value="{{$cfg.EmailDomain}}">
194+
</div>
191195
{{end}}
192196

193197
<!-- OAuth2 -->

templates/admin/auth/new.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
<div class="pam required field {{if not (eq .type 4)}}hide{{end}}">
3939
<label for="pam_service_name">{{.i18n.Tr "admin.auths.pam_service_name"}}</label>
4040
<input id="pam_service_name" name="pam_service_name" value="{{.pam_service_name}}" />
41+
<label for="pam_email_domain">{{.i18n.Tr "admin.auths.pam_email_domain"}}</label>
42+
<input id="pam_email_domain" name="pam_email_domain" value="{{.pam_email_domain}}">
4143
</div>
4244

4345
<!-- OAuth2 -->

0 commit comments

Comments
 (0)