Skip to content
This repository has been archived by the owner on Feb 28, 2024. It is now read-only.

Commit

Permalink
Remove unnecessary casts
Browse files Browse the repository at this point in the history
Unnecessary casts are a blight in that they mask possible programming
errors.  We shouldn't use them where they aren't required.

Also, add a convenience function to convert a header pointer to a
body pointer by adding the correct offset.
  • Loading branch information
pprindeville committed Dec 21, 2016
1 parent 783f106 commit 75413e2
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 43 deletions.
6 changes: 6 additions & 0 deletions libtac/include/libtac.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ void tac_session_free(struct tac_session *);
extern int tac_debug_enable;
extern int tac_readtimeout_enable;

/* we return a void * because there are different types of bodies */
static inline void *tac_hdr_to_body(HDR *th)
{
return (void *)((u_char *)th + TAC_PLUS_HDR_SIZE);
}

HDR *_tac_req_header(struct tac_session *, u_char, bool);

/* connect.c */
Expand Down
19 changes: 10 additions & 9 deletions libtac/lib/acct_r.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ int tac_acct_parse(struct tac_session *sess, u_char *pkt, unsigned pkt_total,

ulen_from_header = ntohl(th->datalength);

tb = (struct acct_reply *)(pkt + TAC_PLUS_HDR_SIZE);
tb = tac_hdr_to_body(th);

if (pkt_total != ulen_from_header) {
TACSYSLOG(LOG_ERR,\
Expand Down Expand Up @@ -88,14 +88,14 @@ int tac_acct_parse(struct tac_session *sess, u_char *pkt, unsigned pkt_total,

/* save status and clean up */
if(tb->msg_len) {
msg=(char *) xcalloc(1, tb->msg_len+1);
msg = xcalloc(1, tb->msg_len+1);
bcopy((u_char *) tb+TAC_ACCT_REPLY_FIXED_FIELDS_SIZE, msg, tb->msg_len);
msg[tb->msg_len] = '\0';
re->msg = msg; /* Freed by caller */
}

if(tb->data_len) {
msg=(char *) xcalloc(1, tb->data_len+1);
msg = xcalloc(1, tb->data_len+1);
bcopy((u_char *) tb+TAC_ACCT_REPLY_FIXED_FIELDS_SIZE+tb->data_len,
msg, tb->data_len);
msg[tb->data_len] = '\0';
Expand Down Expand Up @@ -156,9 +156,9 @@ int tac_acct_read(struct tac_session *sess, struct areply *re) {
return re->status;
}

th = (HDR *)xcalloc(1, TAC_PLUS_HDR_SIZE);
th = xcalloc(1, TAC_PLUS_HDR_SIZE);

spacket_read = read(sess->fd, (char *)th, TAC_PLUS_HDR_SIZE);
spacket_read = read(sess->fd, th, TAC_PLUS_HDR_SIZE);
if(spacket_read < TAC_PLUS_HDR_SIZE) {
TACSYSLOG(LOG_ERR,\
"%s: short reply header, read %zd of %u expected: %m", __FUNCTION__,\
Expand All @@ -181,8 +181,8 @@ int tac_acct_read(struct tac_session *sess, struct areply *re) {
}

/* now make room for entire contiguous packet */
th = (HDR *)xrealloc(th, TAC_PLUS_HDR_SIZE + ulen_from_header);
tb = (struct acct_reply *)((u_char *)th + TAC_PLUS_HDR_SIZE);
th = xrealloc(th, TAC_PLUS_HDR_SIZE + ulen_from_header);
tb = tac_hdr_to_body(th);

/* read reply packet body */
if (tac_readtimeout_enable &&
Expand All @@ -195,7 +195,7 @@ int tac_acct_read(struct tac_session *sess, struct areply *re) {
return re->status;
}

spacket_read = read(sess->fd, (char *)tb, ulen_from_header);
spacket_read = read(sess->fd, tb, ulen_from_header);
if(spacket_read < 0 || (size_t) spacket_read < ulen_from_header) {
TACSYSLOG(LOG_ERR,\
"%s: short reply body, read %zd of %zu: %m",\
Expand All @@ -208,7 +208,8 @@ int tac_acct_read(struct tac_session *sess, struct areply *re) {
}

/* now parse remaining packet fields */
status = tac_acct_parse(sess, (u_char *)th, TAC_PLUS_HDR_SIZE + ulen_from_header, re);
status = tac_acct_parse(sess, (u_char *)th,
TAC_PLUS_HDR_SIZE + ulen_from_header, re);

/* all useful data has been copied out */
free(th);
Expand Down
4 changes: 2 additions & 2 deletions libtac/lib/acct_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void tac_acct_send_pkt(struct tac_session *sess, u_char type,
pkt_total += a->attr_len + 1; /* count length byte too */
}

pkt = (u_char *)xcalloc(1, pkt_total);
pkt = xcalloc(1, pkt_total);
th = (HDR *)pkt;

/* tacacs header */
Expand All @@ -88,7 +88,7 @@ void tac_acct_send_pkt(struct tac_session *sess, u_char type,
th->datalength = htonl(pkt_total - TAC_PLUS_HDR_SIZE);

/* fixed part of tacacs body */
tb = (struct acct *)(pkt + TAC_PLUS_HDR_SIZE);
tb = tac_hdr_to_body(th);
tb->flags = type;
tb->authen_method = sess->tac_authen_method;
tb->priv_lvl = sess->tac_priv_lvl;
Expand Down
14 changes: 7 additions & 7 deletions libtac/lib/attrib.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ void tac_add_attrib(struct tac_attrib **attr, char *name, char *value) {

void tac_add_attrib_pair(struct tac_attrib **attr, char *name, char sep, char *value) {
struct tac_attrib *a;
u_char l1 = (u_char) strlen(name);
u_char l2;
int total_len;
unsigned l1 = (u_char) strlen(name);
unsigned l2;
unsigned total_len;

if (value == NULL) {
l2 = 0;
} else {
l2 = (u_char) strlen(value);
l2 = strlen(value);
}
total_len = l1 + l2 + 1; /* "name" + "=" + "value" */

Expand All @@ -49,15 +49,15 @@ void tac_add_attrib_pair(struct tac_attrib **attr, char *name, char sep, char *v

/* initialize the list if application passed us a null pointer */
if(*attr == NULL) {
*attr = (struct tac_attrib *) xcalloc(1, sizeof(struct tac_attrib));
*attr = xcalloc(1, sizeof(struct tac_attrib));
a = *attr;
} else {
/* find the last allocated block */
a = *attr;
while(a->next != NULL)
a = a->next; /* a holds last allocated block */

a->next = (struct tac_attrib *) xcalloc(1, sizeof(struct tac_attrib));
a->next = xcalloc(1, sizeof(struct tac_attrib));
a = a->next; /* set current block pointer to the new one */
}

Expand All @@ -67,7 +67,7 @@ void tac_add_attrib_pair(struct tac_attrib **attr, char *name, char sep, char *v

/* fill the block */
a->attr_len=total_len;
a->attr = (char *) xcalloc(1, total_len+1);
a->attr = xcalloc(1, total_len+1);
bcopy(name, a->attr, l1); /* paste name */
*(a->attr+l1)=sep; /* insert seperator "[=*]" */
if (value != NULL) {
Expand Down
10 changes: 5 additions & 5 deletions libtac/lib/authen_r.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ int tac_authen_parse(struct tac_session *sess, struct areply *re,

len_from_header = ntohl(th->datalength);

tb = (struct authen_reply *) (pkt + TAC_PLUS_HDR_SIZE);
tb = tac_hdr_to_body(th);

if (pkt_total != TAC_PLUS_HDR_SIZE + len_from_header) {
TACSYSLOG(
Expand Down Expand Up @@ -157,7 +157,7 @@ int tac_authen_read(struct tac_session *sess, struct areply *re) {
return re->status;
}

th = (HDR *)xcalloc(1, TAC_PLUS_HDR_SIZE);
th = xcalloc(1, TAC_PLUS_HDR_SIZE);

r = read(sess->fd, th, TAC_PLUS_HDR_SIZE);
if (r < TAC_PLUS_HDR_SIZE) {
Expand All @@ -181,8 +181,8 @@ int tac_authen_read(struct tac_session *sess, struct areply *re) {
}

/* now make room for entire contiguous packet */
th = (HDR *)xrealloc(th, TAC_PLUS_HDR_SIZE + len_from_header);
tb = (struct authen_reply *) ((u_char *)th + TAC_PLUS_HDR_SIZE);
th = xrealloc(th, TAC_PLUS_HDR_SIZE + len_from_header);
tb = tac_hdr_to_body(th);

/* read reply packet body */
if (tac_readtimeout_enable &&
Expand All @@ -192,7 +192,7 @@ int tac_authen_read(struct tac_session *sess, struct areply *re) {
status = LIBTAC_STATUS_READ_TIMEOUT;
}

r = read(sess->fd, (char *)tb, len_from_header);
r = read(sess->fd, tb, len_from_header);
if (r < 0 || (unsigned) r < len_from_header) {
TACSYSLOG(LOG_ERR,
"%s: short reply body, read %d of %zu: %m",
Expand Down
4 changes: 2 additions & 2 deletions libtac/lib/authen_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ void tac_authen_send_pkt(struct tac_session *sess,
pkt_total = TAC_AUTHEN_START_FIXED_TOTAL +
user_len + port_len + r_addr_len + token_len;

pkt = (u_char *)xcalloc(1, pkt_total);
pkt = xcalloc(1, pkt_total);
th = (HDR *)pkt;

/* set some header options */
Expand All @@ -159,7 +159,7 @@ void tac_authen_send_pkt(struct tac_session *sess,
th->datalength = htonl(pkt_total - TAC_PLUS_HDR_SIZE);

/* fixed part of tacacs body */
tb = (struct authen_start *)(pkt + TAC_PLUS_HDR_SIZE);
tb = tac_hdr_to_body(th);
tb->action = TAC_PLUS_AUTHEN_LOGIN;
tb->priv_lvl = sess->tac_priv_lvl;
if (sess->tac_authen_type == TAC_PLUS_AUTHEN_TYPE_PAP) {
Expand Down
12 changes: 6 additions & 6 deletions libtac/lib/author_r.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ int tac_author_parse(struct tac_session *sess,

len_from_header = ntohl(th->datalength);

tb = (struct author_reply *)(pkt + TAC_PLUS_HDR_SIZE);
tb = tac_hdr_to_body(th);

if (pkt_total != TAC_PLUS_HDR_SIZE + len_from_header) {
TACSYSLOG(
Expand Down Expand Up @@ -106,7 +106,7 @@ int tac_author_parse(struct tac_session *sess,

/* server message for user */
if (tb->msg_len) {
char *msg = (char *) xcalloc(1, tb->msg_len + 1);
char *msg = xcalloc(1, tb->msg_len + 1);
bcopy(
(u_char *) tb + TAC_AUTHOR_REPLY_FIXED_FIELDS_SIZE
+ (tb->arg_cnt) * sizeof(tb->arg_len[0]), msg, tb->msg_len);
Expand All @@ -116,7 +116,7 @@ int tac_author_parse(struct tac_session *sess,

/* server message to syslog */
if (tb->data_len) {
char *smsg = (char *) xcalloc(1, tb->data_len + 1);
char *smsg = xcalloc(1, tb->data_len + 1);
bcopy(
(u_char *) tb + TAC_AUTHOR_REPLY_FIXED_FIELDS_SIZE
+ (tb->arg_cnt) * sizeof(tb->arg_len[0]) + tb->msg_len, smsg,
Expand Down Expand Up @@ -248,7 +248,7 @@ int tac_author_read(struct tac_session *sess, struct areply *re) {
return re->status;
}

th = (HDR *)xcalloc(1, TAC_PLUS_HDR_SIZE);
th = xcalloc(1, TAC_PLUS_HDR_SIZE);

packet_read = read(sess->fd, th, TAC_PLUS_HDR_SIZE);
if (packet_read < TAC_PLUS_HDR_SIZE) {
Expand All @@ -272,8 +272,8 @@ int tac_author_read(struct tac_session *sess, struct areply *re) {
}

/* now make room for entire contiguous packet */
th = (HDR *)xrealloc(th, TAC_PLUS_HDR_SIZE + len_from_header);
tb = (struct author_reply *)((u_char *)th + TAC_PLUS_HDR_SIZE);
th = xrealloc(th, TAC_PLUS_HDR_SIZE + len_from_header);
tb = tac_hdr_to_body(th);

/* read reply packet body */
if (tac_readtimeout_enable
Expand Down
10 changes: 5 additions & 5 deletions libtac/lib/author_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ void tac_author_send_pkt(struct tac_session *sess,
/*
* precompute the buffer size so we don't need to keep resizing/copying it
*/
user_len = (u_char) strlen(user);
port_len = (u_char) strlen(tty);
r_addr_len = (u_char) strlen(r_addr);
user_len = strlen(user);
port_len = strlen(tty);
r_addr_len = strlen(r_addr);

assert(user_len <= UCHAR_MAX);
assert(port_len <= UCHAR_MAX);
Expand All @@ -60,7 +60,7 @@ void tac_author_send_pkt(struct tac_session *sess,
pkt_total += a->attr_len + 1; /* count length byte too */
}

pkt = (u_char *)xcalloc(1, pkt_total);
pkt = xcalloc(1, pkt_total);
th = (HDR *)pkt;

/* tacacs header */
Expand All @@ -72,7 +72,7 @@ void tac_author_send_pkt(struct tac_session *sess,
th->datalength = htonl(pkt_total - TAC_PLUS_HDR_SIZE);

/* fixed part of tacacs body */
tb = (struct author *)(pkt + TAC_PLUS_HDR_SIZE);
tb = tac_hdr_to_body(th);
tb->authen_method = sess->tac_authen_method;
tb->priv_lvl = sess->tac_priv_lvl;
tb->authen_type = sess->tac_authen_type;
Expand Down
4 changes: 2 additions & 2 deletions libtac/lib/cont_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void tac_cont_send_pkt(struct tac_session *sess, const char *pass,
pkt_total = TAC_AUTHEN_CONT_FIXED_TOTAL + pass_len;

/* build the packet */
pkt = (u_char *)xcalloc(1, pkt_total);
pkt = xcalloc(1, pkt_total);
th = (HDR *)pkt;

/* set some header options */
Expand All @@ -63,7 +63,7 @@ void tac_cont_send_pkt(struct tac_session *sess, const char *pass,
th->datalength = htonl(pkt_total - TAC_PLUS_HDR_SIZE);

/* fixed part of tacacs body */
tb = (struct authen_cont *)(pkt + TAC_PLUS_HDR_SIZE);
tb = tac_hdr_to_body(th);
tb->flags = 0;
tb->user_msg_len = htons(pass_len);
tb->user_data_len = 0;
Expand Down
4 changes: 2 additions & 2 deletions libtac/lib/xalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void *xcalloc(size_t nmemb, size_t size) {
void *val = calloc(nmemb, size);
if (val == 0) {
TACSYSLOG(
LOG_ERR, "%s: calloc(%u,%u) failed", __FUNCTION__, (unsigned) nmemb, (unsigned) size);
LOG_ERR, "%s: calloc(%zd,%zd) failed", __FUNCTION__, nmemb, size);
exit(1);
}
return val;
Expand All @@ -36,7 +36,7 @@ void *xrealloc(void *ptr, size_t size) {
void *val = realloc(ptr, size);
if (val == 0) {
TACSYSLOG(
LOG_ERR, "%s: realloc(%u) failed", __FUNCTION__, (unsigned) size);
LOG_ERR, "%s: realloc(%zd) failed", __FUNCTION__, size);
exit(1);
}
return val;
Expand Down
6 changes: 3 additions & 3 deletions support.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ char *_pam_get_user(pam_handle_t *pamh) {
int retval;
char *user;

retval = pam_get_user(pamh, (void *)&user, "Username: ");
retval = pam_get_user(pamh, (const char **)&user, "Username: ");
if (retval != PAM_SUCCESS || user == NULL || *user == '\0') {
_pam_log(LOG_ERR, "unable to obtain username");
user = NULL;
Expand All @@ -68,7 +68,7 @@ char *_pam_get_terminal(pam_handle_t *pamh) {
int retval;
char *tty;

retval = pam_get_item(pamh, PAM_TTY, (void *)&tty);
retval = pam_get_item(pamh, PAM_TTY, (const void **)&tty);
if (retval != PAM_SUCCESS || tty == NULL || *tty == '\0') {
tty = ttyname(STDIN_FILENO);
if(tty == NULL || *tty == '\0')
Expand All @@ -81,7 +81,7 @@ char *_pam_get_rhost(pam_handle_t *pamh) {
int retval;
char *rhost;

retval = pam_get_item(pamh, PAM_RHOST, (void *)&rhost);
retval = pam_get_item(pamh, PAM_RHOST, (const void **)&rhost);
if (retval != PAM_SUCCESS || rhost == NULL || *rhost == '\0') {
rhost = "unknown";
}
Expand Down

0 comments on commit 75413e2

Please sign in to comment.