maddy

Fork https://github.com/foxcpp/maddy

git clone git://git.lin.moe/go/maddy.git

 1/*
 2Maddy Mail Server - Composable all-in-one email server.
 3Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
 4
 5This program is free software: you can redistribute it and/or modify
 6it under the terms of the GNU General Public License as published by
 7the Free Software Foundation, either version 3 of the License, or
 8(at your option) any later version.
 9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program.  If not, see <https://www.gnu.org/licenses/>.
17*/
18
19package shadow
20
21import (
22	"errors"
23	"fmt"
24	"time"
25
26	"github.com/GehirnInc/crypt"
27	_ "github.com/GehirnInc/crypt/sha256_crypt"
28	_ "github.com/GehirnInc/crypt/sha512_crypt"
29)
30
31const secsInDay = 86400
32
33func (e *Entry) IsAccountValid() bool {
34	if e.AcctExpiry == -1 {
35		return true
36	}
37
38	nowDays := int(time.Now().Unix() / secsInDay)
39	return nowDays < e.AcctExpiry
40}
41
42func (e *Entry) IsPasswordValid() bool {
43	if e.LastChange == -1 || e.MaxPassAge == -1 || e.InactivityPeriod == -1 {
44		return true
45	}
46
47	nowDays := int(time.Now().Unix() / secsInDay)
48	return nowDays < e.LastChange+e.MaxPassAge+e.InactivityPeriod
49}
50
51func (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	}
59
60	// 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	}()
66
67	if err := crypt.NewFromHash(e.Pass).Verify(e.Pass, []byte(pass)); err != nil {
68		if errors.Is(err, crypt.ErrKeyMismatch) {
69			return ErrWrongPassword
70		}
71		return err
72	}
73	return nil
74}