diff options
author | Walter de Jong <walter@heiho.net> | 2013-03-28 23:21:43 +0100 |
---|---|---|
committer | Walter de Jong <walter@heiho.net> | 2013-03-28 23:21:43 +0100 |
commit | ca77c0cfd6f62e0ac7780b5161bb6c4c49065d9b (patch) | |
tree | ac9d1a49fcd7f57e727d90d4f95d31bf82228953 | |
parent | 1119da92c3c47a2cb2a48d494f98ff10a6c32619 (diff) | |
download | pam_tacplus-ca77c0cfd6f62e0ac7780b5161bb6c4c49065d9b.tar.gz pam_tacplus-ca77c0cfd6f62e0ac7780b5161bb6c4c49065d9b.zip |
fixes various memory leaks, really
-rw-r--r-- | libtac/include/libtac.h | 2 | ||||
-rw-r--r-- | libtac/lib/connect.c | 36 | ||||
-rw-r--r-- | pam_tacplus.c | 2 | ||||
-rw-r--r-- | support.c | 2 |
4 files changed, 21 insertions, 21 deletions
diff --git a/libtac/include/libtac.h b/libtac/include/libtac.h index bcc5880..8a7381d 100644 --- a/libtac/include/libtac.h +++ b/libtac/include/libtac.h @@ -127,7 +127,7 @@ extern int tac_timeout; int tac_connect(struct addrinfo **, char **, int); int tac_connect_single(struct addrinfo *, const char *); -char *tac_ntop(const struct sockaddr *, size_t); +char *tac_ntop(const struct sockaddr *); int tac_authen_send(int, const char *, char *, char *, char *); diff --git a/libtac/lib/connect.c b/libtac/lib/connect.c index 1ec4c5f..1226797 100644 --- a/libtac/lib/connect.c +++ b/libtac/lib/connect.c @@ -75,7 +75,7 @@ int tac_connect_single(struct addrinfo *server, const char *key) { struct timeval tv; socklen_t len; struct sockaddr_storage addr; - char *ip = NULL; + char *ip; if(server == NULL) { TACSYSLOG((LOG_ERR, "%s: no TACACS+ server defined", __FUNCTION__)) @@ -83,8 +83,7 @@ int tac_connect_single(struct addrinfo *server, const char *key) { } /* format server address into a string for use in messages */ - /* FIXME this leaks memory, ip is not free()d */ - ip = tac_ntop(server->ai_addr, 0); + ip = tac_ntop(server->ai_addr); if((fd=socket(server->ai_family, server->ai_socktype, server->ai_protocol)) < 0) { TACSYSLOG((LOG_ERR,"%s: socket creation error", __FUNCTION__)) @@ -160,8 +159,6 @@ int tac_connect_single(struct addrinfo *server, const char *key) { tac_secret = key; } - free(ip); - /* if valid fd, but error experienced after open, close fd */ if ( fd >= 0 && retval < 0 ) { close(fd); @@ -175,29 +172,32 @@ int tac_connect_single(struct addrinfo *server, const char *key) { /* return value: * ptr to char* with format IP address - * must be freed by caller + * warning: returns a static buffer + * (which some ppl don't like, but it's robust and at last no more memory leaks) */ -char *tac_ntop(const struct sockaddr *sa, size_t unused) { - char portstr[7]; - char *str = (char *) xcalloc(1, INET6_ADDRSTRLEN+sizeof(portstr)); +char *tac_ntop(const struct sockaddr *sa) { + static char server_address[INET6_ADDRSTRLEN+16]; switch(sa->sa_family) { case AF_INET: inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr), - str, INET_ADDRSTRLEN); - snprintf(portstr, sizeof(portstr), ":%hu", - htons(((struct sockaddr_in *)sa)->sin_port)); - strcat(str, portstr); + server_address, INET_ADDRSTRLEN); + + snprintf(server_address + strlen(server_address), 14, ":%hu", + htons(((struct sockaddr_in *)sa)->sin_port)); break; + case AF_INET6: inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)sa)->sin6_addr), - str, INET6_ADDRSTRLEN); - snprintf(portstr, sizeof(portstr), ":%hu", + server_address, INET6_ADDRSTRLEN); + + snprintf(server_address + strlen(server_address), 14, ":%hu", htons(((struct sockaddr_in6 *)sa)->sin6_port)); - strcat(str, portstr); break; + default: - strncpy(str, "Unknown AF", INET6_ADDRSTRLEN); + strcpy(server_address, "Unknown AF"); } - return str; + return server_address; } /* tac_ntop */ + diff --git a/pam_tacplus.c b/pam_tacplus.c index ea2478c..be0c826 100644 --- a/pam_tacplus.c +++ b/pam_tacplus.c @@ -445,7 +445,7 @@ int pam_sm_acct_mgmt (pam_handle_t * pamh, int flags, } if (ctrl & PAM_TAC_DEBUG) syslog (LOG_DEBUG, "%s: active server is [%s]", __FUNCTION__, - tac_ntop(active_server->addr->ai_addr, active_server->addr->ai_addrlen)); + tac_ntop(active_server->addr->ai_addr)); /* checks for specific data required by TACACS+, which should be supplied in command line */ @@ -280,7 +280,7 @@ int _pam_parse (int argc, const char **argv) { _pam_log(LOG_DEBUG, "%d servers defined", tac_srv_no); for(n = 0; n < tac_srv_no; n++) { - _pam_log(LOG_DEBUG, "server[%d] { addr=%s, key='%s' }", n, tac_ntop(tac_srv[n].addr->ai_addr, 0), tac_srv[n].key); + _pam_log(LOG_DEBUG, "server[%d] { addr=%s, key='%s' }", n, tac_ntop(tac_srv[n].addr->ai_addr), tac_srv[n].key); } } |