diff --git a/include/libnfs-private.h b/include/libnfs-private.h index 5e7272c4..60b26d35 100644 --- a/include/libnfs-private.h +++ b/include/libnfs-private.h @@ -317,6 +317,12 @@ struct auth_context { */ bool_t needs_refresh; + /* + * Auth token cached for this context. + * Updated after a successful call to get_token_callback_t. + */ + char *azauth_data; + /* * Expiry time of the current token. * Updated after a successful call to get_token_callback_t. diff --git a/lib/init.c b/lib/init.c index b4d10f05..fe099433 100644 --- a/lib/init.c +++ b/lib/init.c @@ -583,6 +583,7 @@ void rpc_destroy_context(struct rpc_context *rpc) free(rpc->auth_context.auth_type); free(rpc->auth_context.client_version); free(rpc->auth_context.client_id); + free(rpc->auth_context.azauth_data); rpc->auth_context.is_authorized = FALSE; rpc->use_azauth = FALSE; diff --git a/lib/libnfs.c b/lib/libnfs.c index f3c26249..40716be1 100755 --- a/lib/libnfs.c +++ b/lib/libnfs.c @@ -238,6 +238,16 @@ set_auth_token_callback(get_token_callback_t get_cb) static struct auth_token_cb_res * get_azauth_token(struct auth_context *auth) { + // Only access internals here, not in user code! + if (auth->azauth_data && auth->expiry_time > time(NULL)) { + fprintf(stderr, "Using cached auth token, expiry_time: %lu \n", + auth->expiry_time); + // Already cached and valid + struct auth_token_cb_res *cb_res = malloc(sizeof(struct auth_token_cb_res)); + cb_res->azauth_data = strdup(auth->azauth_data); + cb_res->expiry_time = auth->expiry_time; + return cb_res; + } return get_auth_token_cb(auth); } @@ -620,6 +630,7 @@ int nfs_set_auth_context(struct nfs_context *nfs, nfs->rpc->auth_context.client_id = strdup(client_id); nfs->rpc->auth_context.is_authorized = FALSE; nfs->rpc->auth_context.expiry_time = 0; + nfs->rpc->auth_context.azauth_data = NULL; return 0; } @@ -2862,10 +2873,12 @@ rpc_perform_azauth(struct rpc_context *rpc, rpc_cb cb, void *private_data) rpc->auth_context.is_authorized = FALSE; rpc->auth_context.expiry_time = res->expiry_time; + rpc->auth_context.azauth_data = strdup(res->azauth_data); struct azauth_cb_data *data = calloc(1, sizeof(*data)); if (data == NULL) { free_azauth_token(res, &azauthargs); + free(rpc->auth_context.azauth_data); rpc_set_error(rpc, "Out of memory. Failed to allocate azauth_cb_data"); return NULL; } @@ -2881,6 +2894,7 @@ rpc_perform_azauth(struct rpc_context *rpc, rpc_cb cb, void *private_data) if (pdu == NULL) { rpc_set_error(rpc, "AZAUTH RPC failed to set pdu"); free_azauth_token(res, &azauthargs); + free(rpc->auth_context.azauth_data); free_azauth_cb_data(data); return NULL; } diff --git a/lib/pdu.c b/lib/pdu.c index 94fd5829..b7a2cf62 100644 --- a/lib/pdu.c +++ b/lib/pdu.c @@ -919,7 +919,7 @@ int rpc_queue_pdu2(struct rpc_context *rpc, struct rpc_pdu *pdu, int prio) if (sendto(rpc->fd, pdu->zdr.buf, size, MSG_DONTWAIT, (struct sockaddr *)&rpc->udp_dest, sizeof(rpc->udp_dest)) < 0) { - rpc_set_error(rpc, "Sendto failed with errno %s", strerror(errno)); + rpc_set_error(rpc, "Send to failed with errno %s", strerror(errno)); rpc_free_pdu(rpc, pdu); return -1; } diff --git a/lib/socket.c b/lib/socket.c index e48c2226..3cbdddda 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -710,7 +710,7 @@ rpc_read_from_socket(struct rpc_context *rpc) case READ_RM: /* * Read record marker, - * And if this is a cleint context read the next 4 bytes + * And if this is a client context read the next 4 bytes * i.e. the XID on a client */ rpc->pdu_size = 8;