c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher/*
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher SSSD
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher sss_groupadd
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher Copyright (C) Jakub Hrozek <jhrozek@redhat.com> 2009
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher This program is free software; you can redistribute it and/or modify
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher it under the terms of the GNU General Public License as published by
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher the Free Software Foundation; either version 3 of the License, or
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher (at your option) any later version.
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher This program is distributed in the hope that it will be useful,
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher but WITHOUT ANY WARRANTY; without even the implied warranty of
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher GNU General Public License for more details.
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher You should have received a copy of the GNU General Public License
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher along with this program. If not, see <http://www.gnu.org/licenses/>.
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher*/
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher#include <stdio.h>
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher#include <stdlib.h>
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher#include <talloc.h>
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher#include <popt.h>
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher#include <errno.h>
77f445dbaecc8e792e1ad42e3742800ad141bee0Jakub Hrozek#include <unistd.h>
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce#include "util/util.h"
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce#include "db/sysdb.h"
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce#include "tools/tools_util.h"
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce#include "tools/sss_sync_ops.h"
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorceint main(int argc, const char **argv)
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce{
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce gid_t pc_gid = 0;
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce int pc_debug = SSSDBG_DEFAULT;
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce struct poptOption long_options[] = {
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce POPT_AUTOHELP
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov { "debug",'\0', POPT_ARG_INT | POPT_ARGFLAG_DOC_HIDDEN, &pc_debug,
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce 0, _("The debug level to run with"), NULL },
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce { "gid", 'g', POPT_ARG_INT, &pc_gid,
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce 0, _("The GID of the group"), NULL },
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce POPT_TABLEEND
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce };
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce poptContext pc = NULL;
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce struct tools_ctx *tctx = NULL;
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce int ret = EXIT_SUCCESS;
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce errno_t sret;
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce const char *pc_groupname = NULL;
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce bool in_transaction = false;
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce debug_prg_name = argv[0];
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce ret = set_locale();
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce if (ret != EOK) {
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce DEBUG(SSSDBG_CRIT_FAILURE,
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce "set_locale failed (%d): %s\n", ret, strerror(ret));
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce ERROR("Error setting the locale\n");
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce ret = EXIT_FAILURE;
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce goto fini;
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce }
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce /* parse params */
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce pc = poptGetContext(NULL, argc, argv, long_options, 0);
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce poptSetOtherOptionHelp(pc, "GROUPNAME");
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce if ((ret = poptGetNextOpt(pc)) < -1) {
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce BAD_POPT_PARAMS(pc, poptStrerror(ret), ret, fini);
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce }
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce DEBUG_CLI_INIT(pc_debug);
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce /* groupname is an argument, not option */
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce pc_groupname = poptGetArg(pc);
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce if (pc_groupname == NULL) {
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce BAD_POPT_PARAMS(pc, _("Specify group to add\n"), ret, fini);
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce }
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce CHECK_ROOT(ret, debug_prg_name);
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce ret = init_sss_tools(&tctx);
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce if (ret != EOK) {
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce DEBUG(SSSDBG_CRIT_FAILURE,
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce "init_sss_tools failed (%d): %s\n", ret, strerror(ret));
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce if (ret == ENOENT) {
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce ERROR("Error initializing the tools - no local domain\n");
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce } else {
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce ERROR("Error initializing the tools\n");
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce }
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce ret = EXIT_FAILURE;
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce goto fini;
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce }
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce /* if the domain was not given as part of FQDN, default to local domain */
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce ret = parse_name_domain(tctx, pc_groupname);
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce if (ret != EOK) {
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce ERROR("Invalid domain specified in FQDN\n");
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce ret = EXIT_FAILURE;
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce goto fini;
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce }
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce tctx->octx->gid = pc_gid;
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce /* arguments processed, go on to actual work */
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce if (id_in_range(tctx->octx->gid, tctx->octx->domain) != EOK) {
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce ERROR("The selected GID is outside the allowed range\n");
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce ret = EXIT_FAILURE;
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce goto fini;
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher }
cff916f5352fe7c3a679571130090efdb935618aStephen Gallagher
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher tctx->error = sysdb_transaction_start(tctx->sysdb);
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher if (tctx->error != EOK) {
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce DEBUG(SSSDBG_CRIT_FAILURE, "Failed to start transaction\n");
cff916f5352fe7c3a679571130090efdb935618aStephen Gallagher goto done;
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher }
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce in_transaction = true;
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher /* groupadd */
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher tctx->error = groupadd(tctx->octx);
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher if (tctx->error) {
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce goto done;
cff916f5352fe7c3a679571130090efdb935618aStephen Gallagher }
cff916f5352fe7c3a679571130090efdb935618aStephen Gallagher
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov tctx->error = sysdb_transaction_commit(tctx->sysdb);
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov if (tctx->error != EOK) {
cff916f5352fe7c3a679571130090efdb935618aStephen Gallagher DEBUG(SSSDBG_CRIT_FAILURE, "Failed to commit transaction\n");
cff916f5352fe7c3a679571130090efdb935618aStephen Gallagher goto done;
cff916f5352fe7c3a679571130090efdb935618aStephen Gallagher }
cff916f5352fe7c3a679571130090efdb935618aStephen Gallagher in_transaction = false;
cff916f5352fe7c3a679571130090efdb935618aStephen Gallagher
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagherdone:
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher if (in_transaction) {
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce sret = sysdb_transaction_cancel(tctx->sysdb);
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher if (sret != EOK) {
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher DEBUG(SSSDBG_CRIT_FAILURE, "Failed to cancel transaction\n");
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher }
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher }
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher if (tctx->error) {
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher ret = tctx->error;
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher switch (ret) {
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher case ERANGE:
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher ERROR("Could not allocate ID for the group - domain full?\n");
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher break;
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher case EEXIST:
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher ERROR("A group with the same name or GID already exists\n");
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher break;
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher default:
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher DEBUG(SSSDBG_CRIT_FAILURE,
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher "sysdb operation failed (%d)[%s]\n", ret, strerror(ret));
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher ERROR("Transaction error. Could not add group.\n");
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher break;
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher }
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher ret = EXIT_FAILURE;
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher goto fini;
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce }
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagher ret = EXIT_SUCCESS;
c2352a73f52f600d95966ebe0b0819649ba923faStephen Gallagherfini:
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce talloc_free(tctx);
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce poptFreeContext(pc);
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce exit(ret);
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce}
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce
868ae511c9b0d610f83acf8f01975e1f5e3c1aa3Simo Sorce