sss_seed.c revision a004873f367b026fd033d4aa0bfa4225555c346e
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay prompt = talloc_asprintf(NULL, _("Enter %s:"), req);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay len = sss_atomic_write_s(STDOUT_FILENO, &prompt[i++], 1);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("write failed [%d][%s].\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay const char *req,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay while ((bytes_read = sss_atomic_read_s(STDIN_FILENO, buf+len, 1)) != 0) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("read failed [%d][%s].\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("Failed to allocate input\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay while ((bytes_read = sss_atomic_read_s(STDIN_FILENO, buf+len, 1)) != 0) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("read failed [%d][%s].\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_OP_FAILURE, ("strtoll failed on [%s]: [%d][%s].\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_MINOR_FAILURE, ("extra characters [%s] after "
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_OP_FAILURE, ("Failed to get %s input.\n", req));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guaystatic int seed_password_input_prompt(TALLOC_CTX *mem_ctx, char **_password)
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("Could not allocate temp context\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_OP_FAILURE, ("Failed to get prompted password\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay talloc_set_destructor((TALLOC_CTX *)password, password_destructor);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay temp = getpass("Enter temporary password again:");
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_OP_FAILURE, ("Failed to get prompted password\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (strncmp(temp,password,strlen(password)) != 0) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_MINOR_FAILURE, ("Provided passwords do not match\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guaystatic int seed_password_input_file(TALLOC_CTX *mem_ctx,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("Could not allocate temp context\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("Failed to open password file "
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay "[%s] [%d][%s]\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_MINOR_FAILURE, ("Failed to read password from file "
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay "[%s] [%d][%s]\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guaystatic int seed_interactive_input(TALLOC_CTX *mem_ctx,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = seed_str_input(input_uctx, _("username"), &input_uctx->name);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ("Username interactive input failed.\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay input_uctx->name = talloc_strdup(input_uctx, uctx->name);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_MINOR_FAILURE, ("UID interactive input failed.\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_MINOR_FAILURE, ("GID interactive input failed.\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = seed_str_input(input_uctx, _("user comment (gecos)"),
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_MINOR_FAILURE, ("Gecos interactive input failed.\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay input_uctx->gecos = talloc_strdup(input_uctx, uctx->gecos);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = seed_str_input(input_uctx, _("home directory"),
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ("Home directory interactive input fialed.\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay input_uctx->home = talloc_strdup(input_uctx, uctx->home);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = seed_str_input(input_uctx, _("user login shell"),
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_MINOR_FAILURE, ("Shell interactive input failed\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay input_uctx->shell = talloc_strdup(input_uctx, uctx->shell);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay const char **argv,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay { "debug", '\0', POPT_ARG_INT | POPT_ARGFLAG_DOC_HIDDEN, &pc_debug, 0,
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 { "shell", 's', POPT_ARG_STRING, &pc_shell, 0, _("Login Shell"), NULL },
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 /* init contexts */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("Could not allocate tools context\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("Could not allocate user data context\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("set_locale failed (%d): %s\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* parse arguments */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay pc = poptGetContext(NULL, argc, argv, options, 0);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay poptSetOtherOptionHelp(pc, "[OPTIONS] -D <domain> -n <username>");
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_TRACE_INTERNAL, ("Interactive mode selected\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay BAD_POPT_PARAMS(pc, poptStrerror(ret), ret, fini);
a004873f367b026fd033d4aa0bfa4225555c346eMichal Zidek debug_level = debug_convert_old_level(pc_debug);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* check username provided */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay BAD_POPT_PARAMS(pc, _("Username must be specified\n"), ret, fini);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx->uctx->name = talloc_strdup(sctx->uctx, pc_name);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* check domain is provided */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay BAD_POPT_PARAMS(pc, _("Domain must be specified.\n"), ret, fini);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx->uctx->domain_name = talloc_strdup(sctx->uctx, pc_domain);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* copy all information provided from popt */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx->uctx->gecos = talloc_strdup(sctx->uctx, pc_gecos);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx->uctx->home = talloc_strdup(sctx->uctx, pc_home);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx->uctx->shell = talloc_strdup(sctx->uctx, pc_shell);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* check if password file provided */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay sctx->password_file = talloc_strdup(sctx, pc_password_file);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* setup confdb */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay confdb_path = talloc_asprintf(tmp_ctx, "%s/%s", DB_PATH, CONFDB_FILE);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ("Could not initialize connection to the confdb\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ERROR("Could not initialize connection to the confdb\n");
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = sysdb_init_domain_and_sysdb(tmp_ctx, confdb, domain_name,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ("Could not initialize connection to the sysdb\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ERROR("Could not initialize the connection to the sysdb\n");
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay fq_name = talloc_asprintf(tmp_ctx, "%s@%s", name, domain_name);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_MINOR_FAILURE, ("getpwnam failed [%d] [%s]\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* look for user in cache */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ("Couldn't lookup user (%s) in the cache\n", name));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ("Multiple user (%s) entries were found in the cache\n", name));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_TRACE_INTERNAL, ("User found in cache\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_MINOR_FAILURE, ("initgroups failed [%d] [%s]\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("Failed to allocate user information\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("sysdb transaction start failure\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = sysdb_add_user(sctx->sysdb, sctx->uctx->name,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ("Failed to add user to the cache. (%d)[%s]\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = sysdb_cache_password(sctx->sysdb, sctx->uctx->name,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_OP_FAILURE, ("Failed to cache password. (%d)[%s]\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("sysdb transaction commit failure\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay if (in_transaction == true) {
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_OP_FAILURE, ("Failed to cancel transaction\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* initialize seed context and parse options */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_OP_FAILURE,("Seed init failed [%d][%d]\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* set up confdb,sysdb and domain */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = seed_init_db(sctx, sctx->uctx->domain_name, &sctx->confdb,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("Failed to initialize db and domain\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* get user info from domain */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = seed_domain_user_info(sctx->uctx->name, sctx->uctx->domain_name,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_OP_FAILURE, ("Failed lookup of user [%s] in domain [%s]\n",
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* interactive mode to fill in user information */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ERROR(_("User entry already exists in the cache.\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = seed_interactive_input(sctx, sctx->uctx, &input_uctx);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("Failed to get seed input.\n"));
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 /* password input */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = seed_password_input_file(sctx->uctx, sctx->password_file,
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("Password input failure\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay ret = seed_password_input_prompt(sctx->uctx, &sctx->uctx->password);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_CRIT_FAILURE, ("Password input failure\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay /* Add user info and password to sysdb cache */
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_MINOR_FAILURE, ("Failed to modify cache.\n"));
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay printf(_("User cache entry created for %1$s\n"), sctx->uctx->name);
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay printf(_("Temporary password added to cache entry for %1$s\n"),
6ea6ec5cb7d9985e2730fb9d4657624d10aed4d8Nick Guay DEBUG(SSSDBG_TRACE_INTERNAL, ("Exit error: [%d] [%s]\n",