forked from lastpass/lastpass-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd-share.c
More file actions
362 lines (305 loc) · 8.83 KB
/
cmd-share.c
File metadata and controls
362 lines (305 loc) · 8.83 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* commands to manipulate shared folders
*
* Copyright (C) 2015 LastPass.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* In addition, as a special exception, the copyright holders grant you
* additional permission to link or combine this program with the OpenSSL
* library and distribute the resulting work. See the LICENSE.OpenSSL file
* in this distribution for more details.
*
* See LICENSE.OpenSSL for more details regarding this exception.
*/
#include "cmd.h"
#include "util.h"
#include "config.h"
#include "terminal.h"
#include "agent.h"
#include "kdf.h"
#include "endpoints.h"
#include "clipboard.h"
#include "upload-queue.h"
#include "process.h"
#include <getopt.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
struct share_args {
struct session *session;
struct blob *blob;
enum blobsync sync;
unsigned char key[KDF_HASH_LEN];
const char *sharename;
struct share *share;
bool read_only;
bool set_read_only;
bool admin;
bool set_admin;
bool hide_passwords;
bool set_hide_passwords;
};
struct share_command {
const char *name;
const char *usage;
int (*cmd)(struct share_command *cmd, int, char **,
struct share_args *share);
};
#define share_userls_usage "userls SHARE"
#define share_useradd_usage "useradd [--read-only=[true|false] --hidden=[true|false] --admin=[true|false] SHARE USERNAME"
#define share_usermod_usage "usermod [--read-only=[true|false] --hidden=[true|false] --admin=[true|false] SHARE USERNAME"
#define share_userdel_usage "userdel SHARE USERNAME"
#define share_create_usage "create SHARE"
#define share_rm_usage "rm SHARE"
static char *checkmark(int x) {
return (x) ? "x" : "_";
}
static void die_share_usage(struct share_command *cmd)
{
die_usage(cmd->usage);
}
static int share_userls(struct share_command *cmd, int argc, char **argv,
struct share_args *args)
{
UNUSED(argv);
struct share_user *user;
char name[40];
LIST_HEAD(users);
bool has_groups = false;
if (argc)
die_share_usage(cmd);
if (lastpass_share_getinfo(args->session, args->share->id, &users))
die("Unable to access user list for share %s\n",
args->sharename);
terminal_printf(TERMINAL_FG_YELLOW TERMINAL_BOLD "%-40s %6s %6s %6s %6s %6s" TERMINAL_RESET "\n",
"User", "RO", "Admin", "Hide", "OutEnt", "Accept");
list_for_each_entry(user, &users, list) {
if (user->is_group) {
has_groups = true;
continue;
}
if (user->realname) {
snprintf(name, sizeof(name), "%s <%s>",
user->realname, user->username);
} else {
snprintf(name, sizeof(name), "%s", user->username);
}
terminal_printf("%-40s %6s %6s %6s %6s %6s"
"\n",
name,
checkmark(user->read_only),
checkmark(user->admin),
checkmark(user->hide_passwords),
checkmark(user->outside_enterprise),
checkmark(user->accepted));
}
if (!has_groups)
return 0;
terminal_printf(TERMINAL_FG_YELLOW TERMINAL_BOLD "%-40s %6s %6s %6s %6s %6s"
TERMINAL_RESET "\n",
"Group", "RO", "Admin", "Hide", "OutEnt", "Accept");
list_for_each_entry(user, &users, list) {
if (!user->is_group)
continue;
terminal_printf("%-40s %6s %6s %6s %6s %6s"
"\n",
user->username,
checkmark(user->read_only),
checkmark(user->admin),
checkmark(user->hide_passwords),
checkmark(user->outside_enterprise),
checkmark(user->accepted));
}
return 0;
}
static int share_useradd(struct share_command *cmd, int argc, char **argv,
struct share_args *args)
{
struct share_user new_user = {
.read_only = args->read_only,
.hide_passwords = args->hide_passwords,
.admin = args->admin
};
if (argc != 1)
die_share_usage(cmd);
new_user.username = argv[0];
lastpass_share_user_add(args->session, args->share, &new_user);
return 0;
}
static
struct share_user *get_user_from_share(struct session *session,
struct share *share,
const char *username)
{
struct share_user *tmp, *found = NULL;
LIST_HEAD(users);
if (lastpass_share_getinfo(session, share->id, &users))
die("Unable to access user list for share %s\n", share->name);
list_for_each_entry(tmp, &users, list) {
if (strcmp(tmp->username, username) == 0) {
found = tmp;
break;
}
}
if (!found)
die("Unable to find user %s in the user list\n",
username);
return found;
}
static int share_usermod(struct share_command *cmd, int argc, char **argv,
struct share_args *args)
{
struct share_user *user;
if (argc != 1)
die_share_usage(cmd);
user = get_user_from_share(args->session, args->share, argv[0]);
if (args->set_read_only)
user->read_only = args->read_only;
if (args->set_hide_passwords)
user->hide_passwords = args->hide_passwords;
if (args->set_admin)
user->admin = args->admin;
lastpass_share_user_mod(args->session, args->share, user);
return 0;
}
static int share_userdel(struct share_command *cmd, int argc, char **argv,
struct share_args *args)
{
struct share_user *found;
if (argc != 1)
die_share_usage(cmd);
found = get_user_from_share(args->session, args->share, argv[0]);
lastpass_share_user_del(args->session, args->share->id, found);
return 0;
}
static int share_create(struct share_command *cmd, int argc, char **argv,
struct share_args *args)
{
if (argc != 0)
die_share_usage(cmd);
UNUSED(argv);
lastpass_share_create(args->session, args->sharename);
return 0;
}
static int share_rm(struct share_command *cmd, int argc, char **argv,
struct share_args *args)
{
if (argc != 0)
die_share_usage(cmd);
UNUSED(argv);
lastpass_share_delete(args->session, args->share);
return 0;
}
#define SHARE_CMD(name) { #name, "share " share_##name##_usage, share_##name }
static struct share_command share_commands[] = {
SHARE_CMD(userls),
SHARE_CMD(useradd),
SHARE_CMD(usermod),
SHARE_CMD(userdel),
SHARE_CMD(create),
SHARE_CMD(rm),
};
#undef SHARE_CMD
/* Display more verbose usage if no subcmd is given or matched. */
static void share_help(void)
{
terminal_fprintf(stderr, "Usage: %s %s\n", ARGV[0], cmd_share_usage);
for (size_t i = 0; i < ARRAY_SIZE(share_commands); ++i)
printf(" %s %s\n", ARGV[0], share_commands[i].usage);
exit(1);
}
int cmd_share(int argc, char **argv)
{
char *subcmd;
static struct option long_options[] = {
{"sync", required_argument, NULL, 'S'},
{"color", required_argument, NULL, 'C'},
{"read-only", required_argument, NULL, 'r'},
{"hidden", required_argument, NULL, 'H'},
{"admin", required_argument, NULL, 'a'},
{0, 0, 0, 0}
};
struct share_args args = {
.sync = BLOB_SYNC_AUTO,
.read_only = true,
.hide_passwords = true,
};
bool invalid_params = false;
struct share_command *command;
/*
* Parse out all option commands for all subcommands, and store
* them in the share_args struct.
*
* All commands have at least subcmd and sharename non-option args.
* Additional non-option commands are passed as argc/argv to the
* sub-command.
*/
int option;
int option_index;
while ((option = getopt_long(argc, argv, "S:C:r:H:a:", long_options, &option_index)) != -1) {
switch (option) {
case 'S':
args.sync = parse_sync_string(optarg);
break;
case 'C':
terminal_set_color_mode(
parse_color_mode_string(optarg));
break;
case 'r':
args.read_only = parse_bool_arg_string(optarg);
args.set_read_only = true;
break;
case 'H':
args.hide_passwords =
parse_bool_arg_string(optarg);
args.set_hide_passwords = true;
break;
case 'a':
args.admin = parse_bool_arg_string(optarg);
args.set_admin = true;
break;
case '?':
default:
invalid_params = true;
}
}
if (argc - optind < 1)
share_help();
subcmd = argv[optind++];
command = NULL;
for (unsigned int i=0; i < ARRAY_SIZE(share_commands); i++) {
if (strcmp(subcmd, share_commands[i].name) == 0) {
command = &share_commands[i];
break;
}
}
if (!command)
share_help();
if (argc - optind < 1 || invalid_params)
die_share_usage(command);
args.sharename = argv[optind++];
init_all(args.sync, args.key, &args.session, &args.blob);
if (strcmp(subcmd, "create") != 0) {
args.share = find_unique_share(args.blob, args.sharename);
if (!args.share)
die("Share %s not found.", args.sharename);
}
command->cmd(command, argc - optind, &argv[optind], &args);
session_free(args.session);
blob_free(args.blob);
return 0;
}