forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclients.go
177 lines (148 loc) · 4.61 KB
/
clients.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
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
/*
Velociraptor - Dig Deeper
Copyright (C) 2019-2024 Rapid7 Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package api
import (
"context"
"errors"
"os"
"time"
"github.com/Velocidex/ordereddict"
"google.golang.org/protobuf/types/known/emptypb"
"www.velocidex.com/golang/velociraptor/acls"
api_proto "www.velocidex.com/golang/velociraptor/api/proto"
"www.velocidex.com/golang/velociraptor/services"
)
func (self *ApiServer) GetClientMetadata(
ctx context.Context,
in *api_proto.GetClientRequest) (*api_proto.ClientMetadata, error) {
users := services.GetUserManager()
user_record, org_config_obj, err := users.GetUserFromContext(ctx)
if err != nil {
return nil, Status(self.verbose, err)
}
user_name := user_record.Name
permissions := acls.READ_RESULTS
if in.ClientId == "server" {
permissions = acls.SERVER_ADMIN
}
perm, err := services.CheckAccess(org_config_obj, user_name, permissions)
if !perm || err != nil {
return nil, PermissionDenied(err,
"User is not allowed to view clients.")
}
client_info_manager, err := services.GetClientInfoManager(org_config_obj)
if err != nil {
return nil, Status(self.verbose, err)
}
result := &api_proto.ClientMetadata{
ClientId: in.ClientId,
}
client_metadata, err := client_info_manager.GetMetadata(ctx, in.ClientId)
if errors.Is(err, os.ErrNotExist) {
// Metadata not set, start with empty set.
err = nil
}
if err != nil {
return nil, Status(self.verbose, err)
}
for _, k := range client_metadata.Keys() {
v, _ := client_metadata.GetString(k)
if v != "" {
result.Items = append(result.Items,
&api_proto.ClientMetadataItem{
Key: k,
Value: v,
})
}
}
return result, nil
}
func (self *ApiServer) SetClientMetadata(
ctx context.Context,
in *api_proto.SetClientMetadataRequest) (*emptypb.Empty, error) {
users := services.GetUserManager()
user_record, org_config_obj, err := users.GetUserFromContext(ctx)
if err != nil {
return nil, Status(self.verbose, err)
}
user_name := user_record.Name
permissions := acls.LABEL_CLIENT
perm, err := services.CheckAccess(org_config_obj, user_name, permissions)
if !perm || err != nil {
return nil, PermissionDenied(err,
"User is not allowed to modify client labels.")
}
client_info_manager, err := services.GetClientInfoManager(org_config_obj)
if err != nil {
return nil, Status(self.verbose, err)
}
metadata := ordereddict.NewDict()
for _, env := range in.Add {
metadata.Set(env.Key, env.Value)
}
for _, key := range in.Remove {
_, pres := metadata.Get(key)
if !pres {
metadata.Set(key, nil)
}
}
err = client_info_manager.SetMetadata(ctx, in.ClientId, metadata, user_name)
return &emptypb.Empty{}, Status(self.verbose, err)
}
func (self *ApiServer) GetClient(
ctx context.Context,
in *api_proto.GetClientRequest) (*api_proto.ApiClient, error) {
users := services.GetUserManager()
user_record, org_config_obj, err := users.GetUserFromContext(ctx)
if err != nil {
return nil, Status(self.verbose, err)
}
user_name := user_record.Name
permissions := acls.READ_RESULTS
perm, err := services.CheckAccess(org_config_obj, user_name, permissions)
if !perm || err != nil {
return nil, PermissionDenied(err,
"User is not allowed to view clients.")
}
indexer, err := services.GetIndexer(org_config_obj)
if err != nil {
return nil, Status(self.verbose, err)
}
// Update the user's MRU
if in.UpdateMru {
err = indexer.UpdateMRU(org_config_obj, user_name, in.ClientId)
if err != nil {
return nil, Status(self.verbose, err)
}
}
api_client, err := indexer.FastGetApiClient(ctx, org_config_obj, in.ClientId)
if err != nil {
return &api_proto.ApiClient{}, nil
}
if self.server_obj != nil {
if !in.Lightweight {
// Wait up to 2 seconds to find out if clients are connected.
notifier, err := services.GetNotifier(org_config_obj)
if err != nil {
return nil, Status(self.verbose, err)
}
if notifier.IsClientConnected(ctx,
org_config_obj, in.ClientId, 2) {
api_client.LastSeenAt = uint64(time.Now().UnixNano() / 1000)
}
}
}
return api_client, nil
}