Repository navigation
Expand file tree
/
Copy pathdeauth.go
More file actions
200 lines (171 loc) · 7.75 KB
/
Copy pathdeauth.go
File metadata and controls
200 lines (171 loc) · 7.75 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package cli
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"github.com/urfave/cli/v3"
"github.com/umatare5/cisco-wnc-cli/internal/config"
"github.com/umatare5/cisco-wnc-cli/internal/wnc"
)
// deauthCommand drops a client's session. It is a flat tree rather than a reset leaf: that tree
// restarts an access point and keys every leaf on a name the controller holds, and a client has
// neither.
func deauthCommand() *cli.Command {
return &cli.Command{
Name: "deauth",
Usage: "Deauthenticate a client on a controller",
UsageText: synopsisChoice(config.FlagMAC, config.FlagUsername),
Description: "--mac and --username are the mac and username columns of wnc show client,\n" +
"and they select different clients, so give only one. The controller resolves\n" +
"the target first, so a value it holds no client at is refused before the RPC,\n" +
"which answers the same whether or not a client was there. A username may hold\n" +
"more than one session, and the prompt says how many. The client is dropped and\n" +
"re-associates on its own, on a timer its supplicant sets rather than the\n" +
"controller, so allow about four minutes. The operation is absent before 17.15.6.\n" +
"Pass --dry-run to name the target and change nothing.",
Flags: append(execFlags(), macFlag(), clientUsernameFlag(), yesFlag()),
Action: runDeauth,
}
}
// macFlag names the client this leaf acts on. It declares no Sources, for the reason
// apNameFlag gives.
func macFlag() cli.Flag {
return &cli.StringFlag{
Name: config.FlagMAC,
Usage: "client MAC address, as wnc show client --columns mac prints it",
}
}
// clientUsernameFlag names the client's own username, which is not the controller account
// generate-token asks for under the same flag name. Declaring no Sources keeps the two apart:
// WNC_USERNAME supplies that one, and reading it here would let an exported controller
// login select whichever clients happen to carry it.
func clientUsernameFlag() cli.Flag {
return &cli.StringFlag{
Name: config.FlagUsername,
Usage: "client username, as wnc show client --columns username prints it",
}
}
// runDeauth drops one arm's worth of sessions. The read before the post is the guard, not a
// convenience. The RPC answers 204 for a target with no session exactly as it does for one it
// dropped, so without that read a reported deauthentication and a wrong target are the same
// output.
func runDeauth(ctx context.Context, cmd *cli.Command) error {
flag, value, err := requireDeauthTarget(cmd)
if err != nil {
return err
}
target, client, err := actionPrologue(ctx, cmd)
if err != nil {
return err
}
if flag == config.FlagUsername {
return deauthByUsername(ctx, cmd, client, target, value)
}
return deauthByMAC(ctx, cmd, client, target, value)
}
// deauthByMAC drops the one session at an address. The prompt, the report and the wire all use the
// address the row carries, because the controller already serves it in the form the SDK
// normalizes to.
func deauthByMAC(
ctx context.Context, cmd *cli.Command, client *wnc.Client, target config.Target, mac string,
) error {
wire, found, err := client.ClientByMAC(ctx, mac)
if err != nil {
if cause, _ := wnc.Classify(err); cause == wnc.CauseNotFound {
return absentClient(target, mac)
}
return readFailure(target, err)
}
if !found {
return absentClient(target, mac)
}
return confirmAndAct(ctx, cmd, writeWording{
question: "Deauthenticate %s on %s? It is dropped and reconnects on its own. [y/N]: ",
dryRun: "%s on %s: would deauthenticate\n",
sent: "%s on %s: deauthenticate sent\n",
failed: "deauthenticating %s on %s",
}, func(ctx context.Context) error {
return absentOperation(client.DeauthenticateClientByMAC(ctx, wire))
}, wire, target.Name)
}
// deauthByUsername drops every session under a username. The RPC's leaf states no cardinality, so
// the prompt names the number the controller holds. The collection read answers no 404 the way the
// keyed one does, so a failure here is a failure to read and not an absent client.
func deauthByUsername(
ctx context.Context, cmd *cli.Command, client *wnc.Client, target config.Target, username string,
) error {
sessions, err := client.ClientsByUsername(ctx, username)
if err != nil {
return readFailure(target, err)
}
if sessions == 0 {
return fmt.Errorf("%s holds no client authenticated as %s", target.Name, username)
}
return confirmAndAct(ctx, cmd, writeWording{
question: "Deauthenticate %s on %s? Each is dropped and reconnects on its own. [y/N]: ",
dryRun: "%s on %s: would deauthenticate\n",
sent: "%s on %s: deauthenticate sent\n",
failed: "deauthenticating %s on %s",
}, func(ctx context.Context) error {
return absentOperation(client.DeauthenticateClientByUsername(ctx, username))
}, underUsername(sessions, username), target.Name)
}
// underUsername words the resolved target of the username arm. It is a count and not an address
// because the controller canonicalizes no username, so the number of sessions is the only thing
// the read adds to what the operator typed.
func underUsername(sessions int, username string) string {
if sessions == 1 {
return "1 client authenticated as " + username
}
return fmt.Sprintf("%d clients authenticated as %s", sessions, username)
}
// readFailure words a resolve that did not answer. Both arms read the same collection, so both
// name it, and Message keeps the response body an APIError carries out of the line.
func readFailure(target config.Target, err error) error {
return fmt.Errorf("reading common-oper-data from %s: %s", target.Name, wnc.Message(err))
}
// absentClient words an address the controller holds no client at. The 404 the keyed read
// answers and a 200 carrying no row reach here differently and mean the same thing, so they are
// reported the same way – the shape absentAP takes for a name.
func absentClient(target config.Target, mac string) error {
return fmt.Errorf("%s holds no client at %s", target.Name, mac)
}
// requireDeauthTarget reads the arm of the RPC's mandatory choice this invocation fills, and not
// through requireOne, whose message calls the target a name. Only presence and emptiness are
// checked. The SDK normalizes the address spellings, the username leaf is a bare string the schema
// restricts in no way, and an empty username is the value most clients carry.
func requireDeauthTarget(cmd *cli.Command) (flag, value string, err error) {
macs, usernames := cmd.Count(config.FlagMAC), cmd.Count(config.FlagUsername)
switch {
case macs == 0 && usernames == 0:
return "", "", fmt.Errorf("%w: %s requires --%s or --%s: the client's address or its username",
ErrUsage, cmd.Name, config.FlagMAC, config.FlagUsername)
case macs > 0 && usernames > 0:
return "", "", fmt.Errorf("%w: --%s and --%s select different clients, so give only one of them",
ErrUsage, config.FlagMAC, config.FlagUsername)
}
flag, noun := config.FlagMAC, "client"
if usernames > 0 {
flag, noun = config.FlagUsername, "username"
}
if n := cmd.Count(flag); n > 1 {
return "", "", fmt.Errorf("%w: one %s per invocation, --%s given %d times",
ErrUsage, noun, flag, n)
}
value = cmd.String(flag)
if strings.TrimSpace(value) == "" {
return "", "", fmt.Errorf("%w: --%s must not be empty", ErrUsage, flag)
}
return flag, value, nil
}
// absentOperation re-words the answer a release that does not serve the operation gives. The target
// was resolved a moment earlier on the same controller, so a rejected path is the reading left, and
// Message would otherwise report the status alone.
func absentOperation(err error) error {
if cause, status := wnc.Classify(err); cause == wnc.CauseHTTP && status == http.StatusBadRequest {
return errors.New("the controller rejected the operation: a release before 17.15.6 serves no client delete")
}
return err
}