sss_seed.c revision a004873f367b026fd033d4aa0bfa4225555c346e
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay#include <stdio.h>
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay#include <stdlib.h>
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay#include <string.h>
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay#include <talloc.h>
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay#include <popt.h>
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay#include <errno.h>
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay#include <unistd.h>
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay#include <limits.h>
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay#include <grp.h>
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay#include <pwd.h>
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay#include <sys/types.h>
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay#include <sys/stat.h>
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay#include <fcntl.h>
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay#include <ctype.h>
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay#include "util/util.h"
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay#include "db/sysdb.h"
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay#include "tools/tools_util.h"
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay#include "tools/sss_sync_ops.h"
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay#include "confdb/confdb.h"
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay#ifndef BUFSIZE
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay#define BUFSIZE 1024
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay#endif
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay#ifndef PASS_MAX
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay#define PASS_MAX 64
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay#endif
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guayenum seed_pass_method {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay PASS_PROMPT,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay PASS_FILE
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay};
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guaystruct user_ctx {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay char *domain_name;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay char *name;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay uid_t uid;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay gid_t gid;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay char *gecos;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay char *home;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay char *shell;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay char *password;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay};
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guaystruct seed_ctx {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay struct confdb_ctx *confdb;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay struct sysdb_ctx *sysdb;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay struct user_ctx *uctx;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay char *password_file;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay enum seed_pass_method password_method;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay bool interact;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay bool user_cached;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay};
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guaystatic int seed_prompt(const char *req)
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay{
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay size_t len = 0;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay size_t i = 0;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay char *prompt = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay int ret = EOK;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay prompt = talloc_asprintf(NULL, _("Enter %s:"), req);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (prompt == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = ENOMEM;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay while (prompt[i] != '\0') {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay errno = 0;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay len = sss_atomic_write_s(STDOUT_FILENO, &prompt[i++], 1);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (len == -1) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = errno;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("write failed [%d][%s].\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret, strerror(ret)));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guaydone:
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay talloc_free(prompt);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay return ret;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay}
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guaystatic int seed_str_input(TALLOC_CTX *mem_ctx,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay const char *req,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay char **_input)
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay{
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay char buf[BUFSIZE+1];
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay size_t len = 0;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay size_t bytes_read = 0;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay int ret = EOK;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = seed_prompt(req);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret != EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay return ret;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay errno = 0;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay while ((bytes_read = sss_atomic_read_s(STDIN_FILENO, buf+len, 1)) != 0) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (bytes_read == -1) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = errno;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("read failed [%d][%s].\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret, strerror(ret)));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay return ret;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (buf[len] == '\n' || len == BUFSIZE) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay buf[len] = '\0';
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay break;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay len += bytes_read;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay *_input = talloc_strdup(mem_ctx, buf);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (*_input == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = ENOMEM;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("Failed to allocate input\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay return ret;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay}
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guaystatic int seed_id_input(const char *req,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay uid_t *_id_input)
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay{
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay char buf[BUFSIZE+1];
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay size_t len = 0;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay size_t bytes_read = 0;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay char *endptr = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay int ret = EOK;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = seed_prompt(req);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret != EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay return ret;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay errno = 0;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay while ((bytes_read = sss_atomic_read_s(STDIN_FILENO, buf+len, 1)) != 0) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (bytes_read == -1) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = errno;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("read failed [%d][%s].\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret, strerror(ret)));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay return ret;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (buf[len] == '\n' || len == BUFSIZE) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay buf[len] = '\0';
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay break;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay len += bytes_read;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (isdigit(*buf)) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay errno = 0;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay *_id_input = (uid_t)strtoll(buf, &endptr, 10);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (errno != 0) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = errno;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_OP_FAILURE, ("strtoll failed on [%s]: [%d][%s].\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay (char *)buf, ret, strerror(ret)));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay return ret;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (*endptr != '\0') {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_MINOR_FAILURE, ("extra characters [%s] after "
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay "ID [%d]\n", endptr, *_id_input));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay } else {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = EINVAL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_OP_FAILURE, ("Failed to get %s input.\n", req));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay return ret;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay}
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guaystatic int seed_password_input_prompt(TALLOC_CTX *mem_ctx, char **_password)
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay{
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay TALLOC_CTX *tmp_ctx = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay char *password = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay char *temp = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay int ret = EOK;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay tmp_ctx = talloc_new(NULL);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (tmp_ctx == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("Could not allocate temp context\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = ENOMEM;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay temp = getpass("Enter temporary password:");
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (temp == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_OP_FAILURE, ("Failed to get prompted password\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = EINVAL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay password = talloc_strdup(tmp_ctx, temp);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (password == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = ENOMEM;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay talloc_set_destructor((TALLOC_CTX *)password, password_destructor);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay temp = getpass("Enter temporary password again:");
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (temp == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_OP_FAILURE, ("Failed to get prompted password\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = EINVAL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (strncmp(temp,password,strlen(password)) != 0) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ERROR("Passwords do not match\n");
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_MINOR_FAILURE, ("Provided passwords do not match\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = EINVAL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay *_password = talloc_steal(mem_ctx, password);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guaydone:
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay talloc_free(tmp_ctx);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay return ret;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay}
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guaystatic int seed_password_input_file(TALLOC_CTX *mem_ctx,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay char *filename,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay char **_password)
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay{
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay TALLOC_CTX *tmp_ctx = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay char *password = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay int len = 0;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay uint8_t buf[PASS_MAX+1];
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay int fd = -1;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay int ret = EOK;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay tmp_ctx = talloc_new(NULL);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (tmp_ctx == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("Could not allocate temp context\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = ENOMEM;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay fd = open(filename, O_RDONLY);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (fd == -1) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("Failed to open password file "
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay "[%s] [%d][%s]\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay filename, errno, strerror(errno)));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = EINVAL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay errno = 0;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay len = sss_atomic_read_s(fd, buf, PASS_MAX);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (len == -1) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = errno;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_MINOR_FAILURE, ("Failed to read password from file "
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay "[%s] [%d][%s]\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay filename, ret, strerror(ret)));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay close(fd);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay close(fd);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay buf[len] = '\0';
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay password = talloc_strdup(tmp_ctx, (char *)buf);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (password == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = ENOMEM;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay *_password = talloc_steal(mem_ctx, password);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guaydone:
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay talloc_free(tmp_ctx);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay return ret;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay}
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guaystatic int seed_interactive_input(TALLOC_CTX *mem_ctx,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay struct user_ctx *uctx,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay struct user_ctx **_uctx)
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay{
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay struct user_ctx *input_uctx = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay int ret = EOK;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay input_uctx = talloc_zero(NULL, struct user_ctx);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (input_uctx == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = ENOMEM;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (uctx->name == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = seed_str_input(input_uctx, _("username"), &input_uctx->name);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret != EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_MINOR_FAILURE,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ("Username interactive input failed.\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay } else {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay input_uctx->name = talloc_strdup(input_uctx, uctx->name);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (input_uctx->name == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = ENOMEM;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (uctx->uid == 0) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = seed_id_input(_("UID"), &input_uctx->uid);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret != EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_MINOR_FAILURE, ("UID interactive input failed.\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay } else {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay input_uctx->uid = uctx->uid;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (uctx->gid == 0) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = seed_id_input(_("GID"), &input_uctx->gid);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret != EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_MINOR_FAILURE, ("GID interactive input failed.\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay } else {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay input_uctx->gid = uctx->gid;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (uctx->gecos == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = seed_str_input(input_uctx, _("user comment (gecos)"),
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay &input_uctx->gecos);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret != EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_MINOR_FAILURE, ("Gecos interactive input failed.\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay } else {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay input_uctx->gecos = talloc_strdup(input_uctx, uctx->gecos);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (input_uctx->gecos == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = ENOMEM;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (uctx->home == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = seed_str_input(input_uctx, _("home directory"),
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay &input_uctx->home);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret != EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_MINOR_FAILURE,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ("Home directory interactive input fialed.\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay } else {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay input_uctx->home = talloc_strdup(input_uctx, uctx->home);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (input_uctx->home == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = ENOMEM;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (uctx->shell == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = seed_str_input(input_uctx, _("user login shell"),
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay &input_uctx->shell);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret != EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_MINOR_FAILURE, ("Shell interactive input failed\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay } else {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay input_uctx->shell = talloc_strdup(input_uctx, uctx->shell);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (input_uctx->shell == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = ENOMEM;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guaydone:
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret == EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay *_uctx = talloc_steal(mem_ctx, input_uctx);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay } else {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay talloc_zfree(input_uctx);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay return ret;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay}
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guaystatic int seed_init(TALLOC_CTX *mem_ctx,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay const int argc,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay const char **argv,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay struct seed_ctx **_sctx)
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay{
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay TALLOC_CTX *tmp_ctx = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay int pc_debug = SSSDBG_DEFAULT;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay const char *pc_domain = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay const char *pc_name = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay uid_t pc_uid = 0;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay gid_t pc_gid = 0;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay const char *pc_gecos = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay const char *pc_home = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay const char *pc_shell = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay const char *pc_password_file = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay struct seed_ctx *sctx = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay int ret = EOK;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay poptContext pc = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay struct poptOption options[] = {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay POPT_AUTOHELP
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay { "debug", '\0', POPT_ARG_INT | POPT_ARGFLAG_DOC_HIDDEN, &pc_debug, 0,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay _("The debug level to run with"), NULL },
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay { "domain", 'D', POPT_ARG_STRING, &pc_domain, 0, _("Domain"), NULL },
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay { "username", 'n', POPT_ARG_STRING, &pc_name, 0, _("Username"), NULL},
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay { "uid", 'u', POPT_ARG_INT, &pc_uid, 0, _("User UID"), NULL },
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay { "gid", 'g', POPT_ARG_INT, &pc_gid, 0, _("User GID"), NULL },
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay { "gecos", 'c', POPT_ARG_STRING, &pc_gecos, 0,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay _("Comment string"), NULL},
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay { "home", 'h', POPT_ARG_STRING, &pc_home, 0,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay _("Home directory"), NULL },
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay { "shell", 's', POPT_ARG_STRING, &pc_shell, 0, _("Login Shell"), NULL },
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay { "interactive", 'i', POPT_ARG_NONE, NULL, 'i',
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay _("Use interactive mode to enter user data"), NULL },
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay { "password-file", 'p', POPT_ARG_STRING, &pc_password_file, 0,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay _("File from which user's password is read "
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay "(default is to prompt for password)"),NULL },
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay POPT_TABLEEND
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay };
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* init contexts */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay tmp_ctx = talloc_new(NULL);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (tmp_ctx == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = ENOMEM;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto fini;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx = talloc_zero(tmp_ctx, struct seed_ctx);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (sctx == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("Could not allocate tools context\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = ENOMEM;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto fini;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx->uctx = talloc_zero(sctx, struct user_ctx);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (sctx->uctx == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("Could not allocate user data context\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = ENOMEM;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto fini;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay debug_prg_name = argv[0];
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = set_locale();
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret != EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("set_locale failed (%d): %s\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret, strerror(ret)));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ERROR("Error setting the locale\n");
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = EINVAL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto fini;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* parse arguments */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay pc = poptGetContext(NULL, argc, argv, options, 0);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (argc < 2) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay poptPrintUsage(pc,stderr,0);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = EINVAL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto fini;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay poptSetOtherOptionHelp(pc, "[OPTIONS] -D <domain> -n <username>");
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay while ((ret = poptGetNextOpt(pc)) > 0) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay switch (ret) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay case 'i':
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_TRACE_INTERNAL, ("Interactive mode selected\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx->interact = true;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay break;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret != -1) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay BAD_POPT_PARAMS(pc, poptStrerror(ret), ret, fini);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
a004873f367b026fd033d4aa0bfa4225555c346eMichal Zidek debug_level = debug_convert_old_level(pc_debug);
a004873f367b026fd033d4aa0bfa4225555c346eMichal Zidek
a004873f367b026fd033d4aa0bfa4225555c346eMichal Zidek CHECK_ROOT(ret, argv[0]);
a004873f367b026fd033d4aa0bfa4225555c346eMichal Zidek
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* check username provided */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (pc_name == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay BAD_POPT_PARAMS(pc, _("Username must be specified\n"), ret, fini);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx->uctx->name = talloc_strdup(sctx->uctx, pc_name);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (sctx->uctx->name == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = ENOMEM;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto fini;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* check domain is provided */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (pc_domain == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay BAD_POPT_PARAMS(pc, _("Domain must be specified.\n"), ret, fini);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx->uctx->domain_name = talloc_strdup(sctx->uctx, pc_domain);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (sctx->uctx->domain_name == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = ENOMEM;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto fini;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay poptFreeContext(pc);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = EOK;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* copy all information provided from popt */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx->uctx->uid = pc_uid;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx->uctx->gid = pc_gid;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (pc_gecos != NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx->uctx->gecos = talloc_strdup(sctx->uctx, pc_gecos);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (sctx->uctx->gecos == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = ENOMEM;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto fini;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (pc_home != NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx->uctx->home = talloc_strdup(sctx->uctx, pc_home);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (sctx->uctx->home == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = ENOMEM;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto fini;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (pc_shell != NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx->uctx->shell = talloc_strdup(sctx->uctx, pc_shell);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (sctx->uctx->shell == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = ENOMEM;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto fini;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* check if password file provided */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (pc_password_file != NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx->password_file = talloc_strdup(sctx, pc_password_file);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (sctx->password_file == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = ENOMEM;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto fini;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx->password_method = PASS_FILE;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay } else {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx->password_method = PASS_PROMPT;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay *_sctx = talloc_steal(mem_ctx, sctx);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guayfini:
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay talloc_free(tmp_ctx);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay return ret;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay}
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guaystatic int seed_init_db(TALLOC_CTX *mem_ctx,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay const char *domain_name,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay struct confdb_ctx **_confdb,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay struct sysdb_ctx **_sysdb)
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay{
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay TALLOC_CTX *tmp_ctx = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay char *confdb_path = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay struct confdb_ctx *confdb = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay struct sysdb_ctx *sysdb = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay struct sss_domain_info *domain = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay int ret = EOK;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay tmp_ctx = talloc_new(NULL);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (tmp_ctx == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = ENOMEM;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* setup confdb */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay confdb_path = talloc_asprintf(tmp_ctx, "%s/%s", DB_PATH, CONFDB_FILE);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (confdb_path == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = ENOMEM;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = confdb_init(tmp_ctx, &confdb, confdb_path);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret != EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ("Could not initialize connection to the confdb\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ERROR("Could not initialize connection to the confdb\n");
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = sysdb_init_domain_and_sysdb(tmp_ctx, confdb, domain_name,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DB_PATH, &domain, &sysdb);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret != EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ("Could not initialize connection to the sysdb\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ERROR("Could not initialize the connection to the sysdb\n");
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay *_sysdb = talloc_steal(mem_ctx, sysdb);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay *_confdb = talloc_steal(mem_ctx, confdb);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guaydone:
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay talloc_free(tmp_ctx);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay return ret;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay}
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guaystatic int seed_domain_user_info(const char *name,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay const char *domain_name,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay struct sysdb_ctx *sysdb,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay bool *is_cached)
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay{
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay TALLOC_CTX *tmp_ctx = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay char *fq_name = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay struct passwd *passwd = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay struct ldb_result *res = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay int ret = EOK;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay tmp_ctx = talloc_new(NULL);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (tmp_ctx == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = ENOMEM;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay fq_name = talloc_asprintf(tmp_ctx, "%s@%s", name, domain_name);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (fq_name == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = ENOMEM;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay errno = 0;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay passwd = getpwnam(fq_name);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (passwd == NULL) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = errno;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_MINOR_FAILURE, ("getpwnam failed [%d] [%s]\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret, strerror(ret)));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* look for user in cache */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = sysdb_getpwnam(tmp_ctx, sysdb, name, &res);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret != EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ("Couldn't lookup user (%s) in the cache\n", name));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (res->count == 0) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_TRACE_INTERNAL,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ("User (%s) wasn't found in the cache\n", name));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay *is_cached = false;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = ENOENT;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay } else if (res->count > 1) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ("Multiple user (%s) entries were found in the cache\n", name));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = EINVAL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay } else {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_TRACE_INTERNAL, ("User found in cache\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay *is_cached = true;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay errno = 0;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = initgroups(fq_name, passwd->pw_gid);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret != EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = errno;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_MINOR_FAILURE, ("initgroups failed [%d] [%s]\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret, strerror(ret)));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guaydone:
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret == ENOMEM) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("Failed to allocate user information\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay talloc_zfree(tmp_ctx);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay return ret;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay}
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guaystatic int seed_cache_user(struct seed_ctx *sctx)
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay{
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay bool in_transaction = false;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay int ret = EOK;
21d485184df986e1a123f70c689517386e51a5ceMichal Zidek errno_t sret;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = sysdb_transaction_start(sctx->sysdb);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret != EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("sysdb transaction start failure\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay in_transaction = true;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (sctx->user_cached == false) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = sysdb_add_user(sctx->sysdb, sctx->uctx->name,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx->uctx->uid, sctx->uctx->gid,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx->uctx->gecos, sctx->uctx->home,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx->uctx->shell, NULL, 0, 0);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret != EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_OP_FAILURE,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ("Failed to add user to the cache. (%d)[%s]\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret, strerror(ret)));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ERROR("Failed to create user cache entry\n");
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = sysdb_cache_password(sctx->sysdb, sctx->uctx->name,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx->uctx->password);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret != EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_OP_FAILURE, ("Failed to cache password. (%d)[%s]\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret, strerror(ret)));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ERROR("Failed to cache password\n");
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = sysdb_transaction_commit(sctx->sysdb);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret != EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("sysdb transaction commit failure\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay in_transaction = false;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guaydone:
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (in_transaction == true) {
21d485184df986e1a123f70c689517386e51a5ceMichal Zidek sret = sysdb_transaction_cancel(sctx->sysdb);
21d485184df986e1a123f70c689517386e51a5ceMichal Zidek if (sret != EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_OP_FAILURE, ("Failed to cancel transaction\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay return ret;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay}
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guayint main(int argc, const char **argv)
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay{
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay struct seed_ctx *sctx = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay struct user_ctx *input_uctx = NULL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay int ret = EOK;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* initialize seed context and parse options */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = seed_init(sctx, argc, argv, &sctx);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret != EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_OP_FAILURE,("Seed init failed [%d][%d]\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret, strerror(ret)));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* set up confdb,sysdb and domain */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = seed_init_db(sctx, sctx->uctx->domain_name, &sctx->confdb,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay &sctx->sysdb);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret != EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("Failed to initialize db and domain\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* get user info from domain */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = seed_domain_user_info(sctx->uctx->name, sctx->uctx->domain_name,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx->sysdb, &sctx->user_cached);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret != EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_OP_FAILURE, ("Failed lookup of user [%s] in domain [%s]\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx->uctx->name, sctx->uctx->domain_name));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* interactive mode to fill in user information */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (sctx->interact == true) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (sctx->user_cached == true) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ERROR(_("User entry already exists in the cache.\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = EEXIST;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay } else {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = seed_interactive_input(sctx, sctx->uctx, &input_uctx);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret != EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("Failed to get seed input.\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = EINVAL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay talloc_zfree(sctx->uctx);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx->uctx = input_uctx;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (sctx->user_cached == false) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (sctx->uctx->uid == 0 || sctx->uctx->gid == 0) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* require username, UID, and GID to continue */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_MINOR_FAILURE, ("Not enough information provided\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ERROR("UID and primary GID not provided.\n");
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = EINVAL;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* password input */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (sctx->password_method == PASS_FILE) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = seed_password_input_file(sctx->uctx, sctx->password_file,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay &sctx->uctx->password);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret != EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("Password input failure\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay } else {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = seed_password_input_prompt(sctx->uctx, &sctx->uctx->password);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret != EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("Password input failure\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* Add user info and password to sysdb cache */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = seed_cache_user(sctx);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret != EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_MINOR_FAILURE, ("Failed to modify cache.\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay goto done;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay } else {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (sctx->user_cached == false) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay printf(_("User cache entry created for %1$s\n"), sctx->uctx->name);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay printf(_("Temporary password added to cache entry for %1$s\n"),
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx->uctx->name);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guaydone:
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay talloc_zfree(sctx);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (ret != EOK) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_TRACE_INTERNAL, ("Exit error: [%d] [%s]\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret, strerror(ret)));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = EXIT_FAILURE;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay } else {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = EXIT_SUCCESS;
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay }
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay exit(ret);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay}