/*
SSSD
NSS Responder
Copyright (C) Simo Sorce <ssorce@redhat.com> 2008
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "util/util.h"
#include "util/btreemap.h"
#include "responder/common/responder_packet.h"
#include "responder/nss/nsssrv.h"
#include "db/sysdb.h"
#include <time.h>
struct nss_cmd_ctx {
struct cli_ctx *cctx;
const char *name;
uid_t id;
bool immediate;
bool done;
int nr;
};
struct getent_ctx {
struct ldb_result *pwds;
struct ldb_result *grps;
int pwd_cur;
int grp_cur;
};
struct nss_dom_ctx {
struct nss_cmd_ctx *cmdctx;
const char *domain;
bool check_provider;
bool legacy;
};
struct nss_cmd_table {
enum sss_cli_command cmd;
int (*fn)(struct cli_ctx *cctx);
};
static void nss_cmd_done(struct nss_cmd_ctx *cmdctx)
{
/* now that the packet is in place, unlock queue
* making the event writable */
TEVENT_FD_WRITEABLE(cmdctx->cctx->cfde);
/* free all request related data through the talloc hierarchy */
talloc_free(cmdctx);
}
static int nss_cmd_send_error(struct nss_cmd_ctx *cmdctx, int err)
{
struct cli_ctx *cctx = cmdctx->cctx;
int ret;
/* create response packet */
ret = sss_packet_new(cctx->creq, 0,
sss_packet_get_cmd(cctx->creq->in),
&cctx->creq->out);
if (ret != EOK) {
return ret;
}
sss_packet_set_error(cctx->creq->out, err);
return EOK;
}
#define NSS_CMD_FATAL_ERROR(cctx) do { \
DEBUG(1,("Fatal error, killing connection!")); \
talloc_free(cctx); \
return; \
} while(0)
static int nss_parse_name(struct nss_dom_ctx *dctx, const char *fullname)
{
struct nss_cmd_ctx *cmdctx = dctx->cmdctx;
struct nss_ctx *nctx = cmdctx->cctx->nctx;
struct nss_domain_info *info;
struct btreemap *domain_map;
char *delim;
char *domain;
domain_map = nctx->domain_map;
if ((delim = strchr(fullname, NSS_DOMAIN_DELIM)) != NULL) {
domain = delim+1;
} else {
domain = nctx->default_domain;
}
/* Check for registered domain */
info = btreemap_get_value(domain_map, (void *)domain);
if (!info) {
/* No such domain was registered. Return EINVAL.
* TODO: alternative approach?
* Alternatively, we could simply fail down to
* below, treating the entire construct as the
* full name if the domain is unspecified.
*/
return EINVAL;
}
dctx->check_provider = info->has_provider;
dctx->legacy = info->legacy;
dctx->domain = talloc_strdup(dctx, domain);
if (!dctx->domain) return ENOMEM;
if (delim) {
cmdctx->name = talloc_strndup(cmdctx, fullname, delim-fullname);
} else {
cmdctx->name = talloc_strdup(cmdctx, fullname);
}
if (!cmdctx->name) return ENOMEM;
return EOK;
}
static int nss_cmd_get_version(struct cli_ctx *cctx)
{
struct nss_cmd_ctx *cmdctx;
uint8_t *body;
size_t blen;
int ret;
cmdctx = talloc(cctx, struct nss_cmd_ctx);
if (!cmdctx) {
return ENOMEM;
}
cmdctx->cctx = cctx;
/* create response packet */
ret = sss_packet_new(cctx->creq, sizeof(uint32_t),
sss_packet_get_cmd(cctx->creq->in),
&cctx->creq->out);
if (ret != EOK) {
return ret;
}
sss_packet_get_body(cctx->creq->out, &body, &blen);
((uint32_t *)body)[0] = SSS_PROTOCOL_VERSION;
nss_cmd_done(cmdctx);
return EOK;
}
/****************************************************************************
* PASSWD db related functions
***************************************************************************/
static int fill_pwent(struct sss_packet *packet,
struct ldb_message **msgs,
int count)
{
struct ldb_message *msg;
uint8_t *body;
const char *name;
const char *fullname;
const char *homedir;
const char *shell;
uint64_t uid;
uint64_t gid;
size_t rsize, rp, blen;
size_t s1, s2, s3, s4;
int i, ret, num;
/* first 2 fields (len and reserved), filled up later */
ret = sss_packet_grow(packet, 2*sizeof(uint32_t));
rp = 2*sizeof(uint32_t);
num = 0;
for (i = 0; i < count; i++) {
msg = msgs[i];
name = ldb_msg_find_attr_as_string(msg, SYSDB_PW_NAME, NULL);
fullname = ldb_msg_find_attr_as_string(msg, SYSDB_PW_FULLNAME, NULL);
homedir = ldb_msg_find_attr_as_string(msg, SYSDB_PW_HOMEDIR, NULL);
shell = ldb_msg_find_attr_as_string(msg, SYSDB_PW_SHELL, NULL);
uid = ldb_msg_find_attr_as_uint64(msg, SYSDB_PW_UIDNUM, 0);
gid = ldb_msg_find_attr_as_uint64(msg, SYSDB_PW_GIDNUM, 0);
if (!name || !fullname || !homedir || !shell || !uid || !gid) {
DEBUG(1, ("Incomplete user object for %s[%llu]! Skipping\n",
name?name:"<NULL>", (unsigned long long int)uid));
continue;
}
s1 = strlen(name) + 1;
s2 = strlen(fullname) + 1;
s3 = strlen(homedir) + 1;
s4 = strlen(shell) + 1;
rsize = 2*sizeof(uint64_t) +s1 + 2 + s2 + s3 +s4;
ret = sss_packet_grow(packet, rsize);
if (ret != EOK) {
num = 0;
goto done;
}
sss_packet_get_body(packet, &body, &blen);
((uint64_t *)(&body[rp]))[0] = uid;
((uint64_t *)(&body[rp]))[1] = gid;
rp += 2*sizeof(uint64_t);
memcpy(&body[rp], name, s1);
rp += s1;
memcpy(&body[rp], "x", 2);
rp += 2;
memcpy(&body[rp], fullname, s2);
rp += s2;
memcpy(&body[rp], homedir, s3);
rp += s3;
memcpy(&body[rp], shell, s4);
rp += s4;
num++;
}
done:
sss_packet_get_body(packet, &body, &blen);
((uint32_t *)body)[0] = num; /* num results */
((uint32_t *)body)[1] = 0; /* reserved */
return EOK;
}
static void nss_cmd_getpwnam_dp_callback(uint16_t err_maj, uint32_t err_min,
const char *err_msg, void *ptr);
static void nss_cmd_getpwnam_callback(void *ptr, int status,
struct ldb_result *res)
{
struct nss_dom_ctx *dctx = talloc_get_type(ptr, struct nss_dom_ctx);
struct nss_cmd_ctx *cmdctx = dctx->cmdctx;
struct cli_ctx *cctx = cmdctx->cctx;
int timeout;
uint64_t lastUpdate;
uint8_t *body;
size_t blen;
bool call_provider = false;
int ret;
if (status != LDB_SUCCESS) {
ret = nss_cmd_send_error(cmdctx, status);
if (ret != EOK) {
NSS_CMD_FATAL_ERROR(cctx);
}
goto done;
}
if (dctx->check_provider) {
switch (res->count) {
case 0:
call_provider = true;
break;
case 1:
timeout = cmdctx->cctx->nctx->cache_timeout;
lastUpdate = ldb_msg_find_attr_as_uint64(res->msgs[0],
SYSDB_LAST_UPDATE, 0);
if (lastUpdate + timeout < time(NULL)) {
call_provider = true;
}
break;
default:
DEBUG(1, ("getpwnam call returned more than one result !?!\n"));
ret = nss_cmd_send_error(cmdctx, ret);
if (ret != EOK) {
NSS_CMD_FATAL_ERROR(cctx);
}
goto done;
}
}
if (call_provider) {
/* dont loop forever :-) */
dctx->check_provider = false;
timeout = SSS_CLI_SOCKET_TIMEOUT/2;
ret = nss_dp_send_acct_req(cctx->nctx, cmdctx,
nss_cmd_getpwnam_dp_callback, dctx,
timeout, dctx->domain, NSS_DP_USER,
cmdctx->name, 0);
if (ret != EOK) {
DEBUG(3, ("Failed to dispatch request: %d(%s)\n",
ret, strerror(ret)));
ret = nss_cmd_send_error(cmdctx, ret);
if (ret != EOK) {
NSS_CMD_FATAL_ERROR(cctx);
}
goto done;
}
return;
}
switch (res->count) {
case 0:
DEBUG(2, ("No results for getpwnam call\n"));
ret = sss_packet_new(cctx->creq, 2*sizeof(uint32_t),
sss_packet_get_cmd(cctx->creq->in),
&cctx->creq->out);
if (ret != EOK) {
NSS_CMD_FATAL_ERROR(cctx);
}
sss_packet_get_body(cctx->creq->out, &body, &blen);
((uint32_t *)body)[0] = 0; /* 0 results */
((uint32_t *)body)[1] = 0; /* reserved */
break;
case 1:
/* create response packet */
ret = sss_packet_new(cctx->creq, 0,
sss_packet_get_cmd(cctx->creq->in),
&cctx->creq->out);
if (ret != EOK) {
NSS_CMD_FATAL_ERROR(cctx);
}
|