Debian Codegraph @

src/shared/varlink-io.systemd.service.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "bus-polkit.h"
6 #include "env-util.h"
7 #include "json-util.h"
8 #include "log.h"
9 #include "strv.h"
10 #include "utf8.h"
11 #include "varlink-io.systemd.service.h"
12
13 static SD_VARLINK_DEFINE_METHOD(Ping);
14
15 static SD_VARLINK_DEFINE_METHOD(
16 Reload,
17 VARLINK_DEFINE_POLKIT_INPUT);
18
19 static SD_VARLINK_DEFINE_METHOD(
20 SetLogLevel,
21 SD_VARLINK_FIELD_COMMENT("The maximum log level, using BSD syslog log level integers."),
22 SD_VARLINK_DEFINE_INPUT(level, SD_VARLINK_INT, SD_VARLINK_NULLABLE));
23
24 static SD_VARLINK_DEFINE_METHOD(
25 GetEnvironment,
26 SD_VARLINK_FIELD_COMMENT("Returns the current environment block, i.e. the contents of environ[]."),
27 SD_VARLINK_DEFINE_OUTPUT(environment, SD_VARLINK_STRING, SD_VARLINK_NULLABLE|SD_VARLINK_ARRAY));
28
29 static SD_VARLINK_DEFINE_ERROR(
30 InconsistentEnvironment);
31
32 SD_VARLINK_DEFINE_INTERFACE(
33 io_systemd_service,
34 "io.systemd.service",
35 SD_VARLINK_INTERFACE_COMMENT("An interface to control basic properties of systemd services."),
36 SD_VARLINK_SYMBOL_COMMENT("Checks if the service is running."),
37 &vl_method_Ping,
38 SD_VARLINK_SYMBOL_COMMENT("Reloads configuration files."),
39 &vl_method_Reload,
40 SD_VARLINK_SYMBOL_COMMENT("Sets the maximum log level."),
41 &vl_method_SetLogLevel,
42 SD_VARLINK_SYMBOL_COMMENT("Get current environment block."),
43 &vl_method_GetEnvironment,
44 SD_VARLINK_SYMBOL_COMMENT("Returned if the environment block is currently not in a valid state."),
45 &vl_error_InconsistentEnvironment);
46
47 /* Generic implementations for some of the method calls above */
48
49 int varlink_method_ping(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
50 int r;
51
52 assert(link);
53 assert(parameters);
54
55 r = sd_varlink_dispatch(link, parameters, /* dispatch_table= */ NULL, /* userdata= */ NULL);
56 if (r != 0)
57 return r;
58
59 log_debug("Received io.systemd.service.Ping");
60
61 return sd_varlink_reply(link, NULL);
62 }
63
64 int varlink_method_set_log_level(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
65 static const sd_json_dispatch_field dispatch_table[] = {
66 { "level", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_log_level, 0, SD_JSON_MANDATORY },
67 {}
68 };
69
70 int r, level;
71 uid_t uid;
72
73 assert(link);
74 assert(parameters);
75
76 r = sd_varlink_dispatch(link, parameters, dispatch_table, &level);
77 if (r != 0)
78 return r;
79
80 r = sd_varlink_get_peer_uid(link, &uid);
81 if (r < 0)
82 return r;
83
84 if (uid != 0 && uid != getuid())
85 return sd_varlink_error(link, SD_VARLINK_ERROR_PERMISSION_DENIED, parameters);
86
87 log_debug("Received io.systemd.service.SetLogLevel(%i)", level);
88
89 log_set_max_level(level);
90
91 return sd_varlink_reply(link, NULL);
92 }
93
94 int varlink_method_get_environment(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
95 uid_t uid;
96 int r;
97
98 assert(link);
99 assert(parameters);
100
101 /* This is a lot like /proc/$PID/environ, but can properly report the actual environment block as
102 * seen from the process itself, which might be quite different from the contents of the memory that
103 * was originally passed in. This is particularly relevant for cases where the environ[] block has
104 * been enlarged and similar. */
105
106 r = sd_varlink_dispatch(link, parameters, /* dispatch_table= */ NULL, /* userdata= */ NULL);
107 if (r != 0)
108 return r;
109
110 r = sd_varlink_get_peer_uid(link, &uid);
111 if (r < 0)
112 return r;
113
114 /* Don't hand out environment block to arbitrary clients, in some cases people might make the mistake
115 * of passing secrets via env vars */
116 if (uid != 0 && uid != getuid())
117 return sd_varlink_error(link, SD_VARLINK_ERROR_PERMISSION_DENIED, parameters);
118
119 log_debug("Received io.systemd.service.GetEnvironment()");
120
121 _cleanup_strv_free_ char **l = NULL;
122 STRV_FOREACH(e, environ) {
123 if (!env_assignment_is_valid(*e))
124 goto invalid;
125 if (!utf8_is_valid(*e))
126 goto invalid;
127
128 r = strv_env_replace_strdup(&l, *e);
129 if (r < 0)
130 return r;
131 }
132
133 return sd_varlink_replybo(link, SD_JSON_BUILD_PAIR_STRV("environment", l));
134
135 invalid:
136 return sd_varlink_error(link, "io.systemd.service.InconsistentEnvironment", parameters);
137 }