diff options
author | Simo Sorce <simo@redhat.com> | 2016-01-08 17:51:06 -0500 |
---|---|---|
committer | Jakub Hrozek <jhrozek@redhat.com> | 2016-06-29 21:46:03 +0200 |
commit | 4f3a9d837a55b49448eca3c713c85a406207e523 (patch) | |
tree | cf983b0fac5ddbc39ca259306342a0c05245168c /src/responder/nss/nsssrv_services.c | |
parent | 9a6d162cacfaf6946a1bf974b80b643d2a052d7a (diff) | |
download | sssd-4f3a9d837a55b49448eca3c713c85a406207e523.tar.gz sssd-4f3a9d837a55b49448eca3c713c85a406207e523.tar.xz sssd-4f3a9d837a55b49448eca3c713c85a406207e523.zip |
Responders: Make the client context more generic
This is useufl to allow reusing the responder code with other protocols.
Store protocol data and responder state data behind opaque pointers and
use tallog_get_type to check they are of the right type.
This also allows to store per responder state_ctx so that, for example,
the autofs responder does not have to carry useless variables used only
by the nss responder.
Resolves:
https://fedorahosted.org/sssd/ticket/2918
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
Diffstat (limited to 'src/responder/nss/nsssrv_services.c')
-rw-r--r-- | src/responder/nss/nsssrv_services.c | 113 |
1 files changed, 70 insertions, 43 deletions
diff --git a/src/responder/nss/nsssrv_services.c b/src/responder/nss/nsssrv_services.c index 05f9d52fa..e1afac54e 100644 --- a/src/responder/nss/nsssrv_services.c +++ b/src/responder/nss/nsssrv_services.c @@ -790,6 +790,7 @@ nss_cmd_getserv_done(struct tevent_req *req); int nss_cmd_getservbyname(struct cli_ctx *cctx) { errno_t ret; + struct cli_protocol *pctx; struct nss_cmd_ctx *cmdctx; struct nss_dom_ctx *dctx; char *domname; @@ -811,8 +812,10 @@ int nss_cmd_getservbyname(struct cli_ctx *cctx) } dctx->cmdctx = cmdctx; + pctx = talloc_get_type(cctx->protocol_ctx, struct cli_protocol); + /* get service name and protocol */ - sss_packet_get_body(cctx->creq->in, &body, &blen); + sss_packet_get_body(pctx->creq->in, &body, &blen); /* if not terminated fail */ if (body[blen -1] != '\0') { ret = EINVAL; @@ -986,7 +989,7 @@ nss_cmd_getserv_done(struct tevent_req *req) { errno_t ret, reqret; unsigned int i; - + struct cli_protocol *pctx; struct nss_dom_ctx *dctx = tevent_req_callback_data(req, struct nss_dom_ctx); struct nss_cmd_ctx *cmdctx = dctx->cmdctx; @@ -1000,17 +1003,19 @@ nss_cmd_getserv_done(struct tevent_req *req) return; } + pctx = talloc_get_type(cmdctx->cctx->protocol_ctx, struct cli_protocol); + /* Either we succeeded or no domains were eligible */ - ret = sss_packet_new(cmdctx->cctx->creq, 0, - sss_packet_get_cmd(cmdctx->cctx->creq->in), - &cmdctx->cctx->creq->out); + ret = sss_packet_new(pctx->creq, 0, + sss_packet_get_cmd(pctx->creq->in), + &pctx->creq->out); if (ret == EOK) { if (reqret == ENOENT) { /* Notify the caller that this entry wasn't found */ - ret = sss_cmd_empty_packet(cmdctx->cctx->creq->out); + ret = sss_cmd_empty_packet(pctx->creq->out); } else { i = dctx->res->count; - ret = fill_service(cmdctx->cctx->creq->out, + ret = fill_service(pctx->creq->out, dctx->domain, dctx->protocol, dctx->res->msgs, @@ -1105,6 +1110,7 @@ done: int nss_cmd_getservbyport(struct cli_ctx *cctx) { errno_t ret; + struct cli_protocol *pctx; struct nss_cmd_ctx *cmdctx; struct nss_dom_ctx *dctx; uint16_t port; @@ -1125,8 +1131,10 @@ int nss_cmd_getservbyport(struct cli_ctx *cctx) } dctx->cmdctx = cmdctx; + pctx = talloc_get_type(cctx->protocol_ctx, struct cli_protocol); + /* get service port and protocol */ - sss_packet_get_body(cctx->creq->in, &body, &blen); + sss_packet_get_body(pctx->creq->in, &body, &blen); /* if not terminated fail */ if (body[blen -1] != '\0') { ret = EINVAL; @@ -1194,14 +1202,17 @@ setservent_send(TALLOC_CTX *mem_ctx, struct cli_ctx *cctx) struct setservent_ctx *state; struct sss_domain_info *dom; struct setent_step_ctx *step_ctx; - struct nss_ctx *nctx = - talloc_get_type(cctx->rctx->pvt_ctx, struct nss_ctx); + struct nss_ctx *nctx; + struct nss_state_ctx *state_ctx; DEBUG(SSSDBG_TRACE_FUNC, "Received setservent request\n"); + nctx = talloc_get_type(cctx->rctx->pvt_ctx, struct nss_ctx); + state_ctx = talloc_get_type(cctx->state_ctx, struct nss_state_ctx); + /* Reset the read pointers */ - cctx->svc_dom_idx = 0; - cctx->svcent_cur = 0; + state_ctx->svcent.dom_idx = 0; + state_ctx->svcent.cur = 0; req = tevent_req_create(mem_ctx, &state, struct setservent_ctx); if (!req) return NULL; @@ -1611,7 +1622,9 @@ nss_cmd_setservent_done(struct tevent_req *req) errno_t ret; struct nss_cmd_ctx *cmdctx = tevent_req_callback_data(req, struct nss_cmd_ctx); + struct cli_protocol *pctx; + pctx = talloc_get_type(cmdctx->cctx->protocol_ctx, struct cli_protocol); ret = setservent_recv(req); talloc_zfree(req); if (ret == EOK || ret == ENOENT) { @@ -1619,9 +1632,9 @@ nss_cmd_setservent_done(struct tevent_req *req) * were eligible. * Return an acknowledgment */ - ret = sss_packet_new(cmdctx->cctx->creq, 0, - sss_packet_get_cmd(cmdctx->cctx->creq->in), - &cmdctx->cctx->creq->out); + ret = sss_packet_new(pctx->creq, 0, + sss_packet_get_cmd(pctx->creq->in), + &pctx->creq->out); if (ret == EOK) { sss_cmd_done(cmdctx->cctx, cmdctx); return; @@ -1648,6 +1661,7 @@ int nss_cmd_getservent(struct cli_ctx *cctx) struct nss_ctx *nctx; struct nss_cmd_ctx *cmdctx; struct tevent_req *req; + struct nss_state_ctx *state_ctx; DEBUG(SSSDBG_TRACE_FUNC, "Requesting info for all services\n"); @@ -1663,8 +1677,9 @@ int nss_cmd_getservent(struct cli_ctx *cctx) * expired and has to be recreated, we want to resume from the same * location. */ - cmdctx->saved_dom_idx = cctx->svc_dom_idx; - cmdctx->saved_cur = cctx->svcent_cur; + state_ctx = talloc_get_type(cctx->state_ctx, struct nss_state_ctx); + cmdctx->saved_dom_idx = state_ctx->svcent.dom_idx; + cmdctx->saved_cur = state_ctx->svcent.cur; nctx = talloc_get_type(cctx->rctx->pvt_ctx, struct nss_ctx); if(!nctx->svcctx || !nctx->svcctx->ready) { @@ -1690,6 +1705,7 @@ nss_cmd_implicit_setservent_done(struct tevent_req *req) errno_t ret; struct nss_cmd_ctx *cmdctx = tevent_req_callback_data(req, struct nss_cmd_ctx); + struct nss_state_ctx *state_ctx; ret = setservent_recv(req); talloc_zfree(req); @@ -1706,8 +1722,9 @@ nss_cmd_implicit_setservent_done(struct tevent_req *req) } /* Restore the saved index and cursor locations */ - cmdctx->cctx->svc_dom_idx = cmdctx->saved_dom_idx; - cmdctx->cctx->svcent_cur = cmdctx->saved_cur; + state_ctx = talloc_get_type(cmdctx->cctx->state_ctx, struct nss_state_ctx); + state_ctx->svcent.dom_idx = cmdctx->saved_dom_idx; + state_ctx->svcent.cur = cmdctx->saved_cur; ret = nss_cmd_getservent_immediate(cmdctx); if (ret != EOK) { @@ -1721,31 +1738,33 @@ nss_cmd_implicit_setservent_done(struct tevent_req *req) static errno_t nss_cmd_getservent_immediate(struct nss_cmd_ctx *cmdctx) { - struct cli_ctx *cctx = cmdctx->cctx; + struct cli_protocol *pctx; uint8_t *body; size_t blen; uint32_t num; int ret; + pctx = talloc_get_type(cmdctx->cctx->protocol_ctx, struct cli_protocol); + /* get max num of entries to return in one call */ - sss_packet_get_body(cctx->creq->in, &body, &blen); + sss_packet_get_body(pctx->creq->in, &body, &blen); if (blen != sizeof(uint32_t)) { return EINVAL; } SAFEALIGN_COPY_UINT32(&num, body, NULL); /* create response packet */ - ret = sss_packet_new(cctx->creq, 0, - sss_packet_get_cmd(cctx->creq->in), - &cctx->creq->out); + ret = sss_packet_new(pctx->creq, 0, + sss_packet_get_cmd(pctx->creq->in), + &pctx->creq->out); if (ret != EOK) { return ret; } - ret = retservent(cctx, num); + ret = retservent(cmdctx->cctx, num); - sss_packet_set_error(cctx->creq->out, ret); - sss_cmd_done(cctx, cmdctx); + sss_packet_set_error(pctx->creq->out, ret); + sss_cmd_done(cmdctx->cctx, cmdctx); return EOK; } @@ -1753,6 +1772,8 @@ nss_cmd_getservent_immediate(struct nss_cmd_ctx *cmdctx) static errno_t retservent(struct cli_ctx *cctx, int num) { + struct cli_protocol *pctx; + struct nss_state_ctx *state_ctx; struct nss_ctx *nctx; struct getent_ctx *svcctx; struct ldb_message **msgs = NULL; @@ -1760,59 +1781,65 @@ retservent(struct cli_ctx *cctx, int num) unsigned int n = 0; int ret = ENOENT; + pctx = talloc_get_type(cctx->protocol_ctx, struct cli_protocol); + state_ctx = talloc_get_type(cctx->state_ctx, struct nss_state_ctx); nctx = talloc_get_type(cctx->rctx->pvt_ctx, struct nss_ctx); if (!nctx->svcctx) goto none; svcctx = nctx->svcctx; while (ret == ENOENT) { - if (cctx->svc_dom_idx >= svcctx->num) break; + if (state_ctx->svcent.dom_idx >= svcctx->num) break; - pdom = &svcctx->doms[cctx->svc_dom_idx]; + pdom = &svcctx->doms[state_ctx->svcent.dom_idx]; - n = pdom->res->count - cctx->svcent_cur; - if (n <= 0 && (cctx->svc_dom_idx+1 < svcctx->num)) { - cctx->svc_dom_idx++; - pdom = &svcctx->doms[cctx->svc_dom_idx]; + n = pdom->res->count - state_ctx->svcent.cur; + if (n <= 0 && (state_ctx->svcent.dom_idx+1 < svcctx->num)) { + state_ctx->svcent.dom_idx++; + pdom = &svcctx->doms[state_ctx->svcent.dom_idx]; n = pdom->res->count; - cctx->svcent_cur = 0; + state_ctx->svcent.cur = 0; } if (!n) break; if (n > num) n = num; - msgs = &(pdom->res->msgs[cctx->svcent_cur]); + msgs = &(pdom->res->msgs[state_ctx->svcent.cur]); - ret = fill_service(cctx->creq->out, + ret = fill_service(pctx->creq->out, pdom->domain, NULL, msgs, &n); - cctx->svcent_cur += n; + state_ctx->svcent.cur += n; } none: if (ret == ENOENT) { - ret = sss_cmd_empty_packet(cctx->creq->out); + ret = sss_cmd_empty_packet(pctx->creq->out); } return ret; } int nss_cmd_endservent(struct cli_ctx *cctx) { + struct cli_protocol *pctx; + struct nss_state_ctx *state_ctx; struct nss_ctx *nctx; int ret; DEBUG(SSSDBG_TRACE_FUNC, "Terminating request info for all accounts\n"); + pctx = talloc_get_type(cctx->protocol_ctx, struct cli_protocol); + state_ctx = talloc_get_type(cctx->state_ctx, struct nss_state_ctx); nctx = talloc_get_type(cctx->rctx->pvt_ctx, struct nss_ctx); /* create response packet */ - ret = sss_packet_new(cctx->creq, 0, - sss_packet_get_cmd(cctx->creq->in), - &cctx->creq->out); + ret = sss_packet_new(pctx->creq, 0, + sss_packet_get_cmd(pctx->creq->in), + &pctx->creq->out); if (ret != EOK) { return ret; @@ -1820,8 +1847,8 @@ int nss_cmd_endservent(struct cli_ctx *cctx) if (nctx->svcctx == NULL) goto done; /* Reset the indices so that subsequent requests start at zero */ - cctx->svc_dom_idx = 0; - cctx->svcent_cur = 0; + state_ctx->svcent.dom_idx = 0; + state_ctx->svcent.cur = 0; done: sss_cmd_done(cctx, NULL); |