forked from lastpass/lastpass-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload-queue.c
284 lines (262 loc) · 6.31 KB
/
upload-queue.c
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
/*
* Copyright (c) 2014 LastPass.
*
*
*/
#include "upload-queue.h"
#include "session.h"
#include "http.h"
#include "util.h"
#include "config.h"
#include "kdf.h"
#include "process.h"
#include "password.h"
#include "endpoints.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <fcntl.h>
#include <signal.h>
static void upload_queue_write_entry(const char *entry, unsigned const char key[KDF_HASH_LEN])
{
_cleanup_free_ char *base_path = NULL;
_cleanup_free_ char *name = NULL;
struct stat sbuf;
int ret;
unsigned long serial;
base_path = config_path("upload-queue");
ret = stat(base_path, &sbuf);
if ((ret == -1 && errno == ENOENT) || !S_ISDIR(sbuf.st_mode)) {
unlink(base_path);
if (mkdir(base_path, 0700) < 0)
die_errno("mkdir(%s)", base_path);
} else if (ret == -1)
die_errno("stat(%s)", base_path);
for (serial = 0; serial < ULONG_MAX; ++serial) {
free(name);
xasprintf(&name, "upload-queue/%lu%lu", time(NULL), serial);
if (!config_exists(name))
break;
}
if (serial == ULONG_MAX)
die("No more upload queue entry slots available.");
config_write_encrypted_string(name, entry, key);
}
static char *upload_queue_next_entry(unsigned const char key[KDF_HASH_LEN], char **name, char **lock)
{
unsigned long long smallest = ULLONG_MAX, current;
_cleanup_free_ char *smallest_name = NULL;
_cleanup_free_ char *base_path = config_path("upload-queue");
_cleanup_free_ char *pidstr = NULL;
pid_t pid;
char *result, *p;
DIR *dir = opendir(base_path);
struct dirent *entry;
if (!dir)
return NULL;
while ((entry = readdir(dir))) {
if (entry->d_type != DT_REG)
continue;
for (p = entry->d_name; *p; ++p) {
if (!isdigit(*p))
break;
}
if (*p)
continue;
current = strtoull(entry->d_name, NULL, 10);
if (!current)
continue;
if (current < smallest) {
smallest = current;
free(smallest_name);
smallest_name = xstrdup(entry->d_name);
}
}
closedir(dir);
if (smallest == ULLONG_MAX)
return NULL;
xasprintf(name, "upload-queue/%s", smallest_name);
xasprintf(lock, "%s.lock", *name);
while (config_exists(*lock)) {
free(pidstr);
pidstr = config_read_encrypted_string(*lock, key);
if (!pidstr) {
config_unlink(*lock);
break;
}
pid = strtoul(pidstr, NULL, 10);
if (!pid) {
config_unlink(*lock);
break;
}
if (process_is_same_executable(pid))
sleep(1);
else {
config_unlink(*lock);
break;
}
}
free(pidstr);
pidstr = xultostr(getpid());
config_write_encrypted_string(*lock, pidstr, key);
result = config_read_encrypted_string(*name, key);
if (!result) {
config_unlink(*lock);
return NULL;
}
return result;
}
static void upload_queue_cleanup(int signal)
{
UNUSED(signal);
config_unlink("uploader.pid");
_exit(EXIT_SUCCESS);
}
static void upload_queue_upload_all(const struct session *session, unsigned const char key[KDF_HASH_LEN])
{
char *entry, *next_entry, *result;
int size;
char **argv = NULL;
char **argv_ptr;
char *name, *lock, *p;
bool do_break;
bool should_fetch_new_blob_after = false;
while ((entry = upload_queue_next_entry(key, &name, &lock))) {
size = 0;
for (p = entry; *p; ++p) {
if (*p == '\n')
++size;
}
if (p > entry && p[-1] != '\n')
++size;
if (size < 1) {
config_unlink(name);
config_unlink(lock);
goto end;
}
argv_ptr = argv = xcalloc(size + 1, sizeof(char **));
for (do_break = false, p = entry, next_entry = entry; ; ++p) {
if (!*p)
do_break = true;
if (*p == '\n' || !*p) {
*p = '\0';
*(argv_ptr++) = pinentry_unescape(next_entry);
next_entry = p + 1;
if (do_break)
break;
}
}
argv[size] = NULL;
for (int i = 0; i < 5; ++i) {
sleep(i * 2);
result = http_post_lastpass_v(argv[0], session->sessionid, NULL, &argv[1]);
if (result && strlen(result))
should_fetch_new_blob_after = true;
free(result);
if (result)
break;
}
if (!result) {
sleep(30);
config_unlink(lock);
} else {
config_unlink(name);
config_unlink(lock);
}
for (argv_ptr = argv; *argv_ptr; ++argv_ptr)
free(*argv_ptr);
free(argv);
end:
free(name);
free(lock);
free(entry);
}
if (should_fetch_new_blob_after)
blob_free(lastpass_get_blob(session, key));
}
static void upload_queue_run(const struct session *session, unsigned const char key[KDF_HASH_LEN])
{
_cleanup_free_ char *pid = NULL;
upload_queue_kill();
pid_t child = fork();
if (child < 0)
die_errno("fork(agent)");
if (child == 0) {
int null = open("/dev/null", 0);
if (null >= 0) {
dup2(null, 0);
dup2(null, 1);
dup2(null, 2);
close(null);
}
setsid();
IGNORE_RESULT(chdir("/"));
process_set_name("lpass [upload queue]");
signal(SIGHUP, upload_queue_cleanup);
signal(SIGINT, upload_queue_cleanup);
signal(SIGQUIT, upload_queue_cleanup);
signal(SIGTERM, upload_queue_cleanup);
signal(SIGALRM, upload_queue_cleanup);
upload_queue_upload_all(session, key);
upload_queue_cleanup(0);
_exit(EXIT_SUCCESS);
}
pid = xultostr(child);
config_write_string("uploader.pid", pid);
}
void upload_queue_kill(void)
{
_cleanup_free_ char *pidstr = NULL;
pid_t pid;
pidstr = config_read_string("uploader.pid");
if (!pidstr)
return;
pid = strtoul(pidstr, NULL, 10);
if (!pid)
return;
kill(pid, SIGTERM);
}
bool upload_queue_is_running(void)
{
_cleanup_free_ char *pidstr = NULL;
pid_t pid;
pidstr = config_read_string("uploader.pid");
if (!pidstr)
return false;
pid = strtoul(pidstr, NULL, 10);
if (!pid)
return false;
return process_is_same_executable(pid);
}
void upload_queue_enqueue(enum blobsync sync, unsigned const char key[KDF_HASH_LEN], const struct session *session, const char *page, ...)
{
_cleanup_free_ char *sum = xstrdup(page);
char *next = NULL;
char *escaped = NULL;
va_list params;
char *param;
va_start(params, page);
while ((param = va_arg(params, char *))) {
escaped = pinentry_escape(param);
xasprintf(&next, "%s\n%s", sum, escaped);
free(escaped);
free(sum);
sum = next;
}
va_end(params);
upload_queue_write_entry(sum, key);
if (sync != BLOB_SYNC_NO)
upload_queue_ensure_running(key, session);
}
void upload_queue_ensure_running(unsigned const char key[KDF_HASH_LEN], const struct session *session)
{
if (!upload_queue_is_running())
upload_queue_run(session, key);
}