Newer
Older
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
/**
* Callback function that appends an SSH key tot the list of keys authorised for
* access to the initrd.
*
* Example output:
* {"ssh-keys":{"1":"ssh-rsa AAAAB3... example@example.com",
* "2":"ssh-rsa AAAAB3...","5":"command=\"/usr/bin/cryptops-client\" ssh-rsa
* AAAAB3... cryptops-test@greenhost"}}
*
* The indices correspond to line numbers of the authorized_keys file.
* Missing indices (like 3 and 4 in the example) arise from empty lines in the
* file; those are creted when keys are deleted.
*
* @param[in] request incoming HTTP request
* @param[out] response HTTP response to the request
* @param[in] user_data extra data to pass between main thread and callbacks
* @return internal status code
*/
int callback_ssh_keys_post(const struct _u_request * request,
struct _u_response * response, void * user_data)
{
// Open file with append mode
FILE * authorized_keys = fopen(AUTHORIZED_KEYS_PATH, "a");
// Check if that succeeded.
if (authorized_keys == NULL)
{
printf("Could not open authorized_keys file for writing\n");
return send_simple_response(response, 500, "error",
"error reading authorized_keys");
}
// Read in json request body.
json_t * json_input = ulfius_get_json_body_request(request, NULL);
// Read SSH key from request.
const char * ssh_key;
ssh_key = json_string_value(json_object_get(json_input, "ssh-key"));
if (ssh_key == NULL)
{
return send_simple_response(response, 400, "error", "missing ssh-key");
}
// Call cat to append the command correctly:
char * command = NULL;
asprintf(&command, "echo %s | sed -rf %s", ssh_key, RESTRICT_COMMAND_PATH);
FILE *sed_output = popen(command, "r");
if (!sed_output)
{
return send_simple_response(response, 500, "error", "Internal error while handling ssh-key");
}
// Get the output from sed
ssh_key = read_from_file(sed_output);
if(!ssh_key)
{
return send_simple_response(response, 500, "error", "Internal error while converting ssh-key");
}
// Write SSH key to file
fprintf(authorized_keys, ssh_key);
fclose(authorized_keys);
return send_simple_response(response, 200, "status", "ok");
}