aboutsummaryrefslogtreecommitdiff
path: root/dummy_client.c
blob: 2033471adf242c1668770dcd733cd4321e347c8d (plain)
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/* Based on:
 * <https://github.com/troydhanson/network/blob/master/unixdomain/01.basic/cli.c> */

/* A dummy client for use with the dummy service provider, which
 * provides no assurances. */

/* Usage:
 *
 * $ ./client [-s <SOCKET>] -u USERID COMMAND [PARAMS]
 *
 * Where COMMAND and PARAMS are one of the following:
 *   create (takes no parameters)
 *
 *   modifyfile -f FILEIDX -i IMAGE_FILE
 *
 *   retrievefile -f FILEIDX [-v VERSION] -o IMAGE_OUT
 */

#define CLIENT
#include "crypto.h"
#include "service_provider.h"
#include "test.h"

#include <sys/socket.h>
#include <sys/un.h>

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>

static const char *socket_path = "socket";
static const char *parse_args_fail = NULL;
static uint64_t user_id = 0;
static struct user_request cl_request;
const char *image_path = NULL;
static bool labels = false, labels_only = false;

void print_usage(const char *name)
{
    printf("Usage:\n"
           "\n"
           "$ ./client [-s <SOCKET>] -u USERID -k USER_KEY COMMAND [PARAMS]\n"
           "\n"
           "Where COMMAND and PARAMS are one of the following:\n"
           "  create (takes no parameters)\n"
           "\n"
           "  modifyfile -f FILEIDX -i IMAGE_FILE [-ib buildcode_file]\n"
           "             [-ic compose_file] [--encrypt, -e]\n"
           "\n"
           "  retrieveinfo -f FILEIDX [-v VERSION]\n"
           "\n"
           "  retrievefile -f FILEIDX [-v VERSION] -o IMAGE_OUT\n");
}

bool parse_args(int argc, char *argv[])
{
    for(int i = 1; i < argc; ++i)
    {
        char *arg = argv[i];
        if(!strcmp(arg, "-s") || !strcmp(arg, "--socket"))
        {
            if(++i < argc)
                socket_path = argv[i];
            else
            {
                parse_args_fail = "Expected parameter after -s";
                return false;
            }
        }
        else if(!strcmp(arg, "-u"))
        {
            if(++i < argc)
                user_id = atol(argv[i]);
            else
            {
                parse_args_fail = "Expected user id";
                return false;
            }
        }
        else if(!strcmp(arg, "-f"))
        {
            if(++i < argc)
                cl_request.file_idx = atol(argv[i]);
            else
            {
                parse_args_fail = "Expected file index";
                return false;
            }
        }
        else if(!strcmp(arg, "-v"))
        {
            if(++i < argc)
                cl_request.retrieve.version = atol(argv[i]);
            else
            {
                parse_args_fail = "Expected version number";
                return false;
            }
        }
        /* -i and -o are handled identically */
        else if(!strcmp(arg, "-i") || !strcmp(arg, "-o"))
        {
            if(++i < argc)
                image_path = argv[i];
            else
            {
                parse_args_fail = "Expected image path";
                return false;
            }
        }
        else if(!strcmp(arg, "-h") || !strcmp(arg, "--help"))
        {
            print_usage(argv[0]);
            exit(1);
        }
        else if(!strcmp(arg, "-p") || !strcmp(arg, "--profile"))
        {
            cl_request.profile = true;
        }
        else if(!strcmp(arg, "-l") || !strcmp(arg, "--labels"))
        {
            labels = true;
        }
        else if(!strcmp(arg, "--labels-only"))
        {
            labels_only = true;
        }
        else if(!strcmp(arg, "create"))
        {
            if(cl_request.type != USERREQ_NONE)
            {
                parse_args_fail = "Multiple commands";
                return false;
            }
            cl_request.type = CREATE_FILE;
        }
        else if(!strcmp(arg, "modifyfile"))
        {
            if(cl_request.type != USERREQ_NONE)
            {
                parse_args_fail = "Multiple commands";
                return false;
            }
            cl_request.type = MODIFY_FILE;
        }
        else if(!strcmp(arg, "retrieveinfo") || !strcmp(arg, "retrievefile"))
        {
            if(cl_request.type != USERREQ_NONE)
            {
                parse_args_fail = "Multiple commands";
                return false;
            }
            cl_request.type = RETRIEVE_INFO;

            if(!strcmp(arg, "retrievefile"))
            {
                cl_request.type = RETRIEVE_FILE;
            }
        }
        else
        {
            /* ignore unknowns */
            //parse_args_fail = "Unknown parameter";
            //return false;
        }
    }
    if(cl_request.type != USERREQ_NONE && user_id != 0)
    {
        if(cl_request.type > CREATE_FILE)
        {
            if(!cl_request.file_idx)
            {
                parse_args_fail = "No file index specified";
                return false;
            }
        }
        else
        {
            if(cl_request.file_idx)
            {
                parse_args_fail = "Index specified for create";
                return false;
            }
        }

        if(cl_request.type == MODIFY_FILE ||
           cl_request.type == RETRIEVE_FILE)
        {
            if(!image_path)
            {
                parse_args_fail = "No image file specified";
                return false;
            }
        }

        return true;
    }
    else
    {
        parse_args_fail = "Missing required parameter (either command, user ID, or user key)";
        return false;
    }
}

/* In case of modifcation or file creation, returns true on successful
 * completion of request, as acknowledged by module. In case of info
 * retrieval, returns true if version info is verified by module. The
 * verinfo_out, user_key, and keylen parameters must not be NULL in
 * this case (in all other cases they are ignored). */
bool exec_request(int fd, const struct user_request *req,
                  const void *new_file_contents, size_t len, /* MODIFY_FILE only */
                  struct version_info *verinfo_out,          /* RETRIEVE_INFO only */
                  void **file_contents_out,                  /* RETRIEVE_FILE only */
                  size_t *file_len,                          /* RETRIEVE_FILE only */
                  uint64_t *new_idx,                         /* CREATE_FILE only */
		  struct server_profile *profile_out)        /* profile=true only */
{
    write(fd, req, sizeof(*req));
    /* write additional data */
    switch(req->type)
    {
    case MODIFY_FILE:
        /* prefix file with size */
        write(fd, &len, sizeof(len));
        write(fd, new_file_contents, len);
        break;
    case CREATE_FILE:
    case RETRIEVE_INFO:
    case RETRIEVE_FILE:
        /* no additional data needed, fall through */
    default:
        break;
    }

    /* server now processes request */
    
    switch(req->type)
    {
    case CREATE_FILE:
    {
        if(new_idx)
            recv(fd, new_idx, sizeof(*new_idx), MSG_WAITALL);
	
        if(req->profile)
            prof_read(fd, profile_out);
	
        return true;
    }
    case MODIFY_ACL:
    case MODIFY_FILE:
    {
        /* don't verify */
        if(req->profile)
            prof_read(fd, profile_out);

        return true;
    }
    case RETRIEVE_INFO:
    {
        struct version_info verinfo;
        recv(fd, &verinfo, sizeof(verinfo), MSG_WAITALL);
        *verinfo_out = verinfo;
        if(req->profile)
            prof_read(fd, profile_out);

        return true;
    }
    case RETRIEVE_FILE:
    {
        *file_contents_out = deserialize_file(fd, file_len);

        if(req->profile)
            prof_read(fd, profile_out);

        return true;
    }
    default:
        assert(false);
    }
}

/* set version = 0 to get latest version */
struct version_info request_verinfo(int fd, uint64_t user_id,
                                    uint64_t file_idx, uint64_t version)

{
    struct user_request req;
    req.type = RETRIEVE_INFO;
    req.user_id = user_id;
    req.retrieve.file_idx = file_idx;
    req.retrieve.version = version;

    struct version_info verinfo;

    exec_request(fd, &req,
                 NULL, 0,
                 &verinfo,
                 NULL,
                 NULL,
                 NULL,
		 NULL);

    return verinfo;
}

int connect_to_service(const char *sockpath)
{
    struct sockaddr_un addr;
    int fd;

    if ( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
        perror("socket error");
        exit(-1);
    }

    memset(&addr, 0, sizeof(addr));
    addr.sun_family = AF_UNIX;
    if (*sockpath == '\0') {
        *addr.sun_path = '\0';
        strncpy(addr.sun_path+1, sockpath+1, sizeof(addr.sun_path)-2);
    } else {
        strncpy(addr.sun_path, sockpath, sizeof(addr.sun_path)-1);
    }

    if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
        perror("connect error");
        exit(-1);
    }

    return fd;
}

bool server_request(const char *sockpath,
                    uint64_t user_id,
                    struct user_request req,
                    const char *image_path)
{
    void *file_contents = NULL;
    size_t file_len = 0;

    /* Fill in rest of request structure */
    req.user_id = user_id;

    if(req.type == MODIFY_FILE)
    {
        if(image_path)
        {
            file_contents = load_file(image_path, &file_len);
        }
    }

    struct version_info verinfo;
    struct server_profile profile;

    int fd = connect_to_service(sockpath);
    uint64_t new_idx;

    bool success = exec_request(fd, &req,
                                req.type == MODIFY_FILE ? file_contents : NULL,
                                req.type == MODIFY_FILE ? file_len : 0,
                                req.type == RETRIEVE_INFO ? &verinfo : NULL,
                                req.type == RETRIEVE_FILE ? &file_contents : NULL,
                                req.type == RETRIEVE_FILE ? &file_len : NULL,
                                req.type == CREATE_FILE ? &new_idx : NULL,
				req.profile ? &profile : NULL);

    printf("Request %s\n",
           success ?
           "\033[32;1msucceeded\033[0m" :
           "\033[31;1mfailed\033[0m");

    if(!success)
        return false;

    switch(req.type)
    {
    case CREATE_FILE:
        printf("Created file with index %lu.\n", new_idx);
        break;
    case RETRIEVE_INFO:
        printf("File info: ");
        dump_versioninfo(&verinfo);
        break;
    case RETRIEVE_FILE:
    {
        printf("Writing image file to %s.\n", image_path);
        write_file(image_path, file_contents, file_len);
        /* What about build code? We only have the IOMT, not the actual contents. */
        break;
    }
    default:
        break;
    }
    
    if(req.profile)
    {
        /* dump to stderr */
        prof_dump(&profile, labels, labels_only);
    }

    return true;
}

int main(int argc, char *argv[]) {
    if(!parse_args(argc, argv))
    {
        printf("%s\n", parse_args_fail);
        print_usage(argv[0]);
        return 1;
    }

    signal(SIGPIPE, SIG_IGN);

    server_request(socket_path, user_id,
                   cl_request,
                   image_path);

    return 0;
}