1/*2Maddy Mail Server - Composable all-in-one email server.3Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors45This program is free software: you can redistribute it and/or modify6it under the terms of the GNU General Public License as published by7the Free Software Foundation, either version 3 of the License, or8(at your option) any later version.910This program is distributed in the hope that it will be useful,11but WITHOUT ANY WARRANTY; without even the implied warranty of12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13GNU General Public License for more details.1415You should have received a copy of the GNU General Public License16along with this program. If not, see <https://www.gnu.org/licenses/>.17*/1819package shadow2021import (22 "errors"23 "fmt"24 "time"2526 "github.com/GehirnInc/crypt"27 _ "github.com/GehirnInc/crypt/sha256_crypt"28 _ "github.com/GehirnInc/crypt/sha512_crypt"29)3031const secsInDay = 864003233func (e *Entry) IsAccountValid() bool {34 if e.AcctExpiry == -1 {35 return true36 }3738 nowDays := int(time.Now().Unix() / secsInDay)39 return nowDays < e.AcctExpiry40}4142func (e *Entry) IsPasswordValid() bool {43 if e.LastChange == -1 || e.MaxPassAge == -1 || e.InactivityPeriod == -1 {44 return true45 }4647 nowDays := int(time.Now().Unix() / secsInDay)48 return nowDays < e.LastChange+e.MaxPassAge+e.InactivityPeriod49}5051func (e *Entry) VerifyPassword(pass string) (err error) {52 // Do not permit null and locked passwords.53 if e.Pass == "" {54 return errors.New("verify: null password")55 }56 if e.Pass[0] == '!' {57 return errors.New("verify: locked password")58 }5960 // crypt.NewFromHash may panic on unknown hash function.61 defer func() {62 if rcvr := recover(); rcvr != nil {63 err = fmt.Errorf("%v", rcvr)64 }65 }()6667 if err := crypt.NewFromHash(e.Pass).Verify(e.Pass, []byte(pass)); err != nil {68 if errors.Is(err, crypt.ErrKeyMismatch) {69 return ErrWrongPassword70 }71 return err72 }73 return nil74}