Skip to content

Commit 159bc88

Browse files
zeripath6543
andauthored
Restore PAM user autocreation functionality (#15825) (#15867)
Backport #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]> Co-authored-by: 6543 <[email protected]>
1 parent 4b771d3 commit 159bc88

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,

modules/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

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,6 +2281,7 @@ auths.allowed_domains_helper = Leave empty to allow all domains. Separate multip
22812281
auths.enable_tls = Enable TLS Encryption
22822282
auths.skip_tls_verify = Skip TLS Verify
22832283
auths.pam_service_name = PAM Service Name
2284+
auths.pam_email_domain = PAM Email Domain (optional)
22842285
auths.oauth2_provider = OAuth2 Provider
22852286
auths.oauth2_icon_url = Icon URL
22862287
auths.oauth2_clientID = Client ID (Key)

routers/admin/auths.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ func NewAuthSourcePost(ctx *context.Context) {
239239
case models.LoginPAM:
240240
config = &models.PAMConfig{
241241
ServiceName: form.PAMServiceName,
242+
EmailDomain: form.PAMEmailDomain,
242243
}
243244
case models.LoginOAuth2:
244245
config = parseOAuth2Config(form)
@@ -346,6 +347,7 @@ func EditAuthSourcePost(ctx *context.Context) {
346347
case models.LoginPAM:
347348
config = &models.PAMConfig{
348349
ServiceName: form.PAMServiceName,
350+
EmailDomain: form.PAMEmailDomain,
349351
}
350352
case models.LoginOAuth2:
351353
config = parseOAuth2Config(form)

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)