@ -21,6 +21,7 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/util"
gouuid "github.com/google/uuid"
jsoniter "github.com/json-iterator/go"
jsoniter "github.com/json-iterator/go"
"xorm.io/xorm"
"xorm.io/xorm"
@ -116,6 +117,7 @@ func (cfg *SMTPConfig) ToDB() ([]byte, error) {
// PAMConfig holds configuration for the PAM login source.
// PAMConfig holds configuration for the PAM login source.
type PAMConfig struct {
type PAMConfig struct {
ServiceName string // pam service (e.g. system-auth)
ServiceName string // pam service (e.g. system-auth)
EmailDomain string
}
}
// FromDB fills up a PAMConfig from serialized format.
// FromDB fills up a PAMConfig from serialized format.
@ -696,15 +698,26 @@ func LoginViaPAM(user *User, login, password string, sourceID int64, cfg *PAMCon
// Allow PAM sources with `@` in their name, like from Active Directory
// Allow PAM sources with `@` in their name, like from Active Directory
username := pamLogin
username := pamLogin
email := pamLogin
idx := strings . Index ( pamLogin , "@" )
idx := strings . Index ( pamLogin , "@" )
if idx > - 1 {
if idx > - 1 {
username = pamLogin [ : idx ]
username = pamLogin [ : idx ]
}
}
if ValidateEmail ( email ) != nil {
if cfg . EmailDomain != "" {
email = fmt . Sprintf ( "%s@%s" , username , cfg . EmailDomain )
} else {
email = fmt . Sprintf ( "%s@%s" , username , setting . Service . NoReplyAddress )
}
if ValidateEmail ( email ) != nil {
email = gouuid . New ( ) . String ( ) + "@localhost"
}
}
user = & User {
user = & User {
LowerName : strings . ToLower ( username ) ,
LowerName : strings . ToLower ( username ) ,
Name : username ,
Name : username ,
Email : pamLogin ,
Email : email ,
Passwd : password ,
Passwd : password ,
LoginType : LoginPAM ,
LoginType : LoginPAM ,
LoginSource : sourceID ,
LoginSource : sourceID ,