1 /* group-list.c --Print a list of group IDs or names.
2 Copyright (C) 1989-2023 Free Software Foundation, Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 /* Written by Arnold Robbins.
18 Major rewrite by David MacKenzie, djm@gnu.ai.mit.edu.
19 Extracted from id.c by James Youngman. */
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <pwd.h>
25 #include <grp.h>
26
27 #include "system.h"
28 #include "mgetgroups.h"
29 #include "quote.h"
30 #include "group-list.h"
31
32
33 /* Print all of the distinct groups the user is in. */
34 extern bool
print_group_list(char const * username,uid_t ruid,gid_t rgid,gid_t egid,bool use_names,char delim)35 print_group_list (char const *username,
36 uid_t ruid, gid_t rgid, gid_t egid,
37 bool use_names, char delim)
38 {
39 bool ok = true;
40 struct passwd *pwd = nullptr;
41
42 if (username)
43 {
44 pwd = getpwuid (ruid);
45 if (pwd == nullptr)
46 ok = false;
47 }
48
49 if (!print_group (rgid, use_names))
50 ok = false;
51
52 if (egid != rgid)
53 {
54 putchar (delim);
55 if (!print_group (egid, use_names))
56 ok = false;
57 }
58
59 {
60 gid_t *groups;
61
62 int n_groups = xgetgroups (username, (pwd ? pwd->pw_gid : egid), &groups);
63 if (n_groups < 0)
64 {
65 if (username)
66 {
67 error (0, errno, _("failed to get groups for user %s"),
68 quote (username));
69 }
70 else
71 {
72 error (0, errno, _("failed to get groups for the current process"));
73 }
74 return false;
75 }
76
77 for (int i = 0; i < n_groups; i++)
78 if (groups[i] != rgid && groups[i] != egid)
79 {
80 putchar (delim);
81 if (!print_group (groups[i], use_names))
82 ok = false;
83 }
84 free (groups);
85 }
86 return ok;
87 }
88
89 /* Convert a gid_t to string. Do not use this function directly.
90 Instead, use it via the gidtostr macro.
91 Beware that it returns a pointer to static storage. */
92 static char *
gidtostr_ptr(gid_t const * gid)93 gidtostr_ptr (gid_t const *gid)
94 {
95 static char buf[INT_BUFSIZE_BOUND (uintmax_t)];
96 return umaxtostr (*gid, buf);
97 }
98 #define gidtostr(g) gidtostr_ptr (&(g))
99
100 /* Print the name or value of group ID GID. */
101 extern bool
print_group(gid_t gid,bool use_name)102 print_group (gid_t gid, bool use_name)
103 {
104 struct group *grp = nullptr;
105 bool ok = true;
106
107 if (use_name)
108 {
109 grp = getgrgid (gid);
110 if (grp == nullptr)
111 {
112 if (TYPE_SIGNED (gid_t))
113 {
114 intmax_t g = gid;
115 error (0, 0, _("cannot find name for group ID %jd"), g);
116 }
117 else
118 {
119 uintmax_t g = gid;
120 error (0, 0, _("cannot find name for group ID %ju"), g);
121 }
122 ok = false;
123 }
124 }
125
126 char *s = grp ? grp->gr_name : gidtostr (gid);
127 fputs (s, stdout);
128 return ok;
129 }
130