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
|
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include "util.h"
#include "version.h"
#include "yyjson.h"
static ssize_t write_all(int fd, const char *buf, size_t len)
{
size_t off = 0;
while (off < len) {
ssize_t w = write(fd, buf + off, len - off);
if (w < 0) {
if (errno == EINTR)
continue;
return -1;
}
off += (size_t)w;
}
return (ssize_t)off;
}
static char *read_line_fd(int fd)
{
struct buf b;
buf_init(&b);
char chunk[4096];
for (;;) {
ssize_t r = read(fd, chunk, sizeof chunk);
if (r < 0) {
if (errno == EINTR)
continue;
buf_free(&b);
return NULL;
}
if (r == 0)
break;
unsigned char *nl = memchr(chunk, '\n', (size_t)r);
if (nl) {
buf_append(&b, chunk, (size_t)(nl - (unsigned char *)chunk));
break;
}
buf_append(&b, chunk, (size_t)r);
}
char *out = xmalloc(b.len + 1);
memcpy(out, b.p ? (char *)b.p : "", b.len);
out[b.len] = '\0';
buf_free(&b);
return out;
}
static int tcp_connect_addr(const char *addrport)
{
char host[256] = "127.0.0.1";
char port[16] = "8787";
const char *colon = strrchr(addrport, ':');
if (colon) {
size_t hl = (size_t)(colon - addrport);
if (hl < sizeof host) {
memcpy(host, addrport, hl);
host[hl] = '\0';
}
snprintf(port, sizeof port, "%s", colon + 1);
} else {
snprintf(port, sizeof port, "%s", addrport);
}
struct addrinfo hints, *res = NULL;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(host, port, &hints, &res) != 0)
return -1;
int fd = -1;
for (struct addrinfo *ai = res; ai; ai = ai->ai_next) {
fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (fd < 0)
continue;
if (connect(fd, ai->ai_addr, ai->ai_addrlen) == 0)
break;
close(fd);
fd = -1;
}
freeaddrinfo(res);
return fd;
}
static int connect_target(const char *target)
{
if (strncmp(target, "tcp:", 4) == 0)
return tcp_connect_addr(target + 4);
struct sockaddr_un sa;
memset(&sa, 0, sizeof sa);
sa.sun_family = AF_UNIX;
if (strlen(target) >= sizeof sa.sun_path) {
errno = ENAMETOOLONG;
return -1;
}
snprintf(sa.sun_path, sizeof sa.sun_path, "%s", target);
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0)
return -1;
if (connect(fd, (struct sockaddr *)&sa, sizeof sa) != 0) {
close(fd);
return -1;
}
return fd;
}
static char *make_request(const char *cmd, const char *session, int64_t org,
const char *args_json, const char *id)
{
yyjson_doc *adoc = NULL;
if (args_json) {
adoc = yyjson_read(args_json, strlen(args_json), 0);
if (!adoc || !yyjson_is_obj(yyjson_doc_get_root(adoc))) {
yyjson_doc_free(adoc);
return NULL;
}
}
yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);
yyjson_mut_val *o = yyjson_mut_obj(d);
yyjson_mut_doc_set_root(d, o);
yyjson_mut_obj_add_int(d, o, "v", 1);
yyjson_mut_obj_add_str(d, o, "id", id ? id : "cli");
yyjson_mut_obj_add_str(d, o, "cmd", cmd);
if (session)
yyjson_mut_obj_add_str(d, o, "session", session);
if (org > 0)
yyjson_mut_obj_add_int(d, o, "org", org);
if (adoc) {
yyjson_mut_val *args = yyjson_val_mut_copy(d, yyjson_doc_get_root(adoc));
yyjson_mut_obj_add_val(d, o, "args", args);
yyjson_doc_free(adoc);
}
char *s = yyjson_mut_write(d, 0, NULL);
yyjson_mut_doc_free(d);
return s;
}
static char *make_login_args(const char *user, const char *password)
{
yyjson_mut_doc *d = yyjson_mut_doc_new(NULL);
yyjson_mut_val *o = yyjson_mut_obj(d);
yyjson_mut_doc_set_root(d, o);
yyjson_mut_obj_add_str(d, o, "method", "password");
yyjson_mut_obj_add_str(d, o, "username", user);
yyjson_mut_obj_add_str(d, o, "password", password);
char *s = yyjson_mut_write(d, 0, NULL);
yyjson_mut_doc_free(d);
return s;
}
static void usage(void)
{
fprintf(stderr,
"usage: bokfctl [options] <cmd> [args-json]\n"
"\n"
"options:\n"
" --socket TARGET unix socket path or tcp:host:port\n"
" (env BOKFD_SOCKET, default /run/bokfd/bokfd.sock)\n"
" --user NAME login user (env BOKFD_USER)\n"
" --password PW login password (env BOKFD_PASSWORD)\n"
" --org ID active org for this request\n"
" --version\n"
"\n"
"examples:\n"
" bokfctl health\n"
" bokfctl org.create '{\"name\":\"AB Ett\"}'\n"
" bokfctl describe\n");
}
int main(int argc, char **argv)
{
const char *target = getenv("BOKFD_SOCKET");
if (!target)
target = "/run/bokfd/bokfd.sock";
const char *user = getenv("BOKFD_USER");
const char *password = getenv("BOKFD_PASSWORD");
int64_t org = 0;
const char *cmd = NULL;
const char *args_json = NULL;
for (int i = 1; i < argc; i++) {
const char *a = argv[i];
const char *val = NULL;
if (!strncmp(a, "--socket", 8) &&
(a[8] == '\0' || a[8] == '=')) {
val = a[8] == '=' ? a + 9 : (i + 1 < argc ? argv[++i] : NULL);
if (!val) {
fprintf(stderr, "bokfctl: --socket requires a value\n");
return 2;
}
target = val;
} else if (!strncmp(a, "--user", 6) &&
(a[6] == '\0' || a[6] == '=')) {
val = a[6] == '=' ? a + 7 : (i + 1 < argc ? argv[++i] : NULL);
if (!val) {
fprintf(stderr, "bokfctl: --user requires a value\n");
return 2;
}
user = val;
} else if (!strncmp(a, "--password", 10) &&
(a[10] == '\0' || a[10] == '=')) {
val = a[10] == '=' ? a + 11 : (i + 1 < argc ? argv[++i] : NULL);
if (!val) {
fprintf(stderr, "bokfctl: --password requires a value\n");
return 2;
}
password = val;
} else if (!strncmp(a, "--org", 5) &&
(a[5] == '\0' || a[5] == '=')) {
val = a[5] == '=' ? a + 6 : (i + 1 < argc ? argv[++i] : NULL);
if (!val) {
fprintf(stderr, "bokfctl: --org requires a value\n");
return 2;
}
org = strtoll(val, NULL, 10);
} else if (strcmp(a, "--version") == 0) {
printf("bokfctl %s\n", BOKF_VERSION);
return 0;
} else if (strcmp(a, "--help") == 0 || strcmp(a, "-h") == 0) {
usage();
return 0;
} else if (a[0] == '-' && a[1] != '\0') {
fprintf(stderr, "bokfctl: unknown option %s\n", a);
usage();
return 2;
} else if (!cmd) {
cmd = a;
} else if (!args_json) {
args_json = a;
} else {
fprintf(stderr, "bokfctl: unexpected argument %s\n", a);
return 2;
}
}
if (!cmd) {
usage();
return 2;
}
int fd = connect_target(target);
if (fd < 0) {
fprintf(stderr, "bokfctl: cannot connect to %s: %s\n", target,
strerror(errno));
return 2;
}
char session[128] = "";
if (strcmp(cmd, "health") != 0 && strcmp(cmd, "meta") != 0 &&
strcmp(cmd, "session.open") != 0) {
if (!user || !password) {
fprintf(stderr,
"bokfctl: set BOKFD_USER and BOKFD_PASSWORD (or --user/--password) to log in\n");
close(fd);
return 2;
}
char *largs = make_login_args(user, password);
char *lreq = make_request("session.open", NULL, 0, largs, "login");
free(largs);
if (!lreq || write_all(fd, lreq, strlen(lreq)) < 0 ||
write_all(fd, "\n", 1) < 0) {
fprintf(stderr, "bokfctl: send failed\n");
free(lreq);
close(fd);
return 2;
}
free(lreq);
char *lresp = read_line_fd(fd);
if (!lresp) {
fprintf(stderr, "bokfctl: no response\n");
close(fd);
return 2;
}
yyjson_doc *ld = yyjson_read(lresp, strlen(lresp), 0);
int ok = ld && yyjson_is_obj(yyjson_doc_get_root(ld)) &&
yyjson_get_bool(yyjson_obj_get(yyjson_doc_get_root(ld), "ok"));
const char *sid = NULL;
if (ok) {
yyjson_val *r = yyjson_obj_get(yyjson_doc_get_root(ld), "result");
yyjson_val *s = r ? yyjson_obj_get(r, "session") : NULL;
if (s && yyjson_is_str(s))
sid = yyjson_get_str(s);
}
if (!ok || !sid) {
fprintf(stderr, "%s\n", lresp);
yyjson_doc_free(ld);
free(lresp);
close(fd);
return 1;
}
snprintf(session, sizeof session, "%s", sid);
yyjson_doc_free(ld);
free(lresp);
}
char *req = NULL;
if (strcmp(cmd, "raw") == 0) {
if (!args_json) {
fprintf(stderr, "bokfctl: raw requires a full request JSON\n");
close(fd);
return 2;
}
req = xstrdup(args_json);
} else {
req = make_request(cmd, session[0] ? session : NULL, org, args_json,
"cli");
if (!req) {
fprintf(stderr, "bokfctl: args must be a JSON object\n");
close(fd);
return 2;
}
}
if (write_all(fd, req, strlen(req)) < 0 || write_all(fd, "\n", 1) < 0) {
fprintf(stderr, "bokfctl: send failed\n");
free(req);
close(fd);
return 2;
}
free(req);
char *resp = read_line_fd(fd);
close(fd);
if (!resp) {
fprintf(stderr, "bokfctl: no response\n");
return 2;
}
yyjson_doc *rd = yyjson_read(resp, strlen(resp), 0);
char *pretty = rd ? yyjson_write(rd, YYJSON_WRITE_PRETTY, NULL) : NULL;
printf("%s\n", pretty ? pretty : resp);
int ok = rd && yyjson_is_obj(yyjson_doc_get_root(rd)) &&
yyjson_get_bool(yyjson_obj_get(yyjson_doc_get_root(rd), "ok"));
if (rd)
yyjson_doc_free(rd);
free(pretty);
free(resp);
return ok ? 0 : 1;
}
|