chiark / gitweb /
pam-module: add debug= parameter
[elogind.git] / src / pam-module.c
index b4130bbdd6e3e814ff9f6504cfb54073bfbb6206..bdf61334e646e9916307d28f20b9122eb13ab9f8 100644 (file)
@@ -46,7 +46,8 @@ static int parse_argv(pam_handle_t *handle,
                       char ***controllers,
                       char ***reset_controllers,
                       char ***kill_only_users,
-                      char ***kill_exclude_users) {
+                      char ***kill_exclude_users,
+                      bool *debug) {
 
         unsigned i;
         bool reset_controller_set = false;
@@ -145,6 +146,15 @@ static int parse_argv(pam_handle_t *handle,
 
                         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.");
+                                return k;
+                        }
+
+                        if (debug)
+                                *debug = k;
+
                 } else {
                         pam_syslog(handle, LOG_ERR, "Unknown parameter '%s'.", argv[i]);
                         return -EINVAL;
@@ -221,18 +231,19 @@ static uint64_t get_session_id(int *mode) {
 
         /* First attempt: let's use the session ID of the audit
          * system, if it is available. */
-        if (read_one_line_file("/proc/self/sessionid", &s) >= 0) {
-                uint32_t u;
-                int r;
+        if (have_effective_cap(CAP_AUDIT_CONTROL) > 0)
+                if (read_one_line_file("/proc/self/sessionid", &s) >= 0) {
+                        uint32_t u;
+                        int r;
 
-                r = safe_atou32(s, &u);
-                free(s);
+                        r = safe_atou32(s, &u);
+                        free(s);
 
-                if (r >= 0 && u != (uint32_t) -1 && u > 0) {
-                        *mode = SESSION_ID_AUDIT;
-                        return (uint64_t) u;
+                        if (r >= 0 && u != (uint32_t) -1 && u > 0) {
+                                *mode = SESSION_ID_AUDIT;
+                                return (uint64_t) u;
+                        }
                 }
-        }
 
         /* Second attempt, use our own counter. */
         if ((fd = open_file_and_lock(RUNTIME_DIR "/user/.pam-systemd-session")) >= 0) {
@@ -289,7 +300,7 @@ static int get_user_data(
         assert(ret_username);
         assert(ret_pw);
 
-        if (have_effective_cap(CAP_AUDIT_CONTROL)) {
+        if (have_effective_cap(CAP_AUDIT_CONTROL) > 0) {
                 /* Only use audit login uid if we are executed with
                  * sufficient capabilities so that pam_loginuid could
                  * do its job. If we are lacking the CAP_AUDIT_CONTROL
@@ -405,6 +416,7 @@ _public_ PAM_EXTERN int pam_sm_open_session(
         char *buf = NULL;
         int lock_fd = -1;
         bool create_session = true;
+        bool debug = false;
         char **controllers = NULL, **reset_controllers = NULL, **c;
         char *cgroup_user_tree = NULL;
 
@@ -420,7 +432,7 @@ _public_ PAM_EXTERN int pam_sm_open_session(
                        argc, argv,
                        &create_session, NULL, NULL,
                        &controllers, &reset_controllers,
-                       NULL, NULL) < 0)
+                       NULL, NULL, &debug) < 0)
                 return PAM_SESSION_ERR;
 
         if ((r = get_user_data(handle, &username, &pw)) != PAM_SUCCESS)
@@ -504,7 +516,8 @@ _public_ PAM_EXTERN int pam_sm_open_session(
                 goto finish;
         }
 
-        pam_syslog(handle, LOG_INFO, "Moving new user session for %s into control group %s.", username, buf);
+        if (debug)
+                pam_syslog(handle, LOG_DEBUG, "Moving new user session for %s into control group %s.", username, buf);
 
         if ((r = create_user_group(handle, SYSTEMD_CGROUP_CONTROLLER, buf, pw, true, true)) != PAM_SUCCESS)
                 goto finish;
@@ -615,6 +628,7 @@ _public_ PAM_EXTERN int pam_sm_close_session(
         const char *username = NULL;
         bool kill_session = false;
         bool kill_user = false;
+        bool debug = false;
         int lock_fd = -1, r;
         char *session_path = NULL, *nosession_path = NULL, *user_path = NULL;
         const char *id;
@@ -633,7 +647,7 @@ _public_ PAM_EXTERN int pam_sm_close_session(
                        argc, argv,
                        NULL, &kill_session, &kill_user,
                        &controllers, NULL,
-                       &kill_only_users, &kill_exclude_users) < 0)
+                       &kill_only_users, &kill_exclude_users, &debug) < 0)
                 return PAM_SESSION_ERR;
 
         if ((r = get_user_data(handle, &username, &pw)) != PAM_SUCCESS)
@@ -675,13 +689,15 @@ _public_ PAM_EXTERN int pam_sm_close_session(
                 }
 
                 if (kill_session && check_user_lists(handle, pw->pw_uid, kill_only_users, kill_exclude_users))  {
-                        pam_syslog(handle, LOG_INFO, "Killing remaining processes of user session %s of %s.", id, username);
+                        if (debug)
+                                pam_syslog(handle, LOG_DEBUG, "Killing remaining processes of user session %s of %s.", id, username);
 
                         /* Kill processes in session cgroup, and delete it */
                         if ((r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, session_path, true)) < 0)
                                 pam_syslog(handle, LOG_ERR, "Failed to kill session cgroup: %s", strerror(-r));
                 } else {
-                        pam_syslog(handle, LOG_INFO, "Moving remaining processes of user session %s of %s into control group %s.", id, username, nosession_path);
+                        if (debug)
+                                pam_syslog(handle, LOG_DEBUG, "Moving remaining processes of user session %s of %s into control group %s.", id, username, nosession_path);
 
                         /* Migrate processes from session to user
                          * cgroup. First, try to create the user group