Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix code bug in parse config file. #4

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 38 additions & 14 deletions support.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
#include <string.h>
#include <ctype.h> /* isspace() */

/* tacacs config file splitter */
#define CONFIG_FILE_SPLITTER " ,\t\n\r\f"

/* tacacs server information */
tacplus_server_t tac_srv[TAC_PLUS_MAXSERVERS];
struct addrinfo tac_srv_addr[TAC_PLUS_MAXSERVERS];
Expand Down Expand Up @@ -235,6 +238,26 @@ void set_source_ip(const char *tac_source_ip) {
freeaddrinfo(source_address);
_pam_log(LOG_DEBUG, "source ip is set");
}
}

/*
* Reset configuration variables.
* This method need to be called before parse config, otherwise the server list will grow with each call.
*/
int reset_config_variables () {
memset(tac_srv, 0, sizeof(tacplus_server_t) * TAC_PLUS_MAXSERVERS);
tac_srv_no = 0;

tac_service[0] = 0;
tac_protocol[0] = 0;
tac_prompt[0] = 0;
tac_login[0] = 0;
tac_source_ip[0] = 0;

if (tac_source_addr != NULL) {
/* reset source address */
tac_source_addr = NULL;
}
}

/*
Expand Down Expand Up @@ -363,24 +386,25 @@ int parse_config_file(const char *file) {
char line_buffer[256];
int ctrl = 0;

/* otherwise the list will grow with each call */
reset_config_variables();

config_file = fopen(file, "r");
if(config_file == NULL) {
_pam_log(LOG_ERR, "Failed to open config file %s: %m", file);
return 0;
}

if (tac_source_addr != NULL) {
/* reset source address */
tac_source_addr = NULL;
}

char current_secret[256];

char current_secret[256];
memset(current_secret, 0, sizeof(current_secret));
liuh-80 marked this conversation as resolved.
Show resolved Hide resolved
while (fgets(line_buffer, sizeof line_buffer, config_file)) {
if(*line_buffer == '#' || isspace(*line_buffer))
continue; /* skip comments and blank line. */
strtok(line_buffer, " \t\n\r\f");
ctrl |= _pam_parse_arg(line_buffer, current_secret, sizeof(current_secret));
char* config_item = strtok(line_buffer, CONFIG_FILE_SPLITTER);
while (config_item != NULL) {
ctrl |= _pam_parse_arg(config_item, current_secret, sizeof(current_secret));
config_item = strtok(NULL, CONFIG_FILE_SPLITTER);
}
}

fclose(config_file);
Expand All @@ -400,11 +424,11 @@ int _pam_parse (int argc, const char **argv) {
tac_protocol[0] = 0;
tac_prompt[0] = 0;
tac_login[0] = 0;
tac_source_ip[0] = 0;
if (tac_source_addr != NULL) {
/* reset source address */
tac_source_addr = NULL;
tac_source_ip[0] = 0;

if (tac_source_addr != NULL) {
/* reset source address */
tac_source_addr = NULL;
}

for (ctrl = 0; argc-- > 0; ++argv) {
Expand Down