sss_userdel.c revision e391eb27c711a5d51f7a5ad92928212fdcb3133a
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek/*
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek SSSD
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek sss_userdel
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek Copyright (C) Jakub Hrozek <jhrozek@redhat.com> 2009
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek This program is free software; you can redistribute it and/or modify
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek it under the terms of the GNU General Public License as published by
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek the Free Software Foundation; either version 3 of the License, or
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek (at your option) any later version.
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek This program is distributed in the hope that it will be useful,
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek but WITHOUT ANY WARRANTY; without even the implied warranty of
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek GNU General Public License for more details.
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek You should have received a copy of the GNU General Public License
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek along with this program. If not, see <http://www.gnu.org/licenses/>.
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek*/
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek#include <nss.h>
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek#include <stdio.h>
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek#include <stdlib.h>
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek#include <talloc.h>
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek#include <popt.h>
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek#include <sys/types.h>
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek#include <sys/wait.h>
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek#include "db/sysdb.h"
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek#include "util/util.h"
5cd4414fce1e0eb4133dfc6fc828bf25c8a959f9Lukas Slebodnik#include "util/find_uid.h"
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce#include "tools/tools_util.h"
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek#include "tools/sss_sync_ops.h"
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek#ifndef KILL_CMD
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek#define KILL_CMD "killall"
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek#endif
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek#ifndef KILL_CMD_USER_FLAG
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek#define KILL_CMD_USER_FLAG "-u"
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek#endif
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek#ifndef KILL_CMD_SIGNAL_FLAG
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek#define KILL_CMD_SIGNAL_FLAG "-s"
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek#endif
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov#ifndef KILL_CMD_SIGNAL
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov#define KILL_CMD_SIGNAL "SIGKILL"
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov#endif
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozekstatic int is_logged_in(TALLOC_CTX *mem_ctx, uid_t uid)
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek{
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek int ret;
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek hash_key_t key;
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek hash_value_t value;
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek hash_table_t *uid_table;
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek ret = get_uid_table(mem_ctx, &uid_table);
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek if (ret == ENOSYS) return ret;
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek if (ret != EOK) {
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek DEBUG(1, ("Cannot initialize hash table.\n"));
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek return ret;
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek }
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek key.type = HASH_KEY_ULONG;
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov key.ul = (unsigned long) uid;
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov ret = hash_lookup(uid_table, &key, &value);
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek talloc_zfree(uid_table);
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek return ret == HASH_SUCCESS ? EOK : ENOENT;
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek}
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozekstatic int kick_user(struct tools_ctx *tctx)
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek{
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek int ret;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce int status;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce pid_t pid, child_pid;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce tctx->octx->lock = 1;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = usermod(tctx, tctx->sysdb, tctx->octx);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (ret != EOK) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce return ret;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce errno = 0;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce pid = fork();
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (pid == 0) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce /* child */
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce execlp(KILL_CMD, KILL_CMD,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce KILL_CMD_USER_FLAG, tctx->octx->name,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce KILL_CMD_SIGNAL_FLAG, KILL_CMD_SIGNAL,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce (char *) NULL);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce exit(errno);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce } else {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce /* parent */
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (pid == -1) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce DEBUG(1, ("fork failed [%d]: %s\n"));
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce return errno;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce while((child_pid = waitpid(pid, &status, 0)) > 0) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (WIFEXITED(status)) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce break;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (child_pid == -1) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce DEBUG(SSSDBG_CRIT_FAILURE, ("waitpid failed\n"));
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce return errno;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce return EOK;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce}
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorceint main(int argc, const char **argv)
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce{
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce int ret = EXIT_SUCCESS;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce struct tools_ctx *tctx = NULL;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce const char *pc_username = NULL;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce int pc_debug = SSSDBG_DEFAULT;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce int pc_remove = 0;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce int pc_force = 0;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce int pc_kick = 0;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce poptContext pc = NULL;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce struct poptOption long_options[] = {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce POPT_AUTOHELP
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce { "debug", '\0', POPT_ARG_INT | POPT_ARGFLAG_DOC_HIDDEN, &pc_debug,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce 0, _("The debug level to run with"), NULL },
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce { "remove", 'r', POPT_ARG_NONE, NULL, 'r',
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce _("Remove home directory and mail spool"), NULL },
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce { "no-remove", 'R', POPT_ARG_NONE, NULL, 'R',
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce _("Do not remove home directory and mail spool"), NULL },
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce { "force", 'f', POPT_ARG_NONE, NULL, 'f',
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce _("Force removal of files not owned by the user"), NULL },
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce { "kick", 'k', POPT_ARG_NONE, NULL, 'k',
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce _("Kill users' processes before removing him"), NULL },
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce POPT_TABLEEND
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce };
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce debug_prg_name = argv[0];
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = set_locale();
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (ret != EOK) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce DEBUG(1, ("set_locale failed (%d): %s\n", ret, strerror(ret)));
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ERROR("Error setting the locale\n");
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = EXIT_FAILURE;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto fini;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce /* parse parameters */
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce pc = poptGetContext(NULL, argc, argv, long_options, 0);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce poptSetOtherOptionHelp(pc, "USERNAME");
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce while ((ret = poptGetNextOpt(pc)) > 0) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce switch (ret) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce case 'r':
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce pc_remove = DO_REMOVE_HOME;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce break;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce case 'R':
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce pc_remove = DO_NOT_REMOVE_HOME;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce break;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce case 'f':
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce pc_force = DO_FORCE_REMOVAL;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce break;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce case 'k':
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce pc_kick = 1;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce break;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce DEBUG_INIT(pc_debug);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (ret != -1) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce BAD_POPT_PARAMS(pc, poptStrerror(ret), ret, fini);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce pc_username = poptGetArg(pc);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (pc_username == NULL) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce BAD_POPT_PARAMS(pc, _("Specify user to delete\n"), ret, fini);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce CHECK_ROOT(ret, debug_prg_name);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = init_sss_tools(&tctx);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (ret != EOK) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce DEBUG(1, ("init_sss_tools failed (%d): %s\n", ret, strerror(ret)));
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (ret == ENOENT) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ERROR("Error initializing the tools - no local domain\n");
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce } else {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ERROR("Error initializing the tools\n");
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = EXIT_FAILURE;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto fini;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce /* if the domain was not given as part of FQDN, default to local domain */
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = parse_name_domain(tctx, pc_username);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (ret != EOK) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ERROR("Invalid domain specified in FQDN\n");
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = EXIT_FAILURE;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto fini;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce /*
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce * Fills in defaults for ops_ctx user did not specify.
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce */
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = userdel_defaults(tctx, tctx->confdb, tctx->octx, pc_remove);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (ret != EOK) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ERROR("Cannot set default values\n");
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = EXIT_FAILURE;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto fini;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = sysdb_getpwnam_sync(tctx,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce tctx->sysdb,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce tctx->octx->name,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce tctx->octx);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (ret != EOK) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce /* Error message will be printed in the switch */
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto done;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if ((tctx->octx->uid < tctx->local->id_min) ||
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce (tctx->local->id_max && tctx->octx->uid > tctx->local->id_max)) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ERROR("User %1$s is outside the defined ID range for domain\n",
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce tctx->octx->name);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = EXIT_FAILURE;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto fini;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (pc_kick) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = kick_user(tctx);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (ret != EOK) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce tctx->error = ret;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto done;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce /* userdel */
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = userdel(tctx, tctx->sysdb, tctx->octx);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (ret != EOK) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto done;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce /* Set SELinux login context - must be done after transaction is done
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce * b/c libselinux calls getpwnam */
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = del_seuser(tctx->octx->name);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (ret != EOK) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ERROR("Cannot reset SELinux login context\n");
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = EXIT_FAILURE;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto fini;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (!pc_kick) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = is_logged_in(tctx, tctx->octx->uid);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce switch(ret) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce case ENOENT:
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce break;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce case EOK:
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ERROR("WARNING: The user (uid %1$lu) was still logged in when "
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce "deleted.\n", (unsigned long) tctx->octx->uid);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce break;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce case ENOSYS:
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ERROR("Cannot determine if the user was logged in on this "
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce "platform");
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce break;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce default:
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ERROR("Error while checking if the user was logged in\n");
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce break;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = run_userdel_cmd(tctx);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (ret != EOK) {
ERROR("The post-delete command failed: %1$s\n", strerror(ret));
goto fini;
}
/* Delete user from memory cache */
ret = sss_mc_refresh_user(pc_username);
if (ret != EOK) {
ERROR("NSS request failed (%1$d). Entry might remain in memory "
"cache.\n", ret);
/* Nothing we can do about it */
}
if (tctx->octx->remove_homedir) {
ret = remove_homedir(tctx,
tctx->octx->home,
tctx->octx->maildir,
tctx->octx->name,
tctx->octx->uid,
pc_force);
if (ret == EPERM) {
ERROR("Not removing home dir - not owned by user\n");
} else if (ret != EOK) {
ERROR("Cannot remove homedir: %1$s\n", strerror(ret));
ret = EXIT_FAILURE;
goto fini;
}
}
ret = EOK;
done:
if (ret) {
DEBUG(1, ("sysdb operation failed (%d)[%s]\n", ret, strerror(ret)));
switch (ret) {
case ENOENT:
ERROR("No such user in local domain. "
"Removing users only allowed in local domain.\n");
break;
default:
ERROR("Internal error. Could not remove user.\n");
break;
}
ret = EXIT_FAILURE;
goto fini;
}
ret = EXIT_SUCCESS;
fini:
talloc_free(tctx);
poptFreeContext(pc);
exit(ret);
}