~kennylevinsen/gtkgreet

ref: 3471aaaaee4619fd4aded85803b27b4a8dff41a6 gtkgreet/gtkgreet/proto.c -rw-r--r-- 7.6 KiB
3471aaaaRyan Farley proto: use uint32_t offsets and message lengths 1 year, 6 months ago
                                                                                
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
#define _POSIX_C_SOURCE 200809L

#include <json-c/json_object.h>
#include <json-c/json_tokener.h>

#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

#include "proto.h"
#include "window.h"

struct header {
    uint32_t magic;
    uint32_t version;
    uint32_t payload_len;
};

static int write_req(int fd, struct json_object* req) {
    const char* reqstr = json_object_get_string(req);
    size_t len = strlen(reqstr);
    if (len > 0xFFFFFFFF) {
	    goto error;
    }
    uint32_t header = len;
    char* headerp = (char*)&header;
    uint32_t off = 0;

    while (off < 4) {
        ssize_t n = write(fd, &headerp[off], 4-off);
        if (n < 1) {
            goto error;
        }
        off += n;
    }

    off = 0;
    while (off < len) {
        ssize_t n = write(fd, &reqstr[off], len-off);
        if (n < 1) {
            goto error;
        }
        off += n;
    }
 
    return 0;
error:
    return -1;
}

static struct json_object* read_resp(int fd) {
    struct json_object* resp = NULL;
    char *respstr = NULL;
    uint32_t len;
    uint32_t off = 0;

    while (off < 4) {
        char* headerp = (char*)&len;
        ssize_t n = read(fd, &headerp[off], 4-off);
        if (n < 1) {
            goto end;
        }
        off += n;
    }

    off = 0;
    respstr = (char*)calloc(1,len+1);
    while (off <len) {
        int n = read(fd, &respstr[off],len-off);
        if (n < 1) {
            goto end;
        }
        off += n;
    }

    resp = json_tokener_parse(respstr);

end:
    if (respstr != NULL) {
        free(respstr);
    }
    return resp;
}

static int connectto(const char* path) {
    int fd = socket(AF_UNIX, SOCK_STREAM, 0);
    if (fd < 0) {
        fprintf(stderr, "unable to open socket\n");
        return -1;
    }

    struct sockaddr_un addr;
    memset(&addr, 0, sizeof(addr));
    addr.sun_family = AF_UNIX;
    strcpy(addr.sun_path, path);
    if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
        fprintf(stderr, "connect: error: %d\n", errno);
        close(fd);
        return -1;
    }

    return fd;
}

static struct json_object* roundtrip_json(struct json_object* req) {
    struct json_object* resp = NULL;
    int fd;
    char* greetd_sock = getenv("GREETD_SOCK");

    if (greetd_sock == NULL) {
        fprintf(stderr, "GREETD_SOCK not set\n");
        return NULL;
    }

    if ((fd = connectto(greetd_sock)) < 0) {
        fprintf(stderr, "unable to connect to socket\n");
        goto end;
    }

    if (write_req(fd, req) != 0) {
        fprintf(stderr, "unable to write request to socket\n");
        goto end;
    }

    if ((resp = read_resp(fd)) == NULL) {
        fprintf(stderr, "unable to read response from socket\n");
        goto end;
    }

end:
    if (fd > 0) {
        close(fd);
    }
    return resp;
}

const char* json_get_string_from_object(struct json_object* obj, const char* key) {

    struct json_object* val = json_object_object_get(obj, key);
    if (val == NULL) {
        return NULL;
    }
    return json_object_get_string(val);
}

struct response roundtrip(struct request req) {
    struct json_object *json_req = json_object_new_object();
    switch (req.request_type) {
    case request_type_create_session: {
        json_object_object_add(json_req, "type", json_object_new_string("create_session"));
        json_object_object_add(json_req, "username", json_object_new_string(req.body.request_create_session.username));
        break;
    }
    case request_type_start_session: {
        json_object_object_add(json_req, "type", json_object_new_string("start_session"));
        struct json_object* cmd = json_object_new_array();
        json_object_array_add(cmd, json_object_new_string(req.body.request_start_session.cmd));
        json_object_object_add(json_req, "cmd", cmd);
        break;
    }
    case request_type_post_auth_message_response: {
        json_object_object_add(json_req, "type", json_object_new_string("post_auth_message_response"));
        json_object_object_add(json_req, "response", json_object_new_string(req.body.request_post_auth_message_response.response));
        break;
    }
    case request_type_cancel_session: {
        json_object_object_add(json_req, "type", json_object_new_string("cancel_session"));
        break;
    }
    }

    struct response resp = {
        .response_type = response_type_roundtrip_error,
    };
    resp.body.response_error.error_type = error_type_error;

    struct json_object* json_resp = roundtrip_json(json_req);
    if (json_resp == NULL) {
        snprintf(resp.body.response_error.description, 128, "proto: roundtrip failed");
        goto done;
    }

    const char* typestr = json_get_string_from_object(json_resp, "type");
    if (typestr == NULL) {
        snprintf(resp.body.response_error.description, 128, "proto: no type found");
        goto done;
    }


    if (strcmp(typestr, "success") == 0) {
        resp.response_type = response_type_success;
        goto done;
    }

    if (strcmp(typestr, "auth_message") == 0) {
        const char* messagestr = json_get_string_from_object(json_resp, "auth_message");
        if (messagestr == NULL) {
            resp.response_type = response_type_error;
            snprintf(resp.body.response_error.description, 128, "proto: no message");
            goto done;
        }
        const char* auth_message_typestr = json_get_string_from_object(json_resp, "auth_message_type");
        if (auth_message_typestr == NULL) {
            resp.response_type = response_type_error;
            snprintf(resp.body.response_error.description, 128, "proto: no message type");
            goto done;
        }
        resp.response_type = response_type_auth_message;
        if (strcmp(auth_message_typestr, "visible") == 0) {
            resp.body.response_auth_message.auth_message_type = auth_message_type_visible;
        } else if (strcmp(auth_message_typestr, "secret") == 0) {
            resp.body.response_auth_message.auth_message_type = auth_message_type_secret;
        } else if (strcmp(auth_message_typestr, "info") == 0) {
            resp.body.response_auth_message.auth_message_type = auth_message_type_info;
        } else if (strcmp(auth_message_typestr, "error") == 0) {
            resp.body.response_auth_message.auth_message_type = auth_message_type_error;
        } else {
            resp.response_type = response_type_error;
            snprintf(resp.body.response_error.description, 128, "proto: unknown message type");
            goto done;
        }

        strncpy(resp.body.response_auth_message.auth_message, messagestr, sizeof(resp.body.response_auth_message.auth_message)-1);
        goto done;
    }

    if (strcmp(typestr, "error") == 0) {
        const char* descriptionstr = json_get_string_from_object(json_resp, "description");
        if (descriptionstr == NULL) {
            resp.response_type = response_type_error;
            snprintf(resp.body.response_error.description, 128, "proto: no error description");
            goto done;
        }
        resp.response_type = response_type_error;

        const char* errortypestr = json_get_string_from_object(json_resp, "error_type");
        if (strcmp(errortypestr, "auth_error") == 0) {
            resp.body.response_error.error_type = error_type_auth;
        } else {
            resp.body.response_error.error_type = error_type_error;
        }

        strncpy(resp.body.response_error.description, descriptionstr, sizeof(resp.body.response_error.description)-1);
        goto done;
    }

    snprintf(resp.body.response_error.description, 128, "proto: no known type");

done:
    json_object_put(json_resp);
    return resp;
}