1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
/*
SSSD
sss_groupshow
Copyright (C) Jakub Hrozek <jhrozek@redhat.com> 2010
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <talloc.h>
#include <popt.h>
#include "db/sysdb.h"
#include "util/util.h"
#include "tools/tools_util.h"
#include "tools/sss_sync_ops.h"
struct group_show_state {
const char **user_members;
const char **group_members;
const char **memberofs;
gid_t gid;
struct sysdb_ctx *sysdb;
struct sss_domain_info *domain;
bool done;
int ret;
};
const char *rdn_as_string(TALLOC_CTX *mem_ctx,
struct ldb_dn *dn)
{
const struct ldb_val *val;
val = ldb_dn_get_rdn_val(dn);
if (val == NULL) {
return NULL;
}
return ldb_dn_escape_value(mem_ctx, *val);;
}
int parse_members(TALLOC_CTX *mem_ctx,
struct ldb_context *ldb,
struct sss_domain_info *domain,
struct ldb_message_element *el,
const char ***user_members,
const char ***group_members)
{
struct ldb_dn *user_basedn = NULL, *group_basedn = NULL;
struct ldb_dn *parent_dn = NULL;
struct ldb_dn *dn = NULL;
const char **um = NULL, **gm = NULL;
unsigned int um_index = 0, gm_index = 0;
TALLOC_CTX *tmp_ctx = NULL;
int ret;
int i;
tmp_ctx = talloc_new(mem_ctx);
if (!tmp_ctx) {
ret = ENOMEM;
goto fail;
}
user_basedn = ldb_dn_new_fmt(tmp_ctx, ldb,
SYSDB_TMPL_USER_BASE,
domain->name);
group_basedn = ldb_dn_new_fmt(tmp_ctx, ldb,
SYSDB_TMPL_GROUP_BASE,
domain->name);
if (!user_basedn || !group_basedn) {
ret = ENOMEM;
goto fail;
}
um = talloc_array(mem_ctx, const char *, el->num_values+1);
gm = talloc_array(mem_ctx, const char *, el->num_values+1);
if (!um || !gm) {
ret = ENOMEM;
goto fail;
}
for (i = 0; i< el->num_values; ++i) {
dn = ldb_dn_from_ldb_val(tmp_ctx, ldb, &(el->values[i]));
/* user member or group member? */
parent_dn = ldb_dn_get_parent(tmp_ctx, dn);
if (ldb_dn_compare_base(parent_dn, user_basedn) == 0) {
um[um_index] = rdn_as_string(mem_ctx, dn);
if (um[um_index] == NULL) {
ret = ENOMEM;
goto fail;
}
DEBUG(6, ("User member %s\n", um[um_index]));
um_index++;
} else if (ldb_dn_compare_base(parent_dn, group_basedn) == 0) {
gm[gm_index] = rdn_as_string(mem_ctx, dn);
if (gm[gm_index] == NULL) {
ret = ENOMEM;
goto fail;
}
DEBUG(6, ("Group member %s\n", gm[gm_index]));
gm_index++;
} else {
DEBUG(2, ("Group member not a user nor group: %s\n",
ldb_dn_get_linearized(dn)));
ret = EIO;
goto fail;
}
talloc_free(dn);
talloc_free(parent_dn);
}
um[um_index] = NULL;
gm[gm_index] = NULL;
um = talloc_realloc(mem_ctx, um, const char *, um_index+1);
gm = talloc_realloc(mem_ctx, gm, const char *, gm_index+1);
if (!um || !gm) {
ret = ENOMEM;
goto fail;
}
*user_members = um;
*group_members = gm;
talloc_free(tmp_ctx);
return EOK;
fail:
talloc_free(um);
talloc_free(gm);
talloc_free(tmp_ctx);
return ret;
}
static void group_show_done(struct tevent_req *req)
{
struct group_show_state *state = tevent_req_callback_data(req,
struct group_show_state);
int ret;
int i;
struct ldb_message *msg = NULL;
struct ldb_context *ldb = sysdb_ctx_get_ldb(state->sysdb);
struct ldb_message_element *el;
struct ldb_dn *dn = NULL;
ret = sysdb_search_group_recv(req, state, &msg);
talloc_zfree(req);
if (ret) {
DEBUG(2, ("Search failed: %s (%d)\n", strerror(ret), ret));
ret = EIO;
goto done;
}
DEBUG(6, ("Found entry %s\n", ldb_dn_get_linearized(msg->dn)));
/* list members */
el = ldb_msg_find_element(msg, SYSDB_MEMBER);
if (el) {
ret = parse_members(state, ldb, state->domain, el,
&state->user_members,
&state->group_members);
if (ret != EOK) {
goto done;
}
}
/* list memberofs */
el = ldb_msg_find_element(msg, SYSDB_MEMBEROF);
if (el) {
state->memberofs = talloc_array(state, const char *, el->num_values+1);
if (state->memberofs == NULL) {
ret = ENOMEM;
goto done;
}
for (i = 0; i< el->num_values; ++i) {
dn = ldb_dn_from_ldb_val(state, ldb, &(el->values[i]));
state->memberofs[i] = rdn_as_string(state, dn);
if (state->memberofs[i] == NULL) {
ret = ENOMEM;
goto done;
}
DEBUG(6, ("memberof value: %s\n", state->memberofs[i]));
talloc_free(dn);
}
state->memberofs[el->num_values] = NULL;
}
state->gid = ldb_msg_find_attr_as_uint64(msg,
SYSDB_GIDNUM, 0);
if (state->gid == 0) {
ret = EIO;
goto done;
}
ret = EOK;
done:
state->ret = ret;
state->done = true;
}
int main(int argc, const char **argv)
{
int ret = EXIT_SUCCESS;
int pc_debug = 0;
const char *pc_groupname = NULL;
struct tools_ctx *tctx = NULL;
struct tevent_req *req = NULL;
const char *attrs[] = { SYSDB_MEMBEROF, SYSDB_GIDNUM,
SYSDB_MEMBER, NULL };
struct group_show_state *search_state = NULL;
int i;
poptContext pc = NULL;
struct poptOption long_options[] = {
POPT_AUTOHELP
{ "debug", '\0', POPT_ARG_INT | POPT_ARGFLAG_DOC_HIDDEN, &pc_debug,
0, _("The debug level to run with"), NULL },
POPT_TABLEEND
};
debug_prg_name = argv[0];
ret = set_locale();
if (ret != EOK) {
DEBUG(1, ("set_locale failed (%d): %s\n", ret, strerror(ret)));
ERROR("Error setting the locale\n");
ret = EXIT_FAILURE;
goto fini;
}
/* parse ops_ctx */
pc = poptGetContext(NULL, argc, argv, long_options, 0);
poptSetOtherOptionHelp(pc, "GROUPNAME");
if ((ret = poptGetNextOpt(pc)) < -1) {
usage(pc, poptStrerror(ret));
ret = EXIT_FAILURE;
goto fini;
}
debug_level = pc_debug;
pc_groupname = poptGetArg(pc);
if (pc_groupname == NULL) {
usage(pc, _("Specify group to show\n"));
ret = EXIT_FAILURE;
goto fini;
}
CHECK_ROOT(ret, debug_prg_name);
ret = init_sss_tools(&tctx);
if (ret != EOK) {
DEBUG(1, ("init_sss_tools failed (%d): %s\n", ret, strerror(ret)));
if (ret == ENOENT) {
ERROR("Error initializing the tools - no local domain\n");
} else {
ERROR("Error initializing the tools\n");
}
ret = EXIT_FAILURE;
goto fini;
}
/* if the domain was not given as part of FQDN, default to local domain */
ret = parse_name_domain(tctx, pc_groupname);
if (ret != EOK) {
ERROR("Invalid domain specified in FQDN\n");
ret = EXIT_FAILURE;
goto fini;
}
/* Do the search */
search_state = talloc_zero(tctx, struct group_show_state);
if (search_state == NULL) {
ret = EXIT_FAILURE;
goto fini;
}
search_state->sysdb = tctx->sysdb;
search_state->domain = tctx->local;
req = sysdb_search_group_by_name_send(tctx, tctx->ev, tctx->sysdb,
tctx->handle, tctx->local,
tctx->octx->name, attrs);
if (!req || !search_state) {
ret = EXIT_FAILURE;
goto fini;
}
tevent_req_set_callback(req, group_show_done, search_state);
/* Busy wait */
while (!search_state->done) {
tevent_loop_once(tctx->ev);
}
/* Process result */
ret = search_state->ret;
if (ret) {
DEBUG(1, ("sysdb operation failed (%d)[%s]\n", ret, strerror(ret)));
switch (ret) {
case ENOENT:
ERROR("No such group in local domain. "
"Printing groups only allowed in local domain.\n");
break;
default:
ERROR("Internal error. Could not print group.\n");
break;
}
ret = EXIT_FAILURE;
goto fini;
}
/* print the results */
printf(_("Group: %s\nGID number: %d"), tctx->octx->name,
search_state->gid);
if (search_state->user_members) {
printf(_("\nMember users: "));
for (i=0; search_state->user_members[i]; ++i) {
printf("%s ", search_state->user_members[i]);
}
}
if (search_state->group_members) {
printf(_("\nMember groups: "));
for (i=0; search_state->group_members[i]; ++i) {
printf("%s ", search_state->group_members[i]);
}
}
if (search_state->memberofs) {
printf(_("\nIs a member of: "));
for (i=0; search_state->memberofs[i]; ++i) {
printf("%s ", search_state->memberofs[i]);
}
}
printf("\n");
fini:
talloc_free(tctx);
poptFreeContext(pc);
exit(ret);
}
|