@@ -42,6 +42,7 @@ import (
4242 "yunion.io/x/onecloud/pkg/util/fstabutils"
4343 "yunion.io/x/onecloud/pkg/util/netutils2"
4444 "yunion.io/x/onecloud/pkg/util/procutils"
45+ "yunion.io/x/onecloud/pkg/util/pwquality"
4546 "yunion.io/x/onecloud/pkg/util/seclib2"
4647 "yunion.io/x/onecloud/pkg/util/sysutils"
4748)
@@ -226,7 +227,24 @@ func (l *sLinuxRootFs) GetLoginAccount(rootFs IDiskPartition, sUser string, defa
226227 return selUsr , nil
227228}
228229
229- func (l * sLinuxRootFs ) ChangeUserPasswd (rootFs IDiskPartition , account , gid , publicKey , password string ) (string , error ) {
230+ func (l * sLinuxRootFs ) checkInputPasswd (rootFs IDiskPartition , config * pwquality.Config , account , gid , publicKey , password string ) string {
231+ if config == nil {
232+ return password
233+ }
234+
235+ err := config .Validate (password , account )
236+ if err != nil && errors .Cause (err ) == pwquality .ErrPasswordTooWeak {
237+ log .Infof ("password %s too weak, try regenerate password" , password )
238+ npassword := config .GeneratePassword (seclib2 .RandomPassword2 )
239+ if len (npassword ) > 0 {
240+ log .Infof ("regenerate password %s" , npassword )
241+ password = npassword
242+ }
243+ }
244+ return password
245+ }
246+
247+ func (l * sLinuxRootFs ) ChangeUserPasswd (rootFs IDiskPartition , account , gid , publicKey , password string , isRandomPassword bool ) (string , error ) {
230248 var secret string
231249 var err error
232250 err = rootFs .Passwd (account , password , false )
@@ -1099,6 +1117,27 @@ func (d *sDebianLikeRootFs) DeployNetworkingScripts(rootFs IDiskPartition, nics
10991117 return rootFs .FilePutContents (fn , cmds .String (), false , false )
11001118}
11011119
1120+ func (r * sDebianLikeRootFs ) ChangeUserPasswd (rootFs IDiskPartition , account , gid , publicKey , password string , isRandomPassword bool ) (string , error ) {
1121+ if isRandomPassword {
1122+ var pwqualityConf * pwquality.Config
1123+ if rootFs .Exists ("/etc/security/pwquality.conf" , false ) {
1124+ pwConfig , err := rootFs .FileGetContents ("/etc/security/pwquality.conf" , false )
1125+ if err == nil {
1126+ pwqualityConf = pwquality .ParseConfig (pwConfig )
1127+ }
1128+ }
1129+ if rootFs .Exists ("/etc/pam.d/common-password" , false ) {
1130+ pamConfig , err := rootFs .FileGetContents ("/etc/pam.d/common-password" , false )
1131+ if err == nil {
1132+ pwqualityConf = pwquality .ParsePAMConfig (pamConfig , pwqualityConf )
1133+ }
1134+ }
1135+ password = r .checkInputPasswd (rootFs , pwqualityConf , account , gid , publicKey , password )
1136+ }
1137+
1138+ return r .sLinuxRootFs .ChangeUserPasswd (rootFs , account , gid , publicKey , password , isRandomPassword )
1139+ }
1140+
11021141type SDebianRootFs struct {
11031142 * sDebianLikeRootFs
11041143}
@@ -1394,6 +1433,26 @@ func (r *sRedhatLikeRootFs) Centos5DeployNetworkingScripts(rootFs IDiskPartition
13941433 return nil
13951434}
13961435
1436+ func (r * sRedhatLikeRootFs ) ChangeUserPasswd (rootFs IDiskPartition , account , gid , publicKey , password string , isRandomPassword bool ) (string , error ) {
1437+ if isRandomPassword {
1438+ var pwqualityConf * pwquality.Config
1439+ if rootFs .Exists ("/etc/security/pwquality.conf" , false ) {
1440+ pwConfig , err := rootFs .FileGetContents ("/etc/security/pwquality.conf" , false )
1441+ if err == nil {
1442+ pwqualityConf = pwquality .ParseConfig (pwConfig )
1443+ }
1444+ }
1445+ if rootFs .Exists ("/etc/pam.d/system-auth" , false ) {
1446+ pamConfig , err := rootFs .FileGetContents ("/etc/pam.d/system-auth" , false )
1447+ if err == nil {
1448+ pwqualityConf = pwquality .ParsePAMConfig (pamConfig , pwqualityConf )
1449+ }
1450+ }
1451+ password = r .checkInputPasswd (rootFs , pwqualityConf , account , gid , publicKey , password )
1452+ }
1453+ return r .sLinuxRootFs .ChangeUserPasswd (rootFs , account , gid , publicKey , password , isRandomPassword )
1454+ }
1455+
13971456func getMainNic (nics []* types.SServerNic ) * types.SServerNic {
13981457 for i := range nics {
13991458 if nics [i ].IsDefault {
@@ -2375,7 +2434,7 @@ func (d *SCoreOsRootFs) DeployFstabScripts(rootFs IDiskPartition, disks []*deplo
23752434 return nil
23762435}
23772436
2378- func (d * SCoreOsRootFs ) ChangeUserPasswd (rootFs IDiskPartition , account , gid , publicKey , password string ) (string , error ) {
2437+ func (d * SCoreOsRootFs ) ChangeUserPasswd (part IDiskPartition , account , gid , publicKey , password string , isRandomPassword bool ) (string , error ) {
23792438 keys := []string {}
23802439 if len (publicKey ) > 0 {
23812440 keys = append (keys , publicKey )
0 commit comments