-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (129 loc) · 3.74 KB
/
main.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
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/hashicorp/go-hclog"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/netauth/netauth/pkg/netauth"
"github.com/netauth/netauth/pkg/netauth/subtle"
)
var (
cfgfile = pflag.String("config", "", "Config file to use")
verbose = pflag.Bool("verbose", false, "Show logs")
)
type userFilters struct {
DeniedLoginMethods []string `json:"denied_login_methods,omitempty"`
}
type minimalSFTPGoUser struct {
Status int `json:"status,omitempty"`
Username string `json:"username"`
HomeDir string `json:"home_dir,omitempty"`
UID int `json:"uid,omitempty"`
GID int `json:"gid,omitempty"`
Permissions map[string][]string `json:"permissions"`
Filters userFilters `json:"filters"`
}
func dumpUser(u minimalSFTPGoUser) {
resp, _ := json.Marshal(u)
fmt.Printf("%v\n", string(resp))
}
// doAuth fetches all the required informatino from the environment
// and then runs various queries to the netauth server to determine if
// the user should be allowed. Checks in order are: entity is extant,
// unlocked, member of an optional group, has presented a valid public
// key, has presented a valid password.
//
// In a single pass only one authentication method (pubkey or
// password) will be checked. This is to facilitate multi-call auth
// per the sftpgo spec.
func doAuth(c *netauth.Client) {
ctx := context.Background()
// Fish things out of the environment
username := os.Getenv("SFTPGO_AUTHD_USERNAME")
password := os.Getenv("SFTPGO_AUTHD_PASSWORD")
publickey := os.Getenv("SFTPGO_AUTHD_PUBLIC_KEY")
requireGroup := os.Getenv("SFTPGO_NETAUTH_REQUIREGROUP")
entity, err := c.EntityInfo(ctx, username)
if status.Code(err) != codes.OK || entity.GetMeta().GetLocked() {
dumpUser(minimalSFTPGoUser{})
return
}
if len(requireGroup) > 0 {
groups, err := c.EntityGroups(ctx, username)
if status.Code(err) != codes.OK {
dumpUser(minimalSFTPGoUser{})
return
}
set := make(map[string]struct{}, len(groups))
for _, group := range groups {
set[group.GetName()] = struct{}{}
}
if _, ok := set[requireGroup]; !ok {
dumpUser(minimalSFTPGoUser{})
return
}
}
if len(publickey) > 0 {
keys, err := c.EntityKeys(ctx, username, "READ", "SSH", "")
if status.Code(err) != codes.OK {
dumpUser(minimalSFTPGoUser{})
return
}
err = subtle.CompareSSHKeys(keys["SSH"], publickey)
if err != nil {
dumpUser(minimalSFTPGoUser{})
return
}
} else {
err := c.AuthEntity(ctx, username, password)
if status.Code(err) != codes.OK {
dumpUser(minimalSFTPGoUser{})
return
}
}
u := minimalSFTPGoUser{
Status: 1,
Username: username,
UID: int(entity.GetNumber()),
HomeDir: filepath.Join(os.Getenv("SFTPGO_NETAUTH_HOMEDIR"), username),
Permissions: map[string][]string{"/": []string{"*"}},
}
dumpUser(u)
}
func main() {
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
if *cfgfile != "" {
viper.SetConfigFile(*cfgfile)
} else {
viper.SetConfigName("config")
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME/.netauth")
viper.AddConfigPath("/etc/netauth/")
}
if err := viper.ReadInConfig(); err != nil {
fmt.Println("Error reading config:", err)
dumpUser(minimalSFTPGoUser{})
os.Exit(1)
}
// Shut off all the logging
if !*verbose {
hclog.SetDefault(hclog.NewNullLogger())
}
l := hclog.L().Named("netkeys")
c, err := netauth.New()
if err != nil {
l.Warn("Error during client initialization:", "error", err)
dumpUser(minimalSFTPGoUser{})
os.Exit(1)
}
// Set the service ID
c.SetServiceName("sftpgo")
doAuth(c)
}