2 * Sub-authentication Client
4 * (c) 2017 Martin Mares <mj@ucw.cz>
9 #include <ucw-json/json.h>
13 #include <sys/socket.h>
19 static char *socket_path = INSTALL_RUN_DIR "/subauthd.socket";
21 static const struct opt_section options = {
23 OPT_HELP("A client to the sub-authentication daemon."),
24 OPT_HELP("Usage: subauth [options]"),
32 int main(int argc UNUSED, char **argv)
34 opt_parse(&options, argv+1);
36 int sk = socket(PF_UNIX, SOCK_SEQPACKET, 0);
38 die("socket(PF_UNIX, SOCK_SEQPACKET): %m");
40 struct sockaddr_un sun;
41 sun.sun_family = AF_UNIX;
42 if (strlen(socket_path) >= sizeof(sun.sun_path))
43 die("Socket path too long");
44 strcpy(sun.sun_path, socket_path);
46 if (connect(sk, (struct sockaddr *) &sun, sizeof(sun)) < 0)
47 die("Cannot connect to %s: %m", socket_path);
49 struct fastbuf *out = bfdopen(1, 4096);
51 struct json_context *js = json_new();
52 struct json_node *rq = json_new_object(js);
54 bprintf(out, ">>> Request:\n");
55 json_write(js, out, rq);
58 struct fastbuf *rq_fb = fbgrow_create(4096);
59 json_write(js, rq_fb, rq);
61 uint rq_len = fbgrow_get_buf(rq_fb, &rq_buf);
62 if (send(sk, rq_buf, rq_len, 0) < 0)
63 die("Cannot send request: %m");
66 byte rp_buf[4096]; // FIXME
67 int rp_len = recv(sk, rp_buf, sizeof(rp_buf), 0);
69 die("Cannot receive reply: %m");
71 puts("Parsing reply");
73 fbbuf_init_read(&rp_fb, rp_buf, rp_len, 0);
74 struct json_node *rp = json_parse(js, &rp_fb);
75 bprintf(out, "<<< Reply:\n");
76 json_write(js, out, rp);