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