~ubuntu-branches/ubuntu/saucy/sssd/saucy

« back to all changes in this revision

Viewing changes to server/tools/sss_groupdel.c

  • Committer: Stéphane Graber
  • Date: 2011-06-15 16:23:14 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: stgraber@ubuntu.com-20110615162314-rbhoppnpaxfqo5q7
Merge 1.5.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
   SSSD
3
 
 
4
 
   sss_groupdel
5
 
 
6
 
   Copyright (C) Jakub Hrozek <jhrozek@redhat.com>        2009
7
 
 
8
 
   This program is free software; you can redistribute it and/or modify
9
 
   it under the terms of the GNU General Public License as published by
10
 
   the Free Software Foundation; either version 3 of the License, or
11
 
   (at your option) any later version.
12
 
 
13
 
   This program is distributed in the hope that it will be useful,
14
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
   GNU General Public License for more details.
17
 
 
18
 
   You should have received a copy of the GNU General Public License
19
 
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 
*/
21
 
 
22
 
#include <stdio.h>
23
 
#include <stdlib.h>
24
 
#include <talloc.h>
25
 
#include <popt.h>
26
 
 
27
 
#include "db/sysdb.h"
28
 
#include "util/util.h"
29
 
#include "tools/tools_util.h"
30
 
#include "tools/sss_sync_ops.h"
31
 
 
32
 
int main(int argc, const char **argv)
33
 
{
34
 
    int ret = EXIT_SUCCESS;
35
 
    int pc_debug = 0;
36
 
    const char *pc_groupname = NULL;
37
 
    struct tools_ctx *tctx = NULL;
38
 
 
39
 
    poptContext pc = NULL;
40
 
    struct poptOption long_options[] = {
41
 
        POPT_AUTOHELP
42
 
        { "debug", '\0', POPT_ARG_INT | POPT_ARGFLAG_DOC_HIDDEN, &pc_debug,
43
 
                    0, _("The debug level to run with"), NULL },
44
 
        POPT_TABLEEND
45
 
    };
46
 
 
47
 
    debug_prg_name = argv[0];
48
 
 
49
 
    ret = set_locale();
50
 
    if (ret != EOK) {
51
 
        DEBUG(1, ("set_locale failed (%d): %s\n", ret, strerror(ret)));
52
 
        ERROR("Error setting the locale\n");
53
 
        ret = EXIT_FAILURE;
54
 
        goto fini;
55
 
    }
56
 
 
57
 
    /* parse ops_ctx */
58
 
    pc = poptGetContext(NULL, argc, argv, long_options, 0);
59
 
    poptSetOtherOptionHelp(pc, "GROUPNAME");
60
 
    if ((ret = poptGetNextOpt(pc)) < -1) {
61
 
        usage(pc, poptStrerror(ret));
62
 
        ret = EXIT_FAILURE;
63
 
        goto fini;
64
 
    }
65
 
 
66
 
    debug_level = pc_debug;
67
 
 
68
 
    pc_groupname = poptGetArg(pc);
69
 
    if (pc_groupname == NULL) {
70
 
        usage(pc, _("Specify group to delete\n"));
71
 
        ret = EXIT_FAILURE;
72
 
        goto fini;
73
 
    }
74
 
 
75
 
    CHECK_ROOT(ret, debug_prg_name);
76
 
 
77
 
    ret = init_sss_tools(&tctx);
78
 
    if (ret != EOK) {
79
 
        DEBUG(1, ("init_sss_tools failed (%d): %s\n", ret, strerror(ret)));
80
 
        if (ret == ENOENT) {
81
 
            ERROR("Error initializing the tools - no local domain\n");
82
 
        } else {
83
 
            ERROR("Error initializing the tools\n");
84
 
        }
85
 
        ret = EXIT_FAILURE;
86
 
        goto fini;
87
 
    }
88
 
 
89
 
    /* if the domain was not given as part of FQDN, default to local domain */
90
 
    ret = parse_name_domain(tctx, pc_groupname);
91
 
    if (ret != EOK) {
92
 
        ERROR("Invalid domain specified in FQDN\n");
93
 
        ret = EXIT_FAILURE;
94
 
        goto fini;
95
 
    }
96
 
 
97
 
    ret = sysdb_getgrnam_sync(tctx, tctx->ev, tctx->sysdb,
98
 
                              tctx->octx->name, tctx->local,
99
 
                              &tctx->octx);
100
 
    if (ret != EOK) {
101
 
        /* Error message will be printed in the switch */
102
 
        goto done;
103
 
    }
104
 
 
105
 
    if ((tctx->octx->gid < tctx->local->id_min) ||
106
 
        (tctx->local->id_max && tctx->octx->gid > tctx->local->id_max)) {
107
 
        ERROR("Group %s is outside the defined ID range for domain\n",
108
 
              tctx->octx->name);
109
 
        ret = EXIT_FAILURE;
110
 
        goto fini;
111
 
    }
112
 
 
113
 
    start_transaction(tctx);
114
 
    if (tctx->error != EOK) {
115
 
        goto done;
116
 
    }
117
 
 
118
 
    /* groupdel */
119
 
    ret = groupdel(tctx, tctx->ev, tctx->sysdb, tctx->handle, tctx->octx);
120
 
    if (ret != EOK) {
121
 
        tctx->error = ret;
122
 
 
123
 
        /* cancel transaction */
124
 
        talloc_zfree(tctx->handle);
125
 
        goto done;
126
 
    }
127
 
 
128
 
    end_transaction(tctx);
129
 
 
130
 
    ret = tctx->error;
131
 
done:
132
 
    if (ret) {
133
 
        DEBUG(1, ("sysdb operation failed (%d)[%s]\n", ret, strerror(ret)));
134
 
        switch (ret) {
135
 
            case ENOENT:
136
 
                ERROR("No such group in local domain. "
137
 
                      "Removing groups only allowed in local domain.\n");
138
 
                break;
139
 
 
140
 
            default:
141
 
                ERROR("Internal error. Could not remove group.\n");
142
 
                break;
143
 
        }
144
 
        ret = EXIT_FAILURE;
145
 
        goto fini;
146
 
    }
147
 
 
148
 
    ret = EXIT_SUCCESS;
149
 
 
150
 
fini:
151
 
    talloc_free(tctx);
152
 
    poptFreeContext(pc);
153
 
    exit(ret);
154
 
}
155