]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
8c6db833 | 2 | |
00229fe4 | 3 | #include <endian.h> |
8c6db833 LP |
4 | #include <errno.h> |
5 | #include <fcntl.h> | |
8c6db833 | 6 | #include <pwd.h> |
8c6db833 | 7 | #include <security/_pam_macros.h> |
8c6db833 LP |
8 | #include <security/pam_ext.h> |
9 | #include <security/pam_misc.h> | |
00229fe4 LP |
10 | #include <security/pam_modules.h> |
11 | #include <security/pam_modutil.h> | |
12 | #include <sys/file.h> | |
ca78ad1d | 13 | #include <sys/stat.h> |
e21d9060 | 14 | #include <sys/sysmacros.h> |
ca78ad1d ZJS |
15 | #include <sys/types.h> |
16 | #include <unistd.h> | |
8c6db833 | 17 | |
b5efdb8a | 18 | #include "alloc-util.h" |
430f0182 | 19 | #include "audit-util.h" |
00229fe4 LP |
20 | #include "bus-common-errors.h" |
21 | #include "bus-error.h" | |
47094ce0 | 22 | #include "bus-internal.h" |
9b71e4ab | 23 | #include "bus-locator.h" |
fdb3deca | 24 | #include "cgroup-setup.h" |
3a4e4ffa | 25 | #include "devnum-util.h" |
4bbccb02 | 26 | #include "errno-util.h" |
3ffd4af2 | 27 | #include "fd-util.h" |
a5c32cff | 28 | #include "fileio.h" |
f97b34a6 | 29 | #include "format-util.h" |
f9c1f4e1 | 30 | #include "fs-util.h" |
958b66ea | 31 | #include "hostname-util.h" |
f9c1f4e1 | 32 | #include "locale-util.h" |
00229fe4 LP |
33 | #include "login-util.h" |
34 | #include "macro.h" | |
d750dde2 | 35 | #include "pam-util.h" |
6bedfcbb | 36 | #include "parse-util.h" |
e37e5ed3 | 37 | #include "path-util.h" |
ed5033fd | 38 | #include "percent-util.h" |
df0ff127 | 39 | #include "process-util.h" |
f9c1f4e1 | 40 | #include "rlimit-util.h" |
00229fe4 | 41 | #include "socket-util.h" |
055c08ef | 42 | #include "stdio-util.h" |
00229fe4 LP |
43 | #include "strv.h" |
44 | #include "terminal-util.h" | |
9ab0d3eb LP |
45 | #include "user-util.h" |
46 | #include "userdb.h" | |
8c6db833 | 47 | |
fbcb6300 LP |
48 | #define LOGIN_SLOW_BUS_CALL_TIMEOUT_USEC (2*USEC_PER_MINUTE) |
49 | ||
baae0358 LP |
50 | static int parse_argv( |
51 | pam_handle_t *handle, | |
52 | int argc, const char **argv, | |
53 | const char **class, | |
49ebd11f | 54 | const char **type, |
f5cb2820 | 55 | const char **desktop, |
baae0358 | 56 | bool *debug) { |
8c6db833 LP |
57 | |
58 | unsigned i; | |
59 | ||
60 | assert(argc >= 0); | |
61 | assert(argc == 0 || argv); | |
62 | ||
49ebd11f | 63 | for (i = 0; i < (unsigned) argc; i++) { |
d9608d40 LP |
64 | const char *p; |
65 | ||
66 | if ((p = startswith(argv[i], "class="))) { | |
485507b8 | 67 | if (class) |
d9608d40 | 68 | *class = p; |
485507b8 | 69 | |
d9608d40 | 70 | } else if ((p = startswith(argv[i], "type="))) { |
49ebd11f | 71 | if (type) |
d9608d40 | 72 | *type = p; |
49ebd11f | 73 | |
d9608d40 | 74 | } else if ((p = startswith(argv[i], "desktop="))) { |
f5cb2820 | 75 | if (desktop) |
d9608d40 | 76 | *desktop = p; |
f5cb2820 | 77 | |
05a049cc ZJS |
78 | } else if (streq(argv[i], "debug")) { |
79 | if (debug) | |
80 | *debug = true; | |
fb6becb4 | 81 | |
d9608d40 | 82 | } else if ((p = startswith(argv[i], "debug="))) { |
05a049cc | 83 | int k; |
0e318cad | 84 | |
d9608d40 | 85 | k = parse_boolean(p); |
05a049cc | 86 | if (k < 0) |
d9608d40 | 87 | pam_syslog(handle, LOG_WARNING, "Failed to parse debug= argument, ignoring: %s", p); |
05a049cc | 88 | else if (debug) |
0e318cad MS |
89 | *debug = k; |
90 | ||
05a049cc | 91 | } else |
fb6becb4 | 92 | pam_syslog(handle, LOG_WARNING, "Unknown parameter '%s', ignoring", argv[i]); |
49ebd11f | 93 | } |
8c6db833 | 94 | |
8c6db833 LP |
95 | return 0; |
96 | } | |
97 | ||
9ab0d3eb | 98 | static int acquire_user_record( |
8c6db833 | 99 | pam_handle_t *handle, |
9ab0d3eb | 100 | UserRecord **ret_record) { |
8c6db833 | 101 | |
9ab0d3eb LP |
102 | _cleanup_(user_record_unrefp) UserRecord *ur = NULL; |
103 | const char *username = NULL, *json = NULL; | |
dbe7fff4 | 104 | _cleanup_free_ char *field = NULL; |
8c6db833 LP |
105 | int r; |
106 | ||
107 | assert(handle); | |
8c6db833 | 108 | |
baae0358 | 109 | r = pam_get_user(handle, &username, NULL); |
f48e9376 ZJS |
110 | if (r != PAM_SUCCESS) |
111 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to get user name: @PAMERR@"); | |
d90b9d27 | 112 | |
f48e9376 ZJS |
113 | if (isempty(username)) |
114 | return pam_syslog_pam_error(handle, LOG_ERR, PAM_SERVICE_ERR, "User name not valid."); | |
8c6db833 | 115 | |
dbe7fff4 | 116 | /* If pam_systemd_homed (or some other module) already acquired the user record we can reuse it |
9ab0d3eb | 117 | * here. */ |
dbe7fff4 LP |
118 | field = strjoin("systemd-user-record-", username); |
119 | if (!field) | |
120 | return pam_log_oom(handle); | |
9ab0d3eb | 121 | |
dbe7fff4 | 122 | r = pam_get_data(handle, field, (const void**) &json); |
f48e9376 ZJS |
123 | if (!IN_SET(r, PAM_SUCCESS, PAM_NO_MODULE_DATA)) |
124 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to get PAM user record data: @PAMERR@"); | |
dbe7fff4 | 125 | if (r == PAM_SUCCESS && json) { |
9ab0d3eb LP |
126 | _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; |
127 | ||
128 | /* Parse cached record */ | |
129 | r = json_parse(json, JSON_PARSE_SENSITIVE, &v, NULL, NULL); | |
7e7b53b4 ZJS |
130 | if (r < 0) |
131 | return pam_syslog_errno(handle, LOG_ERR, r, "Failed to parse JSON user record: %m"); | |
9ab0d3eb LP |
132 | |
133 | ur = user_record_new(); | |
134 | if (!ur) | |
135 | return pam_log_oom(handle); | |
136 | ||
bfc0cc1a | 137 | r = user_record_load(ur, v, USER_RECORD_LOAD_REFUSE_SECRET|USER_RECORD_PERMISSIVE); |
7e7b53b4 ZJS |
138 | if (r < 0) |
139 | return pam_syslog_errno(handle, LOG_ERR, r, "Failed to load user record: %m"); | |
9ab0d3eb LP |
140 | |
141 | /* Safety check if cached record actually matches what we are looking for */ | |
f48e9376 ZJS |
142 | if (!streq_ptr(username, ur->user_name)) |
143 | return pam_syslog_pam_error(handle, LOG_ERR, PAM_SERVICE_ERR, | |
144 | "Acquired user record does not match user name."); | |
dbe7fff4 LP |
145 | } else { |
146 | _cleanup_free_ char *formatted = NULL; | |
147 | ||
148 | /* Request the record ourselves */ | |
149 | r = userdb_by_name(username, 0, &ur); | |
150 | if (r < 0) { | |
7e7b53b4 | 151 | pam_syslog_errno(handle, LOG_ERR, r, "Failed to get user record: %m"); |
dbe7fff4 LP |
152 | return PAM_USER_UNKNOWN; |
153 | } | |
154 | ||
155 | r = json_variant_format(ur->json, 0, &formatted); | |
7e7b53b4 ZJS |
156 | if (r < 0) |
157 | return pam_syslog_errno(handle, LOG_ERR, r, "Failed to format user JSON: %m"); | |
dbe7fff4 LP |
158 | |
159 | /* And cache it for everyone else */ | |
160 | r = pam_set_data(handle, field, formatted, pam_cleanup_free); | |
f48e9376 ZJS |
161 | if (r != PAM_SUCCESS) |
162 | return pam_syslog_pam_error(handle, LOG_ERR, r, | |
163 | "Failed to set PAM user record data '%s': @PAMERR@", field); | |
dbe7fff4 | 164 | TAKE_PTR(formatted); |
9ab0d3eb LP |
165 | } |
166 | ||
f48e9376 ZJS |
167 | if (!uid_is_valid(ur->uid)) |
168 | return pam_syslog_pam_error(handle, LOG_ERR, PAM_SERVICE_ERR, | |
169 | "Acquired user record does not have a UID."); | |
8c6db833 | 170 | |
9ab0d3eb LP |
171 | if (ret_record) |
172 | *ret_record = TAKE_PTR(ur); | |
8c6db833 LP |
173 | |
174 | return PAM_SUCCESS; | |
175 | } | |
176 | ||
ecd5f1a9 LP |
177 | static bool display_is_local(const char *display) { |
178 | assert(display); | |
179 | ||
180 | return | |
181 | display[0] == ':' && | |
ff25d338 | 182 | ascii_isdigit(display[1]); |
ecd5f1a9 LP |
183 | } |
184 | ||
ddf127cd TM |
185 | static int socket_from_display(const char *display) { |
186 | _cleanup_free_ char *f = NULL; | |
f7b8b5c4 | 187 | size_t k; |
ddf127cd TM |
188 | char *c; |
189 | union sockaddr_union sa; | |
190 | socklen_t sa_len; | |
191 | _cleanup_close_ int fd = -1; | |
192 | int r; | |
f7b8b5c4 LP |
193 | |
194 | assert(display); | |
f7b8b5c4 LP |
195 | |
196 | if (!display_is_local(display)) | |
197 | return -EINVAL; | |
198 | ||
199 | k = strspn(display+1, "0123456789"); | |
200 | ||
ddf127cd TM |
201 | /* Try abstract socket first. */ |
202 | f = new(char, STRLEN("@/tmp/.X11-unix/X") + k + 1); | |
f7b8b5c4 LP |
203 | if (!f) |
204 | return -ENOMEM; | |
205 | ||
ddf127cd | 206 | c = stpcpy(f, "@/tmp/.X11-unix/X"); |
f7b8b5c4 LP |
207 | memcpy(c, display+1, k); |
208 | c[k] = 0; | |
209 | ||
ddf127cd TM |
210 | r = sockaddr_un_set_path(&sa.un, f); |
211 | if (r < 0) | |
212 | return r; | |
213 | sa_len = r; | |
f7b8b5c4 | 214 | |
ddf127cd TM |
215 | fd = RET_NERRNO(socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0)); |
216 | if (fd < 0) | |
217 | return fd; | |
218 | ||
219 | r = RET_NERRNO(connect(fd, &sa.sa, sa_len)); | |
220 | if (r >= 0) | |
221 | return TAKE_FD(fd); | |
222 | if (r != -ECONNREFUSED) | |
223 | return r; | |
224 | ||
225 | /* Try also non-abstract socket. */ | |
226 | r = sockaddr_un_set_path(&sa.un, f + 1); | |
227 | if (r < 0) | |
228 | return r; | |
229 | sa_len = r; | |
230 | ||
231 | r = RET_NERRNO(connect(fd, &sa.sa, sa_len)); | |
232 | if (r >= 0) | |
233 | return TAKE_FD(fd); | |
234 | return r; | |
f7b8b5c4 LP |
235 | } |
236 | ||
4d6d6518 | 237 | static int get_seat_from_display(const char *display, const char **seat, uint32_t *vtnr) { |
ddf127cd | 238 | _cleanup_free_ char *sys_path = NULL, *tty = NULL; |
baae0358 | 239 | _cleanup_close_ int fd = -1; |
4d6d6518 | 240 | struct ucred ucred; |
f36a9d59 | 241 | int v, r; |
e21d9060 | 242 | dev_t display_ctty; |
4d6d6518 LP |
243 | |
244 | assert(display); | |
4d6d6518 LP |
245 | assert(vtnr); |
246 | ||
247 | /* We deduce the X11 socket from the display name, then use | |
248 | * SO_PEERCRED to determine the X11 server process, ask for | |
249 | * the controlling tty of that and if it's a VC then we know | |
250 | * the seat and the virtual terminal. Sounds ugly, is only | |
251 | * semi-ugly. */ | |
252 | ||
ddf127cd | 253 | fd = socket_from_display(display); |
b92bea5d | 254 | if (fd < 0) |
ddf127cd | 255 | return fd; |
4d6d6518 | 256 | |
eff05270 | 257 | r = getpeercred(fd, &ucred); |
4d6d6518 | 258 | if (r < 0) |
eff05270 | 259 | return r; |
4d6d6518 | 260 | |
e21d9060 TM |
261 | r = get_ctty_devnr(ucred.pid, &display_ctty); |
262 | if (r < 0) | |
263 | return r; | |
264 | ||
3a4e4ffa | 265 | if (asprintf(&sys_path, "/sys/dev/char/" DEVNUM_FORMAT_STR, DEVNUM_FORMAT_VAL(display_ctty)) < 0) |
e21d9060 TM |
266 | return -ENOMEM; |
267 | r = readlink_value(sys_path, &tty); | |
4d6d6518 LP |
268 | if (r < 0) |
269 | return r; | |
270 | ||
271 | v = vtnr_from_tty(tty); | |
4d6d6518 LP |
272 | if (v < 0) |
273 | return v; | |
274 | else if (v == 0) | |
275 | return -ENOENT; | |
276 | ||
fc7985ed LP |
277 | if (seat) |
278 | *seat = "seat0"; | |
4d6d6518 LP |
279 | *vtnr = (uint32_t) v; |
280 | ||
281 | return 0; | |
282 | } | |
283 | ||
00efd498 ZJS |
284 | static int export_legacy_dbus_address( |
285 | pam_handle_t *handle, | |
00efd498 ZJS |
286 | const char *runtime) { |
287 | ||
15ee6c20 ZJS |
288 | const char *s; |
289 | _cleanup_free_ char *t = NULL; | |
00efd498 ZJS |
290 | int r = PAM_BUF_ERR; |
291 | ||
15ee6c20 ZJS |
292 | /* We need to export $DBUS_SESSION_BUS_ADDRESS because various applications will not connect |
293 | * correctly to the bus without it. This setting matches what dbus.socket does for the user | |
294 | * session using 'systemctl --user set-environment'. We want to have the same configuration | |
295 | * in processes started from the PAM session. | |
296 | * | |
297 | * The setting of the address is guarded by the access() check because it is also possible to compile | |
298 | * dbus without --enable-user-session, in which case this socket is not used, and | |
299 | * $DBUS_SESSION_BUS_ADDRESS should not be set. An alternative approach would to not do the access() | |
300 | * check here, and let applications try on their own, by using "unix:path=%s/bus;autolaunch:". But we | |
301 | * expect the socket to be present by the time we do this check, so we can just as well check once | |
302 | * here. */ | |
303 | ||
304 | s = strjoina(runtime, "/bus"); | |
305 | if (access(s, F_OK) < 0) | |
306 | return PAM_SUCCESS; | |
307 | ||
308 | if (asprintf(&t, DEFAULT_USER_BUS_ADDRESS_FMT, runtime) < 0) | |
d750dde2 | 309 | return pam_log_oom(handle); |
00efd498 | 310 | |
15ee6c20 | 311 | r = pam_misc_setenv(handle, "DBUS_SESSION_BUS_ADDRESS", t, 0); |
f48e9376 ZJS |
312 | if (r != PAM_SUCCESS) |
313 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to set bus variable: @PAMERR@"); | |
00efd498 ZJS |
314 | |
315 | return PAM_SUCCESS; | |
00efd498 ZJS |
316 | } |
317 | ||
22f93314 JS |
318 | static int append_session_memory_max(pam_handle_t *handle, sd_bus_message *m, const char *limit) { |
319 | uint64_t val; | |
320 | int r; | |
321 | ||
322 | if (isempty(limit)) | |
7bfbf6cc | 323 | return PAM_SUCCESS; |
22f93314 JS |
324 | |
325 | if (streq(limit, "infinity")) { | |
f5fbe71d | 326 | r = sd_bus_message_append(m, "(sv)", "MemoryMax", "t", UINT64_MAX); |
d750dde2 LP |
327 | if (r < 0) |
328 | return pam_bus_log_create_error(handle, r); | |
329 | ||
7bfbf6cc | 330 | return PAM_SUCCESS; |
d750dde2 LP |
331 | } |
332 | ||
fe845b5e | 333 | r = parse_permyriad(limit); |
d750dde2 | 334 | if (r >= 0) { |
9cba32bc | 335 | r = sd_bus_message_append(m, "(sv)", "MemoryMaxScale", "u", UINT32_SCALE_FROM_PERMYRIAD(r)); |
d750dde2 LP |
336 | if (r < 0) |
337 | return pam_bus_log_create_error(handle, r); | |
338 | ||
7bfbf6cc | 339 | return PAM_SUCCESS; |
d750dde2 LP |
340 | } |
341 | ||
342 | r = parse_size(limit, 1024, &val); | |
343 | if (r >= 0) { | |
344 | r = sd_bus_message_append(m, "(sv)", "MemoryMax", "t", val); | |
345 | if (r < 0) | |
346 | return pam_bus_log_create_error(handle, r); | |
347 | ||
7bfbf6cc | 348 | return PAM_SUCCESS; |
22f93314 JS |
349 | } |
350 | ||
d750dde2 | 351 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.memory_max, ignoring: %s", limit); |
7bfbf6cc | 352 | return PAM_SUCCESS; |
22f93314 JS |
353 | } |
354 | ||
adc09af2 PW |
355 | static int append_session_runtime_max_sec(pam_handle_t *handle, sd_bus_message *m, const char *limit) { |
356 | usec_t val; | |
357 | int r; | |
358 | ||
359 | /* No need to parse "infinity" here, it will be set by default later in scope_init() */ | |
360 | if (isempty(limit) || streq(limit, "infinity")) | |
7bfbf6cc | 361 | return PAM_SUCCESS; |
adc09af2 PW |
362 | |
363 | r = parse_sec(limit, &val); | |
364 | if (r >= 0) { | |
365 | r = sd_bus_message_append(m, "(sv)", "RuntimeMaxUSec", "t", (uint64_t) val); | |
7bfbf6cc LP |
366 | if (r < 0) |
367 | return pam_bus_log_create_error(handle, r); | |
adc09af2 PW |
368 | } else |
369 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.runtime_max_sec: %s, ignoring.", limit); | |
370 | ||
7bfbf6cc | 371 | return PAM_SUCCESS; |
adc09af2 PW |
372 | } |
373 | ||
5fdfbbd5 | 374 | static int append_session_tasks_max(pam_handle_t *handle, sd_bus_message *m, const char *limit) { |
22f93314 JS |
375 | uint64_t val; |
376 | int r; | |
377 | ||
378 | /* No need to parse "infinity" here, it will be set unconditionally later in manager_start_scope() */ | |
379 | if (isempty(limit) || streq(limit, "infinity")) | |
7bfbf6cc | 380 | return PAM_SUCCESS; |
22f93314 JS |
381 | |
382 | r = safe_atou64(limit, &val); | |
383 | if (r >= 0) { | |
384 | r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", val); | |
d750dde2 LP |
385 | if (r < 0) |
386 | return pam_bus_log_create_error(handle, r); | |
22f93314 | 387 | } else |
d750dde2 | 388 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.tasks_max, ignoring: %s", limit); |
22f93314 | 389 | |
7bfbf6cc | 390 | return PAM_SUCCESS; |
22f93314 JS |
391 | } |
392 | ||
393 | static int append_session_cg_weight(pam_handle_t *handle, sd_bus_message *m, const char *limit, const char *field) { | |
394 | uint64_t val; | |
395 | int r; | |
c8340822 | 396 | bool is_cpu_weight; |
22f93314 | 397 | |
c8340822 | 398 | is_cpu_weight = streq(field, "CPUWeight"); |
36a4dbae | 399 | if (isempty(limit)) |
7bfbf6cc | 400 | return PAM_SUCCESS; |
36a4dbae | 401 | |
c8340822 | 402 | r = is_cpu_weight ? cg_cpu_weight_parse(limit, &val) : cg_weight_parse(limit, &val); |
36a4dbae LP |
403 | if (r >= 0) { |
404 | r = sd_bus_message_append(m, "(sv)", field, "t", val); | |
d750dde2 LP |
405 | if (r < 0) |
406 | return pam_bus_log_create_error(handle, r); | |
c8340822 | 407 | } else if (is_cpu_weight) |
d750dde2 | 408 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.cpu_weight, ignoring: %s", limit); |
36a4dbae | 409 | else |
d750dde2 | 410 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.io_weight, ignoring: %s", limit); |
22f93314 | 411 | |
7bfbf6cc | 412 | return PAM_SUCCESS; |
22f93314 JS |
413 | } |
414 | ||
0ecc1c9d LP |
415 | static const char* getenv_harder(pam_handle_t *handle, const char *key, const char *fallback) { |
416 | const char *v; | |
417 | ||
418 | assert(handle); | |
419 | assert(key); | |
420 | ||
5238e957 | 421 | /* Looks for an environment variable, preferably in the environment block associated with the |
83d4ab55 LP |
422 | * specified PAM handle, falling back to the process' block instead. Why check both? Because we want |
423 | * to permit configuration of session properties from unit files that invoke PAM services, so that | |
424 | * PAM services don't have to be reworked to set systemd-specific properties, but these properties | |
425 | * can still be set from the unit file Environment= block. */ | |
0ecc1c9d LP |
426 | |
427 | v = pam_getenv(handle, key); | |
428 | if (!isempty(v)) | |
429 | return v; | |
430 | ||
83d4ab55 LP |
431 | /* We use secure_getenv() here, since we might get loaded into su/sudo, which are SUID. Ideally |
432 | * they'd clean up the environment before invoking foreign code (such as PAM modules), but alas they | |
433 | * currently don't (to be precise, they clean up the environment they pass to their children, but | |
434 | * not their own environ[]). */ | |
435 | v = secure_getenv(key); | |
0ecc1c9d LP |
436 | if (!isempty(v)) |
437 | return v; | |
438 | ||
439 | return fallback; | |
440 | } | |
441 | ||
d6baaa69 LP |
442 | static int update_environment(pam_handle_t *handle, const char *key, const char *value) { |
443 | int r; | |
444 | ||
445 | assert(handle); | |
446 | assert(key); | |
447 | ||
448 | /* Updates the environment, but only if there's actually a value set. Also, log about errors */ | |
449 | ||
450 | if (isempty(value)) | |
451 | return PAM_SUCCESS; | |
452 | ||
453 | r = pam_misc_setenv(handle, key, value, 0); | |
454 | if (r != PAM_SUCCESS) | |
f48e9376 ZJS |
455 | return pam_syslog_pam_error(handle, LOG_ERR, r, |
456 | "Failed to set environment variable %s: @PAMERR@", key); | |
d6baaa69 | 457 | |
f48e9376 | 458 | return PAM_SUCCESS; |
d6baaa69 LP |
459 | } |
460 | ||
b9217112 LP |
461 | static bool validate_runtime_directory(pam_handle_t *handle, const char *path, uid_t uid) { |
462 | struct stat st; | |
463 | ||
dca81e28 | 464 | assert(handle); |
b9217112 LP |
465 | assert(path); |
466 | ||
e0d70f76 LP |
467 | /* Some extra paranoia: let's not set $XDG_RUNTIME_DIR if the directory we'd set it to isn't actually |
468 | * set up properly for us. This is supposed to provide a careful safety net for supporting su/sudo | |
469 | * type transitions: in that case the UID changes, but the session and thus the user owning it | |
15e6a6e8 | 470 | * doesn't change. Since the $XDG_RUNTIME_DIR lifecycle is bound to the session's user being logged |
e0d70f76 LP |
471 | * in at least once we should be particularly careful when setting the environment variable, since |
472 | * otherwise we might end up setting $XDG_RUNTIME_DIR to some directory owned by the wrong user. */ | |
b9217112 | 473 | |
6d06dfad LP |
474 | if (!path_is_absolute(path)) { |
475 | pam_syslog(handle, LOG_ERR, "Provided runtime directory '%s' is not absolute.", path); | |
476 | goto fail; | |
477 | } | |
478 | ||
b9217112 | 479 | if (lstat(path, &st) < 0) { |
7e7b53b4 | 480 | pam_syslog_errno(handle, LOG_ERR, errno, "Failed to stat() runtime directory '%s': %m", path); |
b9217112 LP |
481 | goto fail; |
482 | } | |
483 | ||
484 | if (!S_ISDIR(st.st_mode)) { | |
485 | pam_syslog(handle, LOG_ERR, "Runtime directory '%s' is not actually a directory.", path); | |
486 | goto fail; | |
487 | } | |
488 | ||
489 | if (st.st_uid != uid) { | |
490 | pam_syslog(handle, LOG_ERR, "Runtime directory '%s' is not owned by UID " UID_FMT ", as it should.", path, uid); | |
491 | goto fail; | |
492 | } | |
493 | ||
494 | return true; | |
495 | ||
496 | fail: | |
497 | pam_syslog(handle, LOG_WARNING, "Not setting $XDG_RUNTIME_DIR, as the directory is not in order."); | |
498 | return false; | |
499 | } | |
500 | ||
f9c1f4e1 LP |
501 | static int pam_putenv_and_log(pam_handle_t *handle, const char *e, bool debug) { |
502 | int r; | |
503 | ||
504 | assert(handle); | |
505 | assert(e); | |
506 | ||
507 | r = pam_putenv(handle, e); | |
f48e9376 ZJS |
508 | if (r != PAM_SUCCESS) |
509 | return pam_syslog_pam_error(handle, LOG_ERR, r, | |
510 | "Failed to set PAM environment variable %s: @PAMERR@", e); | |
f9c1f4e1 LP |
511 | |
512 | if (debug) | |
513 | pam_syslog(handle, LOG_DEBUG, "PAM environment variable %s set based on user record.", e); | |
514 | ||
515 | return PAM_SUCCESS; | |
516 | } | |
517 | ||
518 | static int apply_user_record_settings(pam_handle_t *handle, UserRecord *ur, bool debug) { | |
f9c1f4e1 LP |
519 | int r; |
520 | ||
521 | assert(handle); | |
522 | assert(ur); | |
523 | ||
524 | if (ur->umask != MODE_INVALID) { | |
525 | umask(ur->umask); | |
526 | ||
527 | if (debug) | |
528 | pam_syslog(handle, LOG_DEBUG, "Set user umask to %04o based on user record.", ur->umask); | |
529 | } | |
530 | ||
531 | STRV_FOREACH(i, ur->environment) { | |
532 | _cleanup_free_ char *n = NULL; | |
533 | const char *e; | |
534 | ||
535 | assert_se(e = strchr(*i, '=')); /* environment was already validated while parsing JSON record, this thus must hold */ | |
536 | ||
537 | n = strndup(*i, e - *i); | |
538 | if (!n) | |
539 | return pam_log_oom(handle); | |
540 | ||
541 | if (pam_getenv(handle, n)) { | |
542 | if (debug) | |
543 | pam_syslog(handle, LOG_DEBUG, "PAM environment variable $%s already set, not changing based on record.", *i); | |
544 | continue; | |
545 | } | |
546 | ||
547 | r = pam_putenv_and_log(handle, *i, debug); | |
548 | if (r != PAM_SUCCESS) | |
549 | return r; | |
550 | } | |
551 | ||
552 | if (ur->email_address) { | |
553 | if (pam_getenv(handle, "EMAIL")) { | |
554 | if (debug) | |
555 | pam_syslog(handle, LOG_DEBUG, "PAM environment variable $EMAIL already set, not changing based on user record."); | |
556 | } else { | |
557 | _cleanup_free_ char *joined = NULL; | |
558 | ||
559 | joined = strjoin("EMAIL=", ur->email_address); | |
560 | if (!joined) | |
561 | return pam_log_oom(handle); | |
562 | ||
563 | r = pam_putenv_and_log(handle, joined, debug); | |
564 | if (r != PAM_SUCCESS) | |
565 | return r; | |
566 | } | |
567 | } | |
568 | ||
569 | if (ur->time_zone) { | |
570 | if (pam_getenv(handle, "TZ")) { | |
571 | if (debug) | |
572 | pam_syslog(handle, LOG_DEBUG, "PAM environment variable $TZ already set, not changing based on user record."); | |
573 | } else if (!timezone_is_valid(ur->time_zone, LOG_DEBUG)) { | |
574 | if (debug) | |
575 | pam_syslog(handle, LOG_DEBUG, "Time zone specified in user record is not valid locally, not setting $TZ."); | |
576 | } else { | |
577 | _cleanup_free_ char *joined = NULL; | |
578 | ||
579 | joined = strjoin("TZ=:", ur->time_zone); | |
580 | if (!joined) | |
581 | return pam_log_oom(handle); | |
582 | ||
583 | r = pam_putenv_and_log(handle, joined, debug); | |
584 | if (r != PAM_SUCCESS) | |
585 | return r; | |
586 | } | |
587 | } | |
588 | ||
589 | if (ur->preferred_language) { | |
590 | if (pam_getenv(handle, "LANG")) { | |
591 | if (debug) | |
592 | pam_syslog(handle, LOG_DEBUG, "PAM environment variable $LANG already set, not changing based on user record."); | |
a00a78b8 | 593 | } else if (locale_is_installed(ur->preferred_language) <= 0) { |
f9c1f4e1 | 594 | if (debug) |
a00a78b8 | 595 | pam_syslog(handle, LOG_DEBUG, "Preferred language specified in user record is not valid or not installed, not setting $LANG."); |
f9c1f4e1 LP |
596 | } else { |
597 | _cleanup_free_ char *joined = NULL; | |
598 | ||
599 | joined = strjoin("LANG=", ur->preferred_language); | |
600 | if (!joined) | |
601 | return pam_log_oom(handle); | |
602 | ||
603 | r = pam_putenv_and_log(handle, joined, debug); | |
604 | if (r != PAM_SUCCESS) | |
605 | return r; | |
606 | } | |
607 | } | |
608 | ||
609 | if (nice_is_valid(ur->nice_level)) { | |
610 | if (nice(ur->nice_level) < 0) | |
7e7b53b4 ZJS |
611 | pam_syslog_errno(handle, LOG_ERR, errno, |
612 | "Failed to set nice level to %i, ignoring: %m", ur->nice_level); | |
f9c1f4e1 | 613 | else if (debug) |
7e7b53b4 ZJS |
614 | pam_syslog(handle, LOG_DEBUG, |
615 | "Nice level set to %i, based on user record.", ur->nice_level); | |
f9c1f4e1 LP |
616 | } |
617 | ||
618 | for (int rl = 0; rl < _RLIMIT_MAX; rl++) { | |
619 | ||
620 | if (!ur->rlimits[rl]) | |
621 | continue; | |
622 | ||
623 | r = setrlimit_closest(rl, ur->rlimits[rl]); | |
624 | if (r < 0) | |
7e7b53b4 ZJS |
625 | pam_syslog_errno(handle, LOG_ERR, r, |
626 | "Failed to set resource limit %s, ignoring: %m", rlimit_to_string(rl)); | |
f9c1f4e1 | 627 | else if (debug) |
7e7b53b4 ZJS |
628 | pam_syslog(handle, LOG_DEBUG, |
629 | "Resource limit %s set, based on user record.", rlimit_to_string(rl)); | |
f9c1f4e1 LP |
630 | } |
631 | ||
632 | return PAM_SUCCESS; | |
633 | } | |
634 | ||
e0d70f76 LP |
635 | static int configure_runtime_directory( |
636 | pam_handle_t *handle, | |
637 | UserRecord *ur, | |
638 | const char *rt) { | |
639 | ||
640 | int r; | |
641 | ||
642 | assert(handle); | |
643 | assert(ur); | |
644 | assert(rt); | |
645 | ||
646 | if (!validate_runtime_directory(handle, rt, ur->uid)) | |
647 | return PAM_SUCCESS; | |
648 | ||
649 | r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", rt, 0); | |
f48e9376 ZJS |
650 | if (r != PAM_SUCCESS) |
651 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to set runtime dir: @PAMERR@"); | |
e0d70f76 LP |
652 | |
653 | return export_legacy_dbus_address(handle, rt); | |
654 | } | |
655 | ||
98a28fef | 656 | _public_ PAM_EXTERN int pam_sm_open_session( |
8c6db833 LP |
657 | pam_handle_t *handle, |
658 | int flags, | |
659 | int argc, const char **argv) { | |
660 | ||
4afd3348 | 661 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
22f93314 | 662 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; |
d1529c9e | 663 | const char |
9ab0d3eb | 664 | *id, *object_path, *runtime_path, |
d1529c9e LP |
665 | *service = NULL, |
666 | *tty = NULL, *display = NULL, | |
667 | *remote_user = NULL, *remote_host = NULL, | |
668 | *seat = NULL, | |
669 | *type = NULL, *class = NULL, | |
f5cb2820 | 670 | *class_pam = NULL, *type_pam = NULL, *cvtnr = NULL, *desktop = NULL, *desktop_pam = NULL, |
adc09af2 | 671 | *memory_max = NULL, *tasks_max = NULL, *cpu_weight = NULL, *io_weight = NULL, *runtime_max_sec = NULL; |
4afd3348 | 672 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
9ab0d3eb | 673 | _cleanup_(user_record_unrefp) UserRecord *ur = NULL; |
d1529c9e | 674 | int session_fd = -1, existing, r; |
d1529c9e | 675 | bool debug = false, remote; |
baae0358 LP |
676 | uint32_t vtnr = 0; |
677 | uid_t original_uid; | |
8c6db833 | 678 | |
ffcfcb6b | 679 | assert(handle); |
98a28fef | 680 | |
e9fbc77c LP |
681 | if (parse_argv(handle, |
682 | argc, argv, | |
fb6becb4 | 683 | &class_pam, |
49ebd11f | 684 | &type_pam, |
f5cb2820 | 685 | &desktop_pam, |
5a330cda ZJS |
686 | &debug) < 0) |
687 | return PAM_SESSION_ERR; | |
74fe1fe3 | 688 | |
baae0358 | 689 | if (debug) |
3831838a | 690 | pam_syslog(handle, LOG_DEBUG, "pam-systemd initializing"); |
baae0358 | 691 | |
9ab0d3eb | 692 | r = acquire_user_record(handle, &ur); |
42e66809 | 693 | if (r != PAM_SUCCESS) |
5a330cda | 694 | return r; |
8c6db833 | 695 | |
e945dd9e LP |
696 | /* Make most of this a NOP on non-logind systems */ |
697 | if (!logind_running()) | |
698 | goto success; | |
699 | ||
30b2c336 LP |
700 | /* Make sure we don't enter a loop by talking to |
701 | * systemd-logind when it is actually waiting for the | |
702 | * background to finish start-up. If the service is | |
5c390a4a | 703 | * "systemd-user" we simply set XDG_RUNTIME_DIR and |
30b2c336 LP |
704 | * leave. */ |
705 | ||
148369de | 706 | r = pam_get_item(handle, PAM_SERVICE, (const void**) &service); |
f48e9376 ZJS |
707 | if (!IN_SET(r, PAM_BAD_ITEM, PAM_SUCCESS)) |
708 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to get PAM service: @PAMERR@"); | |
5c390a4a | 709 | if (streq_ptr(service, "systemd-user")) { |
055c08ef | 710 | char rt[STRLEN("/run/user/") + DECIMAL_STR_MAX(uid_t)]; |
30b2c336 | 711 | |
9ab0d3eb | 712 | xsprintf(rt, "/run/user/"UID_FMT, ur->uid); |
e0d70f76 | 713 | r = configure_runtime_directory(handle, ur, rt); |
00efd498 ZJS |
714 | if (r != PAM_SUCCESS) |
715 | return r; | |
716 | ||
e945dd9e | 717 | goto success; |
8c6db833 LP |
718 | } |
719 | ||
ffcfcb6b | 720 | /* Otherwise, we ask logind to create a session for us */ |
8c6db833 | 721 | |
148369de | 722 | r = pam_get_item(handle, PAM_XDISPLAY, (const void**) &display); |
f48e9376 ZJS |
723 | if (!IN_SET(r, PAM_BAD_ITEM, PAM_SUCCESS)) |
724 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to get PAM_XDISPLAY: @PAMERR@"); | |
148369de | 725 | r = pam_get_item(handle, PAM_TTY, (const void**) &tty); |
f48e9376 ZJS |
726 | if (!IN_SET(r, PAM_BAD_ITEM, PAM_SUCCESS)) |
727 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to get PAM_TTY: @PAMERR@"); | |
148369de | 728 | r = pam_get_item(handle, PAM_RUSER, (const void**) &remote_user); |
f48e9376 ZJS |
729 | if (!IN_SET(r, PAM_BAD_ITEM, PAM_SUCCESS)) |
730 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to get PAM_RUSER: @PAMERR@"); | |
148369de | 731 | r = pam_get_item(handle, PAM_RHOST, (const void**) &remote_host); |
f48e9376 ZJS |
732 | if (!IN_SET(r, PAM_BAD_ITEM, PAM_SUCCESS)) |
733 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to get PAM_RHOST: @PAMERR@"); | |
a8573ccc | 734 | |
0ecc1c9d LP |
735 | seat = getenv_harder(handle, "XDG_SEAT", NULL); |
736 | cvtnr = getenv_harder(handle, "XDG_VTNR", NULL); | |
737 | type = getenv_harder(handle, "XDG_SESSION_TYPE", type_pam); | |
738 | class = getenv_harder(handle, "XDG_SESSION_CLASS", class_pam); | |
f5cb2820 | 739 | desktop = getenv_harder(handle, "XDG_SESSION_DESKTOP", desktop_pam); |
a4cd87e9 | 740 | |
ed18b08b | 741 | tty = strempty(tty); |
98a28fef | 742 | |
ee8545b0 | 743 | if (strchr(tty, ':')) { |
3a736949 LP |
744 | /* A tty with a colon is usually an X11 display, placed there to show up in utmp. We rearrange things |
745 | * and don't pretend that an X display was a tty. */ | |
ee8545b0 LP |
746 | if (isempty(display)) |
747 | display = tty; | |
a4cd87e9 | 748 | tty = NULL; |
3a736949 | 749 | |
1a4459d6 | 750 | } else if (streq(tty, "cron")) { |
3a736949 LP |
751 | /* cron is setting PAM_TTY to "cron" for some reason (the commit carries no information why, but |
752 | * probably because it wants to set it to something as pam_time/pam_access/… require PAM_TTY to be set | |
753 | * (as they otherwise even try to update it!) — but cron doesn't actually allocate a TTY for its forked | |
754 | * off processes.) */ | |
0ad1271f | 755 | type = "unspecified"; |
49ebd11f | 756 | class = "background"; |
a4cd87e9 | 757 | tty = NULL; |
3a736949 | 758 | |
0ad1271f | 759 | } else if (streq(tty, "ssh")) { |
3a736949 LP |
760 | /* ssh has been setting PAM_TTY to "ssh" (for the same reason as cron does this, see above. For further |
761 | * details look for "PAM_TTY_KLUDGE" in the openssh sources). */ | |
0ad1271f | 762 | type ="tty"; |
49ebd11f | 763 | class = "user"; |
3a736949 LP |
764 | tty = NULL; /* This one is particularly sad, as this means that ssh sessions — even though usually |
765 | * associated with a pty — won't be tracked by their tty in logind. This is because ssh | |
766 | * does the PAM session registration early for new connections, and registers a pty only | |
767 | * much later (this is because it doesn't know yet if it needs one at all, as whether to | |
768 | * register a pty or not is negotiated much later in the protocol). */ | |
513cf7da | 769 | |
c9ed61e7 LP |
770 | } else |
771 | /* Chop off leading /dev prefix that some clients specify, but others do not. */ | |
772 | tty = skip_dev_prefix(tty); | |
ee8545b0 | 773 | |
8e7705e5 | 774 | /* If this fails vtnr will be 0, that's intended */ |
4d6d6518 | 775 | if (!isempty(cvtnr)) |
2ae4842b | 776 | (void) safe_atou32(cvtnr, &vtnr); |
4d6d6518 | 777 | |
92bd5ff3 | 778 | if (!isempty(display) && !vtnr) { |
fc7985ed | 779 | if (isempty(seat)) |
d487e2d6 | 780 | (void) get_seat_from_display(display, &seat, &vtnr); |
fc7985ed | 781 | else if (streq(seat, "seat0")) |
d487e2d6 | 782 | (void) get_seat_from_display(display, NULL, &vtnr); |
fc7985ed | 783 | } |
4d6d6518 | 784 | |
49ebd11f | 785 | if (seat && !streq(seat, "seat0") && vtnr != 0) { |
2675747f LP |
786 | if (debug) |
787 | pam_syslog(handle, LOG_DEBUG, "Ignoring vtnr %"PRIu32" for %s which is not seat0", vtnr, seat); | |
d7353ef6 MM |
788 | vtnr = 0; |
789 | } | |
790 | ||
49ebd11f | 791 | if (isempty(type)) |
0ad1271f | 792 | type = !isempty(display) ? "x11" : |
49ebd11f | 793 | !isempty(tty) ? "tty" : "unspecified"; |
98a28fef | 794 | |
55efac6c | 795 | if (isempty(class)) |
e2acb67b | 796 | class = streq(type, "unspecified") ? "background" : "user"; |
55efac6c | 797 | |
fecc80c1 | 798 | remote = !isempty(remote_host) && !is_localhost(remote_host); |
98a28fef | 799 | |
148369de | 800 | r = pam_get_data(handle, "systemd.memory_max", (const void **)&memory_max); |
f48e9376 ZJS |
801 | if (!IN_SET(r, PAM_SUCCESS, PAM_NO_MODULE_DATA)) |
802 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to get PAM systemd.memory_max data: @PAMERR@"); | |
148369de | 803 | r = pam_get_data(handle, "systemd.tasks_max", (const void **)&tasks_max); |
f48e9376 ZJS |
804 | if (!IN_SET(r, PAM_SUCCESS, PAM_NO_MODULE_DATA)) |
805 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to get PAM systemd.tasks_max data: @PAMERR@"); | |
148369de | 806 | r = pam_get_data(handle, "systemd.cpu_weight", (const void **)&cpu_weight); |
f48e9376 ZJS |
807 | if (!IN_SET(r, PAM_SUCCESS, PAM_NO_MODULE_DATA)) |
808 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to get PAM systemd.cpu_weight data: @PAMERR@"); | |
148369de | 809 | r = pam_get_data(handle, "systemd.io_weight", (const void **)&io_weight); |
f48e9376 ZJS |
810 | if (!IN_SET(r, PAM_SUCCESS, PAM_NO_MODULE_DATA)) |
811 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to get PAM systemd.io_weight data: @PAMERR@"); | |
148369de | 812 | r = pam_get_data(handle, "systemd.runtime_max_sec", (const void **)&runtime_max_sec); |
f48e9376 ZJS |
813 | if (!IN_SET(r, PAM_SUCCESS, PAM_NO_MODULE_DATA)) |
814 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to get PAM systemd.runtime_max_sec data: @PAMERR@"); | |
22f93314 | 815 | |
4d49b48c | 816 | /* Talk to logind over the message bus */ |
5a330cda | 817 | |
355c9966 LP |
818 | r = pam_acquire_bus_connection(handle, &bus); |
819 | if (r != PAM_SUCCESS) | |
820 | return r; | |
cc377381 | 821 | |
22f93314 | 822 | if (debug) { |
ce959314 | 823 | pam_syslog(handle, LOG_DEBUG, "Asking logind to create session: " |
1fa2f38f | 824 | "uid="UID_FMT" pid="PID_FMT" service=%s type=%s class=%s desktop=%s seat=%s vtnr=%"PRIu32" tty=%s display=%s remote=%s remote_user=%s remote_host=%s", |
9ab0d3eb | 825 | ur->uid, getpid_cached(), |
baae0358 | 826 | strempty(service), |
cda7ecb0 | 827 | type, class, strempty(desktop), |
a4cd87e9 | 828 | strempty(seat), vtnr, strempty(tty), strempty(display), |
baae0358 | 829 | yes_no(remote), strempty(remote_user), strempty(remote_host)); |
22f93314 | 830 | pam_syslog(handle, LOG_DEBUG, "Session limits: " |
adc09af2 PW |
831 | "memory_max=%s tasks_max=%s cpu_weight=%s io_weight=%s runtime_max_sec=%s", |
832 | strna(memory_max), strna(tasks_max), strna(cpu_weight), strna(io_weight), strna(runtime_max_sec)); | |
22f93314 JS |
833 | } |
834 | ||
5d990cc5 | 835 | r = bus_message_new_method_call(bus, &m, bus_login_mgr, "CreateSession"); |
d750dde2 LP |
836 | if (r < 0) |
837 | return pam_bus_log_create_error(handle, r); | |
22f93314 JS |
838 | |
839 | r = sd_bus_message_append(m, "uusssssussbss", | |
9ab0d3eb | 840 | (uint32_t) ur->uid, |
da0da5ec | 841 | 0, |
22f93314 JS |
842 | service, |
843 | type, | |
844 | class, | |
845 | desktop, | |
846 | seat, | |
847 | vtnr, | |
848 | tty, | |
849 | display, | |
850 | remote, | |
851 | remote_user, | |
852 | remote_host); | |
d750dde2 LP |
853 | if (r < 0) |
854 | return pam_bus_log_create_error(handle, r); | |
22f93314 JS |
855 | |
856 | r = sd_bus_message_open_container(m, 'a', "(sv)"); | |
d750dde2 LP |
857 | if (r < 0) |
858 | return pam_bus_log_create_error(handle, r); | |
22f93314 JS |
859 | |
860 | r = append_session_memory_max(handle, m, memory_max); | |
7bfbf6cc LP |
861 | if (r != PAM_SUCCESS) |
862 | return r; | |
22f93314 | 863 | |
adc09af2 | 864 | r = append_session_runtime_max_sec(handle, m, runtime_max_sec); |
7bfbf6cc LP |
865 | if (r != PAM_SUCCESS) |
866 | return r; | |
adc09af2 | 867 | |
22f93314 | 868 | r = append_session_tasks_max(handle, m, tasks_max); |
7bfbf6cc LP |
869 | if (r != PAM_SUCCESS) |
870 | return r; | |
22f93314 JS |
871 | |
872 | r = append_session_cg_weight(handle, m, cpu_weight, "CPUWeight"); | |
7bfbf6cc LP |
873 | if (r != PAM_SUCCESS) |
874 | return r; | |
22f93314 JS |
875 | |
876 | r = append_session_cg_weight(handle, m, io_weight, "IOWeight"); | |
7bfbf6cc LP |
877 | if (r != PAM_SUCCESS) |
878 | return r; | |
22f93314 JS |
879 | |
880 | r = sd_bus_message_close_container(m); | |
d750dde2 LP |
881 | if (r < 0) |
882 | return pam_bus_log_create_error(handle, r); | |
ce959314 | 883 | |
fbcb6300 | 884 | r = sd_bus_call(bus, m, LOGIN_SLOW_BUS_CALL_TIMEOUT_USEC, &error, &reply); |
ffcfcb6b | 885 | if (r < 0) { |
b80120c4 | 886 | if (sd_bus_error_has_name(&error, BUS_ERROR_SESSION_BUSY)) { |
2675747f | 887 | if (debug) |
c7e93c4d | 888 | pam_syslog(handle, LOG_DEBUG, "Not creating session: %s", bus_error_message(&error, r)); |
e945dd9e LP |
889 | |
890 | /* We are already in a session, don't do anything */ | |
891 | goto success; | |
b80120c4 DH |
892 | } else { |
893 | pam_syslog(handle, LOG_ERR, "Failed to create session: %s", bus_error_message(&error, r)); | |
8d46418e | 894 | return PAM_SESSION_ERR; |
b80120c4 | 895 | } |
98a28fef | 896 | } |
74fe1fe3 | 897 | |
ffcfcb6b | 898 | r = sd_bus_message_read(reply, |
baae0358 | 899 | "soshusub", |
ffcfcb6b ZJS |
900 | &id, |
901 | &object_path, | |
902 | &runtime_path, | |
903 | &session_fd, | |
baae0358 | 904 | &original_uid, |
ffcfcb6b ZJS |
905 | &seat, |
906 | &vtnr, | |
907 | &existing); | |
d750dde2 LP |
908 | if (r < 0) |
909 | return pam_bus_log_parse_error(handle, r); | |
74fe1fe3 | 910 | |
ce959314 MS |
911 | if (debug) |
912 | pam_syslog(handle, LOG_DEBUG, "Reply from logind: " | |
baae0358 LP |
913 | "id=%s object_path=%s runtime_path=%s session_fd=%d seat=%s vtnr=%u original_uid=%u", |
914 | id, object_path, runtime_path, session_fd, seat, vtnr, original_uid); | |
ce959314 | 915 | |
d6baaa69 LP |
916 | r = update_environment(handle, "XDG_SESSION_ID", id); |
917 | if (r != PAM_SUCCESS) | |
5a330cda | 918 | return r; |
8c6db833 | 919 | |
9ab0d3eb | 920 | if (original_uid == ur->uid) { |
e0d70f76 LP |
921 | /* Don't set $XDG_RUNTIME_DIR if the user we now authenticated for does not match the |
922 | * original user of the session. We do this in order not to result in privileged apps | |
923 | * clobbering the runtime directory unnecessarily. */ | |
00efd498 | 924 | |
e0d70f76 | 925 | r = configure_runtime_directory(handle, ur, runtime_path); |
00efd498 ZJS |
926 | if (r != PAM_SUCCESS) |
927 | return r; | |
98a28fef | 928 | } |
8c6db833 | 929 | |
b2f74f07 LP |
930 | /* Most likely we got the session/type/class from environment variables, but might have gotten the data |
931 | * somewhere else (for example PAM module parameters). Let's now update the environment variables, so that this | |
932 | * data is inherited into the session processes, and programs can rely on them to be initialized. */ | |
933 | ||
934 | r = update_environment(handle, "XDG_SESSION_TYPE", type); | |
935 | if (r != PAM_SUCCESS) | |
936 | return r; | |
937 | ||
938 | r = update_environment(handle, "XDG_SESSION_CLASS", class); | |
939 | if (r != PAM_SUCCESS) | |
940 | return r; | |
941 | ||
942 | r = update_environment(handle, "XDG_SESSION_DESKTOP", desktop); | |
943 | if (r != PAM_SUCCESS) | |
944 | return r; | |
945 | ||
d6baaa69 LP |
946 | r = update_environment(handle, "XDG_SEAT", seat); |
947 | if (r != PAM_SUCCESS) | |
948 | return r; | |
bbc73283 LP |
949 | |
950 | if (vtnr > 0) { | |
29d230f6 | 951 | char buf[DECIMAL_STR_MAX(vtnr)]; |
baae0358 | 952 | sprintf(buf, "%u", vtnr); |
bbc73283 | 953 | |
d6baaa69 LP |
954 | r = update_environment(handle, "XDG_VTNR", buf); |
955 | if (r != PAM_SUCCESS) | |
5a330cda | 956 | return r; |
bbc73283 LP |
957 | } |
958 | ||
77085881 | 959 | r = pam_set_data(handle, "systemd.existing", INT_TO_PTR(!!existing), NULL); |
f48e9376 ZJS |
960 | if (r != PAM_SUCCESS) |
961 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to install existing flag: @PAMERR@"); | |
77085881 | 962 | |
21c390cc | 963 | if (session_fd >= 0) { |
f48e9376 ZJS |
964 | _cleanup_close_ int fd = fcntl(session_fd, F_DUPFD_CLOEXEC, 3); |
965 | if (fd < 0) | |
7e7b53b4 | 966 | return pam_syslog_errno(handle, LOG_ERR, errno, "Failed to dup session fd: %m"); |
5a330cda | 967 | |
f48e9376 ZJS |
968 | r = pam_set_data(handle, "systemd.session-fd", FD_TO_PTR(fd), NULL); |
969 | if (r != PAM_SUCCESS) | |
970 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to install session fd: @PAMERR@"); | |
971 | TAKE_FD(fd); | |
98a28fef | 972 | } |
8c6db833 | 973 | |
e945dd9e | 974 | success: |
f9c1f4e1 LP |
975 | r = apply_user_record_settings(handle, ur, debug); |
976 | if (r != PAM_SUCCESS) | |
977 | return r; | |
978 | ||
355c9966 | 979 | /* Let's release the D-Bus connection, after all the session might live quite a long time, and we are |
f9c1f4e1 LP |
980 | * not going to use the bus connection in that time, so let's better close before the daemon kicks us |
981 | * off because we are not processing anything. */ | |
355c9966 | 982 | (void) pam_release_bus_connection(handle); |
ffcfcb6b | 983 | return PAM_SUCCESS; |
98a28fef | 984 | } |
8c6db833 | 985 | |
98a28fef LP |
986 | _public_ PAM_EXTERN int pam_sm_close_session( |
987 | pam_handle_t *handle, | |
988 | int flags, | |
989 | int argc, const char **argv) { | |
8c6db833 | 990 | |
5f41d1f1 | 991 | const void *existing = NULL; |
45c5fa25 | 992 | bool debug = false; |
75c8e3cf | 993 | const char *id; |
75c8e3cf | 994 | int r; |
8c6db833 | 995 | |
ffcfcb6b | 996 | assert(handle); |
75c8e3cf | 997 | |
45c5fa25 LP |
998 | if (parse_argv(handle, |
999 | argc, argv, | |
1000 | NULL, | |
1001 | NULL, | |
1002 | NULL, | |
1003 | &debug) < 0) | |
1004 | return PAM_SESSION_ERR; | |
1005 | ||
1006 | if (debug) | |
1007 | pam_syslog(handle, LOG_DEBUG, "pam-systemd shutting down"); | |
1008 | ||
77085881 LP |
1009 | /* Only release session if it wasn't pre-existing when we |
1010 | * tried to create it */ | |
148369de | 1011 | r = pam_get_data(handle, "systemd.existing", &existing); |
f48e9376 ZJS |
1012 | if (!IN_SET(r, PAM_SUCCESS, PAM_NO_MODULE_DATA)) |
1013 | return pam_syslog_pam_error(handle, LOG_ERR, r, | |
1014 | "Failed to get PAM systemd.existing data: @PAMERR@"); | |
77085881 | 1015 | |
75c8e3cf | 1016 | id = pam_getenv(handle, "XDG_SESSION_ID"); |
77085881 | 1017 | if (id && !existing) { |
355c9966 LP |
1018 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
1019 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
75c8e3cf | 1020 | |
355c9966 LP |
1021 | /* Before we go and close the FIFO we need to tell logind that this is a clean session |
1022 | * shutdown, so that it doesn't just go and slaughter us immediately after closing the fd */ | |
75c8e3cf | 1023 | |
355c9966 LP |
1024 | r = pam_acquire_bus_connection(handle, &bus); |
1025 | if (r != PAM_SUCCESS) | |
1026 | return r; | |
75c8e3cf | 1027 | |
5d990cc5 | 1028 | r = bus_call_method(bus, bus_login_mgr, "ReleaseSession", &error, NULL, "s", id); |
f48e9376 ZJS |
1029 | if (r < 0) |
1030 | return pam_syslog_pam_error(handle, LOG_ERR, PAM_SESSION_ERR, | |
1031 | "Failed to release session: %s", bus_error_message(&error, r)); | |
75c8e3cf LP |
1032 | } |
1033 | ||
355c9966 LP |
1034 | /* Note that we are knowingly leaking the FIFO fd here. This way, logind can watch us die. If we |
1035 | * closed it here it would not have any clue when that is completed. Given that one cannot really | |
1036 | * have multiple PAM sessions open from the same process this means we will leak one FD at max. */ | |
75c8e3cf | 1037 | |
5f41d1f1 | 1038 | return PAM_SUCCESS; |
8c6db833 | 1039 | } |