mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2025-12-20 04:40:04 +01:00
Add a new public function nm_utils_copy_cert_as_user() to libnm. It reads a certificate or key file on behalf of the given user and writes it to a directory in /run/NetworkManager. It is useful for VPN plugins that run as root and need to verify that the user owning the connection (the one listed in the connection.permissions property) can access the file.
45 lines
No EOL
1 KiB
C
45 lines
No EOL
1 KiB
C
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
|
|
|
#include "libnm-std-aux/nm-default-std.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
enum {
|
|
RETURN_SUCCESS = 0,
|
|
RETURN_INVALID_CMD = 1,
|
|
RETURN_INVALID_ARGS = 2,
|
|
RETURN_ERROR = 3,
|
|
};
|
|
|
|
static int
|
|
read_file_as_user(const char *filename, const char *user)
|
|
{
|
|
char error[1024];
|
|
|
|
if (!nm_utils_set_effective_user(user, error, sizeof(error))) {
|
|
fprintf(stderr, "Failed to set effective user '%s': %s", user, error);
|
|
return RETURN_ERROR;
|
|
}
|
|
|
|
if (!nm_utils_read_file_to_stdout(filename, error, sizeof(error))) {
|
|
fprintf(stderr, "Failed to read file '%s' as user '%s': %s", filename, user, error);
|
|
return RETURN_ERROR;
|
|
}
|
|
|
|
return RETURN_SUCCESS;
|
|
}
|
|
|
|
int
|
|
main(int argc, char **argv)
|
|
{
|
|
if (argc <= 1)
|
|
return RETURN_INVALID_CMD;
|
|
|
|
if (nm_streq(argv[1], "read-file-as-user")) {
|
|
if (argc != 4)
|
|
return RETURN_INVALID_ARGS;
|
|
return read_file_as_user(argv[2], argv[3]);
|
|
}
|
|
|
|
return RETURN_INVALID_CMD;
|
|
} |