This repository has been archived by the owner on May 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
shadow.go
134 lines (117 loc) · 3.66 KB
/
shadow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package shadow
import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"github.com/ubuntu/aad-auth/internal/cache"
"github.com/ubuntu/aad-auth/internal/logger"
"github.com/ubuntu/aad-auth/internal/nss"
)
// Shadow is the nss shadow object.
type Shadow struct {
name string /* username */
passwd string /* user password */
lstchg int /* Date of last change */
min int /* Minimum number of days between changes. */
max int /* Maximum number of days between changes. */
warn int /* Number of days to warn user to change the password. */
inact int /* Number of days the account may be inactive. */
expire int /* Number of days since 1970-01-01 until account expires. */
}
// String creates a string with Shadow values.
func (s Shadow) String() string {
v := []string{
s.name,
s.passwd,
strconv.Itoa(s.lstchg),
strconv.Itoa(s.min),
strconv.Itoa(s.max),
strconv.Itoa(s.warn),
strconv.Itoa(s.inact),
strconv.Itoa(s.expire),
}
return strings.Join(v, ":")
}
// NewByName returns a passwd entry from a name.
func NewByName(ctx context.Context, name string, cacheOpts ...cache.Option) (s Shadow, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("failed to get a shadow entry from name %q: %w", name, err)
}
}()
logger.Debug(ctx, "Requesting a shadow entry matching name %q", name)
c, err := cache.New(ctx, cacheOpts...)
if err != nil {
return Shadow{}, nss.ConvertErr(err)
}
defer c.Close(ctx)
spw, err := c.GetShadowByName(ctx, name)
if err != nil {
return Shadow{}, nss.ConvertErr(err)
}
return Shadow{
name: spw.Name,
passwd: "*", // we want to prevent pam_unix using this field to use a cached account without calling pam_aad.
//passwd: spw.Password,
lstchg: spw.LastPwdChange,
min: spw.MinPwdAge,
max: spw.MaxPwdAge,
warn: spw.PwdWarnPeriod,
inact: spw.PwdInactivity,
expire: spw.ExpirationDate,
}, nil
}
var shadowIterationCache *cache.Cache
// StartEntryIteration open a new cache for iteration.
// This needs to be called prior to calling NextEntry and be closed with EndEntryIteration.
func StartEntryIteration(ctx context.Context, cacheOpts ...cache.Option) error {
if shadowIterationCache != nil {
return nss.ConvertErr(errors.New("shadow entry iteration already in progress. End it before starting a new one"))
}
c, err := cache.New(ctx, cacheOpts...)
if err != nil {
return nss.ConvertErr(err)
}
shadowIterationCache = c
return nil
}
// EndEntryIteration closes the underlying DB iterator.
func EndEntryIteration(ctx context.Context) error {
if shadowIterationCache == nil {
logger.Warn(ctx, "shadow entry iteration ended without initialization first")
return nil
}
c := shadowIterationCache
defer c.Close(ctx)
shadowIterationCache = nil
return nss.ConvertErr(c.CloseShadowIterator(ctx))
}
// NextEntry returns next available entry in Shadow. It will returns ENOENT from cache when the iteration is done.
// It automatically opens and close the cache on first/last iteration.
func NextEntry(ctx context.Context) (sp Shadow, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("failed to get a shadow entry: %w", err)
}
}()
logger.Debug(ctx, "get next shadow entry")
if shadowIterationCache == nil {
return Shadow{}, nss.ConvertErr(errors.New("shadow entry iteration called without initialization first"))
}
spw, err := shadowIterationCache.NextShadowEntry(ctx)
if err != nil {
return Shadow{}, nss.ConvertErr(err)
}
return Shadow{
name: spw.Name,
passwd: spw.Password,
lstchg: spw.LastPwdChange,
min: spw.MinPwdAge,
max: spw.MaxPwdAge,
warn: spw.PwdWarnPeriod,
inact: spw.PwdInactivity,
expire: spw.ExpirationDate,
}, nil
}