#include "strv.h"
#include "dbus-common.h"
#include "def.h"
+#include "socket-util.h"
static int parse_argv(pam_handle_t *handle,
int argc, const char **argv,
bool *debug) {
unsigned i;
- bool reset_controller_set = false;
- bool kill_exclude_users_set = false;
assert(argc >= 0);
assert(argc == 0 || argv);
for (i = 0; i < (unsigned) argc; i++) {
int k;
- if (startswith(argv[i], "kill-processes=")) {
- if ((k = parse_boolean(argv[i] + 15)) < 0) {
- pam_syslog(handle, LOG_ERR, "Failed to parse kill-processes= argument.");
+ if (startswith(argv[i], "kill-session-processes=")) {
+ if ((k = parse_boolean(argv[i] + 23)) < 0) {
+ pam_syslog(handle, LOG_ERR, "Failed to parse kill-session-processes= argument.");
return k;
}
*reset_controllers = l;
}
- reset_controller_set = true;
-
} else if (startswith(argv[i], "kill-only-users=")) {
if (kill_only_users) {
*kill_exclude_users = l;
}
- kill_exclude_users_set = true;
-
} else if (startswith(argv[i], "debug=")) {
if ((k = parse_boolean(argv[i] + 6)) < 0) {
pam_syslog(handle, LOG_ERR, "Failed to parse debug= argument.");
}
}
- if (!reset_controller_set && reset_controllers) {
- char **l;
-
- if (!(l = strv_new("cpu", NULL))) {
- pam_syslog(handle, LOG_ERR, "Out of memory");
- return -ENOMEM;
- }
-
- *reset_controllers = l;
- }
-
- if (controllers)
- strv_remove(*controllers, SYSTEMD_CGROUP_CONTROLLER);
-
- if (reset_controllers)
- strv_remove(*reset_controllers, SYSTEMD_CGROUP_CONTROLLER);
-
- if (!kill_exclude_users_set && kill_exclude_users) {
- char **l;
-
- if (!(l = strv_new("root", NULL))) {
- pam_syslog(handle, LOG_ERR, "Out of memory");
- return -ENOMEM;
- }
-
- *kill_exclude_users = l;
- }
-
return 0;
}
return false;
}
+static int get_seat_from_display(const char *display, const char **seat, uint32_t *vtnr) {
+ char *p = NULL;
+ int r;
+ int fd;
+ union sockaddr_union sa;
+ struct ucred ucred;
+ socklen_t l;
+ char *tty;
+ int v;
+
+ assert(display);
+ assert(seat);
+ assert(vtnr);
+
+ /* We deduce the X11 socket from the display name, then use
+ * SO_PEERCRED to determine the X11 server process, ask for
+ * the controlling tty of that and if it's a VC then we know
+ * the seat and the virtual terminal. Sounds ugly, is only
+ * semi-ugly. */
+
+ r = socket_from_display(display, &p);
+ if (r < 0)
+ return r;
+
+ fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
+ if (fd < 0) {
+ free(p);
+ return -errno;
+ }
+
+ zero(sa);
+ sa.un.sun_family = AF_UNIX;
+ strncpy(sa.un.sun_path, p, sizeof(sa.un.sun_path)-1);
+ free(p);
+
+ if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
+ close_nointr_nofail(fd);
+ return -errno;
+ }
+
+ l = sizeof(ucred);
+ r = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l);
+ close_nointr_nofail(fd);
+
+ if (r < 0)
+ return -errno;
+
+ r = get_ctty(ucred.pid, NULL, &tty);
+ if (r < 0)
+ return r;
+
+ v = vtnr_from_tty(tty);
+ free(tty);
+
+ if (v < 0)
+ return v;
+ else if (v == 0)
+ return -ENOENT;
+
+ *seat = "seat0";
+ *vtnr = (uint32_t) v;
+
+ return 0;
+}
+
_public_ PAM_EXTERN int pam_sm_open_session(
pam_handle_t *handle,
int flags,
struct passwd *pw;
bool kill_processes = false, debug = false;
- const char *username, *id, *object_path, *runtime_path, *service = NULL, *tty = NULL, *display = NULL, *remote_user = NULL, *remote_host = NULL, *seat = NULL, *type;
+ 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;
char **controllers = NULL, **reset_controllers = NULL, **kill_only_users = NULL, **kill_exclude_users = NULL;
DBusError error;
uint32_t uid, pid;
DBusMessage *m = NULL, *reply = NULL;
dbus_bool_t remote;
int r;
+ uint32_t vtnr = 0;
assert(handle);
if (sd_booted() <= 0)
return PAM_SUCCESS;
+ /* Make sure we don't enter a loop by talking to
+ * systemd-logind when it is actually waiting for the
+ * background to finish start-up, */
+ pam_get_item(handle, PAM_SERVICE, (const void**) &service);
+ if (streq_ptr(service, "systemd-shared"))
+ return PAM_SUCCESS;
+
if (parse_argv(handle,
argc, argv,
&controllers, &reset_controllers,
uid = pw->pw_uid;
pid = getpid();
- pam_get_item(handle, PAM_SERVICE, (const void**) &service);
pam_get_item(handle, PAM_XDISPLAY, (const void**) &display);
pam_get_item(handle, PAM_TTY, (const void**) &tty);
pam_get_item(handle, PAM_RUSER, (const void**) &remote_user);
pam_get_item(handle, PAM_RHOST, (const void**) &remote_host);
- seat = pam_getenv(handle, "XDG_SEAT");
+ seat = pam_getenv(handle, "LOGIN_SEAT");
+ cvtnr = pam_getenv(handle, "LOGIN_VTNR");
service = strempty(service);
tty = strempty(tty);
tty = "";
}
+ if (!isempty(cvtnr))
+ safe_atou32(cvtnr, &vtnr);
+
+ if (!isempty(display) && isempty(seat) && vtnr <= 0)
+ get_seat_from_display(display, &seat, &vtnr);
+
type = !isempty(display) ? "x11" :
!isempty(tty) ? "tty" : "other";
DBUS_TYPE_STRING, &service,
DBUS_TYPE_STRING, &type,
DBUS_TYPE_STRING, &seat,
+ DBUS_TYPE_UINT32, &vtnr,
DBUS_TYPE_STRING, &tty,
DBUS_TYPE_STRING, &display,
DBUS_TYPE_BOOLEAN, &remote,