|
| 1 | +#include <security/pam_modules.h> |
| 2 | +#include <security/pam_ext.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <string.h> |
| 5 | +#include <stdio.h> |
| 6 | +#include <syslog.h> |
| 7 | + |
| 8 | +/* |
| 9 | + * Helper function to check if a specific argument is provided in pam.conf |
| 10 | + */ |
| 11 | +int has_argument(int argc, const char **argv, const char *arg) { |
| 12 | + for (int i = 0; i < argc; i++) { |
| 13 | + if (strcmp(argv[i], arg) == 0) { |
| 14 | + return 1; // Argument found |
| 15 | + } |
| 16 | + } |
| 17 | + return 0; // Argument not found |
| 18 | +} |
| 19 | + |
| 20 | +/* |
| 21 | + * PAM authentication function. |
| 22 | +*/ |
| 23 | +PAM_EXTERN int pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv) { |
| 24 | + const char *authtok; |
| 25 | + int pam_ret; |
| 26 | + |
| 27 | + // Check if the "query_missing_token" argument is provided |
| 28 | + int query_missing_token = has_argument(argc, argv, "query_missing_token"); |
| 29 | + |
| 30 | + // Retrieve the current PAM_AUTHTOK (the user-supplied password + token) |
| 31 | + pam_ret = pam_get_item(pamh, PAM_AUTHTOK, (const void **)&authtok); |
| 32 | + if (pam_ret != PAM_SUCCESS || authtok == NULL || strlen(authtok) == 0) { |
| 33 | + // Only prompt if query_missing_token argument is provided |
| 34 | + if (query_missing_token) { |
| 35 | + // Allocate a buffer for the input |
| 36 | + char *input = NULL; |
| 37 | + |
| 38 | + // Prompt the user for the password+token |
| 39 | + pam_ret = pam_prompt(pamh, PAM_PROMPT_ECHO_OFF, &input, "Password+Token: "); |
| 40 | + if (pam_ret != PAM_SUCCESS || input == NULL) { |
| 41 | + pam_syslog(pamh, LOG_ERR, "Failed to prompt for Password+Token."); |
| 42 | + return PAM_AUTH_ERR; |
| 43 | + } |
| 44 | + |
| 45 | + authtok = input; // Use this as the authtok |
| 46 | + } else { |
| 47 | + pam_syslog(pamh, LOG_ERR, "PAM_AUTHTOK is empty and query_missing_token is not set."); |
| 48 | + return PAM_AUTH_ERR; // Fail if no authtok and no query_missing_token argument |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // Ensure the authtok is not unreasonably long |
| 53 | + if (strlen(authtok) > PAM_MAX_RESP_SIZE) { |
| 54 | + pam_syslog(pamh, LOG_ERR, "PAM_AUTHTOK is too long."); |
| 55 | + return PAM_AUTH_ERR; |
| 56 | + } |
| 57 | + |
| 58 | + // Find the position of the last '+' character |
| 59 | + char *plus_pos = strrchr(authtok, '+'); |
| 60 | + if (plus_pos == NULL) { |
| 61 | + pam_syslog(pamh, LOG_ERR, "No '+' found in PAM_AUTHTOK."); |
| 62 | + return PAM_AUTH_ERR; |
| 63 | + } |
| 64 | + |
| 65 | + // Split the authtok into password and token |
| 66 | + size_t password_len = plus_pos - authtok; |
| 67 | + char *password = strndup(authtok, password_len); // Copy only the password part |
| 68 | + if (password == NULL) { |
| 69 | + pam_syslog(pamh, LOG_ERR, "Failed to allocate memory for password."); |
| 70 | + return PAM_BUF_ERR; |
| 71 | + } |
| 72 | + |
| 73 | + // Ensure the token is not empty |
| 74 | + if (*(plus_pos + 1) == '\0') { |
| 75 | + pam_syslog(pamh, LOG_ERR, "No token found after the last '+'."); |
| 76 | + free(password); |
| 77 | + return PAM_AUTH_ERR; |
| 78 | + } |
| 79 | + |
| 80 | + const char *token = plus_pos + 1; // Token part starts after '+' |
| 81 | + |
| 82 | + // Set the modified password (without the token) back into PAM_AUTHTOK |
| 83 | + pam_ret = pam_set_item(pamh, PAM_AUTHTOK, password); |
| 84 | + if (pam_ret != PAM_SUCCESS) { |
| 85 | + pam_syslog(pamh, LOG_ERR, "Failed to set PAM_AUTHTOK."); |
| 86 | + free(password); |
| 87 | + return PAM_AUTH_ERR; |
| 88 | + } |
| 89 | + |
| 90 | + // Allocate memory for the environment variable to hold the token |
| 91 | + size_t token_env_size = strlen("PAM_SPLIT_TOKEN=") + strlen(token) + 1; // +1 for null terminator |
| 92 | + char *token_env = malloc(token_env_size); |
| 93 | + if (!token_env) { |
| 94 | + pam_syslog(pamh, LOG_ERR, "Failed to allocate memory for token environment variable."); |
| 95 | + free(password); |
| 96 | + return PAM_BUF_ERR; |
| 97 | + } |
| 98 | + |
| 99 | + snprintf(token_env, token_env_size, "PAM_SPLIT_TOKEN=%s", token); |
| 100 | + |
| 101 | + // Set PAM_SPLIT_TOKEN environment variable to the token |
| 102 | + pam_ret = pam_putenv(pamh, token_env); |
| 103 | + if (pam_ret != PAM_SUCCESS) { |
| 104 | + pam_syslog(pamh, LOG_ERR, "Failed to set PAM_SPLIT_TOKEN environment variable."); |
| 105 | + free(password); |
| 106 | + free(token_env); |
| 107 | + return PAM_AUTH_ERR; |
| 108 | + } |
| 109 | + |
| 110 | + // Log success |
| 111 | + pam_syslog(pamh, LOG_INFO, "PAM_AUTHTOK and PAM_SPLIT_TOKEN set successfully."); |
| 112 | + |
| 113 | + // Clean up |
| 114 | + free(password); |
| 115 | + free(token_env); |
| 116 | + |
| 117 | + return PAM_SUCCESS; |
| 118 | +} |
| 119 | + |
| 120 | +PAM_EXTERN int pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv) { |
| 121 | + return PAM_SUCCESS; |
| 122 | +} |
0 commit comments