Skip to content

Commit

Permalink
get_shadow_line: Refactor
Browse files Browse the repository at this point in the history
Refactor _get_shadow_line_from_server into get_shadow_line.
  • Loading branch information
donald committed May 7, 2021
1 parent 7bba383 commit f04a31c
Showing 1 changed file with 12 additions and 23 deletions.
35 changes: 12 additions & 23 deletions get_shadow_line.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ static int connect_with_timeout(int sockfd, struct sockaddr *addr, socklen_t add
return status;
}

static int _get_shadow_line_from_server(char *user, char *buf, size_t buflen) {
#define BUFLEN_SPWD (1024)

int get_shadow_line(char *user, char **line) {

struct sockaddr_in sockaddr;
bzero(&sockaddr, sizeof(sockaddr));
Expand Down Expand Up @@ -112,33 +114,20 @@ static int _get_shadow_line_from_server(char *user, char *buf, size_t buflen) {
return -1;
}

len = ssl_read_with_timeout(ssl, sock, buf, buflen, 1000);
if (len<0)
return -1;
SSL_shutdown(ssl);
if (len == buflen) {
fprintf(stderr, "%s: buffer to small", __func__);
errno = ERANGE; // as getspent
return -1;
}
buf[len] = '\0';
return 0;
}

#define BUFLEN_SPWD (1024)

int get_shadow_line(char *user, char **line) {
char *buffer _cleanup_(free_string) = malloc(BUFLEN_SPWD);
if (buffer == NULL)
return -1;
int status = _get_shadow_line_from_server(user, buffer, BUFLEN_SPWD);
if (status == -1) {
if (errno == ERANGE) {
/* we don't expect reply lines longer than BUFLEN_SPWD. If we get one, regard this as a protocol error */
errno = EPROTO;
}

len = ssl_read_with_timeout(ssl, sock, buffer, BUFLEN_SPWD, 1000);
if (len<0)
return -1;
SSL_shutdown(ssl);
if (len == BUFLEN_SPWD) {
/* we don't expect reply lines longer than BUFLEN_SPWD. If we get one, regard this as a protocol error */
errno = EPROTO;
return -1;
}
buffer[len] = '\0';
*line = buffer;
buffer = NULL;
return 0;
Expand Down

0 comments on commit f04a31c

Please sign in to comment.