chiark / gitweb /
sd-login: return size of arrays as return value in functions that generate an array
[elogind.git] / src / pam-module.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <sys/file.h>
25 #include <pwd.h>
26 #include <endian.h>
27 #include <sys/capability.h>
28
29 #include <security/pam_modules.h>
30 #include <security/_pam_macros.h>
31 #include <security/pam_modutil.h>
32 #include <security/pam_ext.h>
33 #include <security/pam_misc.h>
34
35 #include "util.h"
36 #include "macro.h"
37 #include "sd-daemon.h"
38 #include "strv.h"
39 #include "dbus-common.h"
40 #include "def.h"
41 #include "socket-util.h"
42
43 static int parse_argv(pam_handle_t *handle,
44                       int argc, const char **argv,
45                       char ***controllers,
46                       char ***reset_controllers,
47                       bool *kill_processes,
48                       char ***kill_only_users,
49                       char ***kill_exclude_users,
50                       bool *debug) {
51
52         unsigned i;
53
54         assert(argc >= 0);
55         assert(argc == 0 || argv);
56
57         for (i = 0; i < (unsigned) argc; i++) {
58                 int k;
59
60                 if (startswith(argv[i], "kill-session-processes=")) {
61                         if ((k = parse_boolean(argv[i] + 23)) < 0) {
62                                 pam_syslog(handle, LOG_ERR, "Failed to parse kill-session-processes= argument.");
63                                 return k;
64                         }
65
66                         if (kill_processes)
67                                 *kill_processes = k;
68
69                 } else if (startswith(argv[i], "kill-session=")) {
70                         /* As compatibility for old versions */
71
72                         if ((k = parse_boolean(argv[i] + 13)) < 0) {
73                                 pam_syslog(handle, LOG_ERR, "Failed to parse kill-session= argument.");
74                                 return k;
75                         }
76
77                         if (kill_processes)
78                                 *kill_processes = k;
79
80                 } else if (startswith(argv[i], "controllers=")) {
81
82                         if (controllers) {
83                                 char **l;
84
85                                 if (!(l = strv_split(argv[i] + 12, ","))) {
86                                         pam_syslog(handle, LOG_ERR, "Out of memory.");
87                                         return -ENOMEM;
88                                 }
89
90                                 strv_free(*controllers);
91                                 *controllers = l;
92                         }
93
94                 } else if (startswith(argv[i], "reset-controllers=")) {
95
96                         if (reset_controllers) {
97                                 char **l;
98
99                                 if (!(l = strv_split(argv[i] + 18, ","))) {
100                                         pam_syslog(handle, LOG_ERR, "Out of memory.");
101                                         return -ENOMEM;
102                                 }
103
104                                 strv_free(*reset_controllers);
105                                 *reset_controllers = l;
106                         }
107
108                 } else if (startswith(argv[i], "kill-only-users=")) {
109
110                         if (kill_only_users) {
111                                 char **l;
112
113                                 if (!(l = strv_split(argv[i] + 16, ","))) {
114                                         pam_syslog(handle, LOG_ERR, "Out of memory.");
115                                         return -ENOMEM;
116                                 }
117
118                                 strv_free(*kill_only_users);
119                                 *kill_only_users = l;
120                         }
121
122                 } else if (startswith(argv[i], "kill-exclude-users=")) {
123
124                         if (kill_exclude_users) {
125                                 char **l;
126
127                                 if (!(l = strv_split(argv[i] + 19, ","))) {
128                                         pam_syslog(handle, LOG_ERR, "Out of memory.");
129                                         return -ENOMEM;
130                                 }
131
132                                 strv_free(*kill_exclude_users);
133                                 *kill_exclude_users = l;
134                         }
135
136                 } else if (startswith(argv[i], "debug=")) {
137                         if ((k = parse_boolean(argv[i] + 6)) < 0) {
138                                 pam_syslog(handle, LOG_ERR, "Failed to parse debug= argument.");
139                                 return k;
140                         }
141
142                         if (debug)
143                                 *debug = k;
144
145                 } else if (startswith(argv[i], "create-session=") ||
146                            startswith(argv[i], "kill-user=")) {
147
148                         pam_syslog(handle, LOG_WARNING, "Option %s not supported anymore, ignoring.", argv[i]);
149
150                 } else {
151                         pam_syslog(handle, LOG_ERR, "Unknown parameter '%s'.", argv[i]);
152                         return -EINVAL;
153                 }
154         }
155
156         return 0;
157 }
158
159 static int get_user_data(
160                 pam_handle_t *handle,
161                 const char **ret_username,
162                 struct passwd **ret_pw) {
163
164         const char *username = NULL;
165         struct passwd *pw = NULL;
166         int r;
167         bool have_loginuid = false;
168         char *s;
169
170         assert(handle);
171         assert(ret_username);
172         assert(ret_pw);
173
174         if (have_effective_cap(CAP_AUDIT_CONTROL) > 0) {
175                 /* Only use audit login uid if we are executed with
176                  * sufficient capabilities so that pam_loginuid could
177                  * do its job. If we are lacking the CAP_AUDIT_CONTROL
178                  * capabality we most likely are being run in a
179                  * container and /proc/self/loginuid is useless since
180                  * it probably contains a uid of the host system. */
181
182                 if (read_one_line_file("/proc/self/loginuid", &s) >= 0) {
183                         uid_t uid;
184
185                         r = parse_uid(s, &uid);
186                         free(s);
187
188                         if (r >= 0 && uid != (uint32_t) -1) {
189                                 have_loginuid = true;
190                                 pw = pam_modutil_getpwuid(handle, uid);
191                         }
192                 }
193         }
194
195         if (!have_loginuid) {
196                 if ((r = pam_get_user(handle, &username, NULL)) != PAM_SUCCESS) {
197                         pam_syslog(handle, LOG_ERR, "Failed to get user name.");
198                         return r;
199                 }
200
201                 if (!username || !*username) {
202                         pam_syslog(handle, LOG_ERR, "User name not valid.");
203                         return PAM_AUTH_ERR;
204                 }
205
206                 pw = pam_modutil_getpwnam(handle, username);
207         }
208
209         if (!pw) {
210                 pam_syslog(handle, LOG_ERR, "Failed to get user data.");
211                 return PAM_USER_UNKNOWN;
212         }
213
214         *ret_pw = pw;
215         *ret_username = username ? username : pw->pw_name;
216
217         return PAM_SUCCESS;
218 }
219
220 static bool check_user_lists(
221                 pam_handle_t *handle,
222                 uid_t uid,
223                 char **kill_only_users,
224                 char **kill_exclude_users) {
225
226         const char *name = NULL;
227         char **l;
228
229         assert(handle);
230
231         if (uid == 0)
232                 name = "root"; /* Avoid obvious NSS requests, to suppress network traffic */
233         else {
234                 struct passwd *pw;
235
236                 pw = pam_modutil_getpwuid(handle, uid);
237                 if (pw)
238                         name = pw->pw_name;
239         }
240
241         STRV_FOREACH(l, kill_exclude_users) {
242                 uid_t u;
243
244                 if (parse_uid(*l, &u) >= 0)
245                         if (u == uid)
246                                 return false;
247
248                 if (name && streq(name, *l))
249                         return false;
250         }
251
252         if (strv_isempty(kill_only_users))
253                 return true;
254
255         STRV_FOREACH(l, kill_only_users) {
256                 uid_t u;
257
258                 if (parse_uid(*l, &u) >= 0)
259                         if (u == uid)
260                                 return true;
261
262                 if (name && streq(name, *l))
263                         return true;
264         }
265
266         return false;
267 }
268
269 static int get_seat_from_display(const char *display, const char **seat, uint32_t *vtnr) {
270         char *p = NULL;
271         int r;
272         int fd;
273         union sockaddr_union sa;
274         struct ucred ucred;
275         socklen_t l;
276         char *tty;
277         int v;
278
279         assert(display);
280         assert(seat);
281         assert(vtnr);
282
283         /* We deduce the X11 socket from the display name, then use
284          * SO_PEERCRED to determine the X11 server process, ask for
285          * the controlling tty of that and if it's a VC then we know
286          * the seat and the virtual terminal. Sounds ugly, is only
287          * semi-ugly. */
288
289         r = socket_from_display(display, &p);
290         if (r < 0)
291                 return r;
292
293         fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
294         if (fd < 0) {
295                 free(p);
296                 return -errno;
297         }
298
299         zero(sa);
300         sa.un.sun_family = AF_UNIX;
301         strncpy(sa.un.sun_path, p, sizeof(sa.un.sun_path)-1);
302         free(p);
303
304         if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
305                 close_nointr_nofail(fd);
306                 return -errno;
307         }
308
309         l = sizeof(ucred);
310         r = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l);
311         close_nointr_nofail(fd);
312
313         if (r < 0)
314                 return -errno;
315
316         r = get_ctty(ucred.pid, NULL, &tty);
317         if (r < 0)
318                 return r;
319
320         v = vtnr_from_tty(tty);
321         free(tty);
322
323         if (v < 0)
324                 return v;
325         else if (v == 0)
326                 return -ENOENT;
327
328         *seat = "seat0";
329         *vtnr = (uint32_t) v;
330
331         return 0;
332 }
333
334 _public_ PAM_EXTERN int pam_sm_open_session(
335                 pam_handle_t *handle,
336                 int flags,
337                 int argc, const char **argv) {
338
339         struct passwd *pw;
340         bool kill_processes = false, debug = false;
341         const char *username, *id, *object_path, *runtime_path, *service = NULL, *tty = NULL, *display = NULL, *remote_user = NULL, *remote_host = NULL, *seat = NULL, *type, *cvtnr = NULL;
342         char **controllers = NULL, **reset_controllers = NULL, **kill_only_users = NULL, **kill_exclude_users = NULL;
343         DBusError error;
344         uint32_t uid, pid;
345         DBusMessageIter iter;
346         dbus_bool_t kp;
347         int session_fd = -1;
348         DBusConnection *bus = NULL;
349         DBusMessage *m = NULL, *reply = NULL;
350         dbus_bool_t remote;
351         int r;
352         uint32_t vtnr = 0;
353
354         assert(handle);
355
356         dbus_error_init(&error);
357
358         /* pam_syslog(handle, LOG_INFO, "pam-systemd initializing"); */
359
360         /* Make this a NOP on non-systemd systems */
361         if (sd_booted() <= 0)
362                 return PAM_SUCCESS;
363
364         if (parse_argv(handle,
365                        argc, argv,
366                        &controllers, &reset_controllers,
367                        &kill_processes, &kill_only_users, &kill_exclude_users,
368                        &debug) < 0) {
369                 r = PAM_SESSION_ERR;
370                 goto finish;
371         }
372
373         r = get_user_data(handle, &username, &pw);
374         if (r != PAM_SUCCESS)
375                 goto finish;
376
377         /* Make sure we don't enter a loop by talking to
378          * systemd-logind when it is actually waiting for the
379          * background to finish start-up. If the service is
380          * "systemd-shared" we simply set XDG_RUNTIME_DIR and
381          * leave. */
382
383         pam_get_item(handle, PAM_SERVICE, (const void**) &service);
384         if (streq_ptr(service, "systemd-shared")) {
385                 char *p, *rt = NULL;
386
387                 if (asprintf(&p, "/run/systemd/users/%lu", (unsigned long) pw->pw_uid) < 0) {
388                         r = PAM_BUF_ERR;
389                         goto finish;
390                 }
391
392                 r = parse_env_file(p, NEWLINE,
393                                    "RUNTIME", &rt,
394                                    NULL);
395                 free(p);
396
397                 if (r < 0 && r != -ENOENT) {
398                         r = PAM_SESSION_ERR;
399                         free(rt);
400                         goto finish;
401                 }
402
403                 if (rt)  {
404                         r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", rt, 0);
405                         free(rt);
406
407                         if (r != PAM_SUCCESS) {
408                                 pam_syslog(handle, LOG_ERR, "Failed to set runtime dir.");
409                                 goto finish;
410                         }
411                 }
412
413                 r = PAM_SUCCESS;
414                 goto finish;
415         }
416
417         if (kill_processes)
418                 kill_processes = check_user_lists(handle, pw->pw_uid, kill_only_users, kill_exclude_users);
419
420         dbus_connection_set_change_sigpipe(FALSE);
421
422         bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
423         if (!bus) {
424                 pam_syslog(handle, LOG_ERR, "Failed to connect to system bus: %s", bus_error_message(&error));
425                 r = PAM_SESSION_ERR;
426                 goto finish;
427         }
428
429         m = dbus_message_new_method_call(
430                         "org.freedesktop.login1",
431                         "/org/freedesktop/login1",
432                         "org.freedesktop.login1.Manager",
433                         "CreateSession");
434
435         if (!m) {
436                 pam_syslog(handle, LOG_ERR, "Could not allocate create session message.");
437                 r = PAM_BUF_ERR;
438                 goto finish;
439         }
440
441         uid = pw->pw_uid;
442         pid = getpid();
443
444         pam_get_item(handle, PAM_XDISPLAY, (const void**) &display);
445         pam_get_item(handle, PAM_TTY, (const void**) &tty);
446         pam_get_item(handle, PAM_RUSER, (const void**) &remote_user);
447         pam_get_item(handle, PAM_RHOST, (const void**) &remote_host);
448         seat = pam_getenv(handle, "XDG_SEAT");
449         cvtnr = pam_getenv(handle, "XDG_VTNR");
450
451         service = strempty(service);
452         tty = strempty(tty);
453         display = strempty(display);
454         remote_user = strempty(remote_user);
455         remote_host = strempty(remote_host);
456         seat = strempty(seat);
457
458         if (strchr(tty, ':')) {
459                 /* A tty with a colon is usually an X11 display, place
460                  * there to show up in utmp. We rearrange things and
461                  * don't pretend that an X display was a tty */
462
463                 if (isempty(display))
464                         display = tty;
465                 tty = "";
466         }
467
468         if (!isempty(cvtnr))
469                 safe_atou32(cvtnr, &vtnr);
470
471         if (!isempty(display) && isempty(seat) && vtnr <= 0)
472                 get_seat_from_display(display, &seat, &vtnr);
473
474         type = !isempty(display) ? "x11" :
475                    !isempty(tty) ? "tty" : "other";
476
477         remote = !isempty(remote_host) && !streq(remote_host, "localhost") && !streq(remote_host, "localhost.localdomain");
478
479         if (!dbus_message_append_args(m,
480                                       DBUS_TYPE_UINT32, &uid,
481                                       DBUS_TYPE_UINT32, &pid,
482                                       DBUS_TYPE_STRING, &service,
483                                       DBUS_TYPE_STRING, &type,
484                                       DBUS_TYPE_STRING, &seat,
485                                       DBUS_TYPE_UINT32, &vtnr,
486                                       DBUS_TYPE_STRING, &tty,
487                                       DBUS_TYPE_STRING, &display,
488                                       DBUS_TYPE_BOOLEAN, &remote,
489                                       DBUS_TYPE_STRING, &remote_user,
490                                       DBUS_TYPE_STRING, &remote_host,
491                                       DBUS_TYPE_INVALID)) {
492                 pam_syslog(handle, LOG_ERR, "Could not attach parameters to message.");
493                 r = PAM_BUF_ERR;
494                 goto finish;
495         }
496
497         dbus_message_iter_init_append(m, &iter);
498
499         r = bus_append_strv_iter(&iter, controllers);
500         if (r < 0) {
501                 pam_syslog(handle, LOG_ERR, "Could not attach parameter to message.");
502                 r = PAM_BUF_ERR;
503                 goto finish;
504         }
505
506         r = bus_append_strv_iter(&iter, reset_controllers);
507         if (r < 0) {
508                 pam_syslog(handle, LOG_ERR, "Could not attach parameter to message.");
509                 r = PAM_BUF_ERR;
510                 goto finish;
511         }
512
513         kp = kill_processes;
514         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &kp)) {
515                 pam_syslog(handle, LOG_ERR, "Could not attach parameter to message.");
516                 r = PAM_BUF_ERR;
517                 goto finish;
518         }
519
520         reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
521         if (!reply) {
522                 pam_syslog(handle, LOG_ERR, "Failed to create session: %s", bus_error_message(&error));
523                 r = PAM_SESSION_ERR;
524                 goto finish;
525         }
526
527         if (!dbus_message_get_args(reply, &error,
528                                    DBUS_TYPE_STRING, &id,
529                                    DBUS_TYPE_OBJECT_PATH, &object_path,
530                                    DBUS_TYPE_STRING, &runtime_path,
531                                    DBUS_TYPE_UNIX_FD, &session_fd,
532                                    DBUS_TYPE_STRING, &seat,
533                                    DBUS_TYPE_UINT32, &vtnr,
534                                    DBUS_TYPE_INVALID)) {
535                 pam_syslog(handle, LOG_ERR, "Failed to parse message: %s", bus_error_message(&error));
536                 r = PAM_SESSION_ERR;
537                 goto finish;
538         }
539
540         r = pam_misc_setenv(handle, "XDG_SESSION_ID", id, 0);
541         if (r != PAM_SUCCESS) {
542                 pam_syslog(handle, LOG_ERR, "Failed to set session id.");
543                 goto finish;
544         }
545
546         r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", runtime_path, 0);
547         if (r != PAM_SUCCESS) {
548                 pam_syslog(handle, LOG_ERR, "Failed to set runtime dir.");
549                 goto finish;
550         }
551
552         if (!isempty(seat)) {
553                 r = pam_misc_setenv(handle, "XDG_SEAT", seat, 0);
554                 if (r != PAM_SUCCESS) {
555                         pam_syslog(handle, LOG_ERR, "Failed to set seat.");
556                         goto finish;
557                 }
558         }
559
560         if (vtnr > 0) {
561                 char buf[11];
562                 snprintf(buf, sizeof(buf), "%u", vtnr);
563                 char_array_0(buf);
564
565                 r = pam_misc_setenv(handle, "XDG_VTNR", buf, 0);
566                 if (r != PAM_SUCCESS) {
567                         pam_syslog(handle, LOG_ERR, "Failed to set virtual terminal number.");
568                         goto finish;
569                 }
570         }
571
572         if (session_fd >= 0) {
573                 r = pam_set_data(handle, "systemd.session-fd", INT_TO_PTR(session_fd+1), NULL);
574                 if (r != PAM_SUCCESS) {
575                         pam_syslog(handle, LOG_ERR, "Failed to install session fd.");
576                         return r;
577                 }
578         }
579
580         session_fd = -1;
581
582         r = PAM_SUCCESS;
583
584 finish:
585         strv_free(controllers);
586         strv_free(reset_controllers);
587         strv_free(kill_only_users);
588         strv_free(kill_exclude_users);
589
590         dbus_error_free(&error);
591
592         if (bus) {
593                 dbus_connection_close(bus);
594                 dbus_connection_unref(bus);
595         }
596
597         if (m)
598                 dbus_message_unref(m);
599
600         if (reply)
601                 dbus_message_unref(reply);
602
603         if (session_fd >= 0)
604                 close_nointr_nofail(session_fd);
605
606         return r;
607 }
608
609 _public_ PAM_EXTERN int pam_sm_close_session(
610                 pam_handle_t *handle,
611                 int flags,
612                 int argc, const char **argv) {
613
614         const void *p = NULL;
615
616         pam_get_data(handle, "systemd.session-fd", &p);
617
618         if (p)
619                 close_nointr(PTR_TO_INT(p) - 1);
620
621         return PAM_SUCCESS;
622 }