From bc3b6846356532d0bcab88d97b0b84def32be7b1 Mon Sep 17 00:00:00 2001 From: Vincent Jardin Date: Sun, 1 Dec 2024 17:01:30 +0100 Subject: [PATCH] hostname: add conf hostname= If the configuration provides a hostname, let's use it instead of the local hostname. --- src/pam_radius_auth.c | 21 +++++++++++++++++++-- src/pam_radius_auth.h | 2 ++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/pam_radius_auth.c b/src/pam_radius_auth.c index 443c526..649020b 100644 --- a/src/pam_radius_auth.c +++ b/src/pam_radius_auth.c @@ -163,6 +163,19 @@ static int _pam_parse(int argc, CONST char **argv, radius_conf_t *conf) ctrl |= PAM_DEBUG_ARG; conf->debug = TRUE; + } else if (!strncmp(*argv, "hostname=", 9)) { + if (!strncmp(conf->hostname, (char*)*argv+9, MAXHOSTNAME)) { + _pam_log(LOG_WARNING, "ignoring duplicate '%s'", *argv); + } else { + /* truncate excessive hostnames to (MAXHOSTNAME -1) length */ + if (strlen((char*)*argv+9) >= (MAXHOSTNAME - 1)) { + *((char*)*argv+9 + (MAXHOSTNAME - 1)) = 0; + } + /* set the new hostname */ + memset(conf->hostname, 0, sizeof(conf->hostname)); + snprintf(conf->hostname, MAXHOSTNAME, "%s", (char*)*argv+9); + } + } else if (!strncmp(arg, "prompt=", 7)) { if (!strncmp(conf->prompt, (arg+7), MAXPROMPT)) { _pam_log(LOG_WARNING, "ignoring duplicate '%s'", arg); @@ -957,10 +970,14 @@ static int initialize(radius_conf_t *conf, int accounting) */ static void build_radius_packet(AUTH_HDR *request, CONST char *user, CONST char *password, radius_conf_t *conf) { - char hostname[256]; + char hostname[MAXHOSTNAME]; hostname[0] = '\0'; - gethostname(hostname, sizeof(hostname) - 1); + if (conf->hostname[0] != '\0') { + strcpy(hostname, conf->hostname); + } else { + gethostname(hostname, sizeof(hostname) - 1); + } /* * For Access-Request, create a random authentication diff --git a/src/pam_radius_auth.h b/src/pam_radius_auth.h index acecb4d..6fa9b6a 100644 --- a/src/pam_radius_auth.h +++ b/src/pam_radius_auth.h @@ -58,6 +58,7 @@ #define MAXPROMPT 33 /* max prompt length, including '\0' */ #define DEFAULT_PROMPT "Password" /* default prompt, without the ': ' */ +#define MAXHOSTNAME 65 /************************************************************************* * Platform specific defines @@ -190,6 +191,7 @@ typedef struct radius_conf_t { int privilege_level; int require_message_authenticator; uint8_t *message_authenticator; + char hostname[MAXHOSTNAME]; } radius_conf_t; #endif /* PAM_RADIUS_H */