Skip to content
Snippets Groups Projects
ssh_keys_put.c 2.43 KiB
Newer Older


/**
 * Callback function that deletes an SSH key from the list of keys authorised
 * for access to the initrd. The line will be left empty, because that keeps
 * the ids if SSH keys in tact for ssh_keys_get
 *
 * 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_put(const struct _u_request * request,
    struct _u_response * response, void * user_data)
{
    // Read ssh key id from request URI.
    const char * id_string = u_map_get(request->map_url, "id");
    if (id_string == NULL)
    {
        return send_simple_response(response, 400, "error",
            "missing url parameter `id`");
    }

    int id;
    int r = parse_int(id_string, &id);
    if (r != 0)
    {
        printf("invalid url parameter `id`: %s\n", id_string);
        return send_simple_response(response, 400, "error",
            "invalid url parameter `id`");
    }

    // 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");
    }

    // add cryptops-client command to ssh-key
    char * ssh_key_with_command;
    add_ssh_command(&ssh_key_with_command, ssh_key);

    r = replace_ssh_key(id, ssh_key_with_command);

            return send_simple_response(response, 500, "error",
                "error opening authorized_keys");
            return send_simple_response(response, 500, "error",
                "error opening authorized_keys tmp file");
        return send_simple_response(response, 500, "error",
            "Unknown error while processing ssh keys");
    }
    return send_simple_response(response, 200, "status", "ok");
}