]> granicus.if.org Git - shadow/blob - src/groupmems.c
* src/groupmems.c (whoami): Make sure usr and grp are not NULL
[shadow] / src / groupmems.c
1 /*
2  * Copyright (c) 2000       , International Business Machines
3  *                            George Kraft IV, gk4@us.ibm.com, 03/23/2000
4  * Copyright (c) 2000 - 2006, Tomasz Kłoczko
5  * Copyright (c) 2007 - 2008, Nicolas François
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the copyright holders or contributors may not be used to
17  *    endorse or promote products derived from this software without
18  *    specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
24  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <config.h>
34
35 #include <fcntl.h>
36 #include <getopt.h>
37 #include <grp.h>
38 #include <stdio.h>
39 #include <sys/types.h>
40 #ifdef USE_PAM
41 #include "pam_defs.h"
42 #endif                          /* USE_PAM */
43 #include <pwd.h>
44 #include "defines.h"
45 #include "prototypes.h"
46 #include "groupio.h"
47
48 /* Exit Status Values */
49
50 #define EXIT_SUCCESS            0       /* success */
51 #define EXIT_USAGE              1       /* invalid command syntax */
52 #define EXIT_GROUP_FILE         2       /* group file access problems */
53 #define EXIT_NOT_ROOT           3       /* not superuser  */
54 #define EXIT_NOT_EROOT          4       /* not effective superuser  */
55 #define EXIT_NOT_PRIMARY        5       /* not primary owner of group  */
56 #define EXIT_NOT_MEMBER         6       /* member of group does not exist */
57 #define EXIT_MEMBER_EXISTS      7       /* member of group already exists */
58 #define EXIT_INVALID_USER       8       /* specified user does not exist */
59 #define EXIT_INVALID_GROUP      9       /* specified group does not exist */
60
61 /*
62  * Global variables
63  */
64 static char *adduser = NULL;
65 static char *deluser = NULL;
66 static char *thisgroup = NULL;
67 static bool purge = false;
68 static bool list = false;
69 static int exclusive = 0;
70 static char *Prog;
71 static bool group_locked = false;
72
73 static char *whoami (void);
74 static void members (char **members);
75 static void usage (void);
76 static void process_flags (int argc, char **argv);
77 static void check_perms (void);
78 static void fail_exit (int code);
79 #define isroot()                (getuid () == 0)
80
81 static char *whoami (void)
82 {
83         /* local, no need for xgetgrgid */
84         struct group *grp = getgrgid (getgid ());
85         /* local, no need for xgetpwuid */
86         struct passwd *usr = getpwuid (getuid ());
87
88         if (   (NULL != usr)
89             && (NULL != grp)
90             && (0 == strcmp (usr->pw_name, grp->gr_name))) {
91                 return xstrdup (usr->pw_name);
92         } else {
93                 return NULL;
94         }
95 }
96
97 static void members (char **members)
98 {
99         int i;
100
101         for (i = 0; NULL != members[i]; i++) {
102                 printf ("%s ", members[i]);
103
104                 if (NULL == members[i + 1]) {
105                         printf ("\n");
106                 } else {
107                         printf (" ");
108                 }
109         }
110 }
111
112 static void usage (void)
113 {
114         fputs (_("Usage: groupmems -a username | -d username | -D | -l [-g groupname]\n"), stderr);
115         fail_exit (EXIT_USAGE);
116 }
117
118 /*
119  * process_flags - perform command line argument setting
120  */
121 static void process_flags (int argc, char **argv)
122 {
123         int arg;
124         int option_index = 0;
125         static struct option long_options[] = {
126                 {"add", required_argument, NULL, 'a'},
127                 {"delete", required_argument, NULL, 'd'},
128                 {"group", required_argument, NULL, 'g'},
129                 {"list", no_argument, NULL, 'l'},
130                 {"purge", no_argument, NULL, 'p'},
131                 {NULL, 0, NULL, '\0'}
132         };
133
134         while ((arg = getopt_long (argc, argv, "a:d:g:lp", long_options,
135                                    &option_index)) != EOF) {
136                 switch (arg) {
137                 case 'a':
138                         adduser = xstrdup (optarg);
139                         ++exclusive;
140                         break;
141                 case 'd':
142                         deluser = xstrdup (optarg);
143                         ++exclusive;
144                         break;
145                 case 'p':
146                         purge = true;
147                         ++exclusive;
148                         break;
149                 case 'g':
150                         thisgroup = xstrdup (optarg);
151                         break;
152                 case 'l':
153                         list = true;
154                         ++exclusive;
155                         break;
156                 default:
157                         usage ();
158                 }
159         }
160
161         if ((exclusive > 1) || (optind < argc)) {
162                 usage ();
163         }
164
165         /* local, no need for xgetpwnam */
166         if (getpwnam (adduser) == NULL) {
167                 fprintf (stderr, _("%s: user `%s' does not exist\n"),
168                          Prog, adduser);
169                 fail_exit (EXIT_INVALID_USER);
170         }
171
172 }
173
174 static void check_perms (void)
175 {
176 #ifdef USE_PAM
177         pam_handle_t *pamh = NULL;
178         int retval = PAM_SUCCESS;
179         struct passwd *pampw;
180
181         pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
182         if (NULL == pampw) {
183                 retval = PAM_USER_UNKNOWN;
184         }
185
186         if (PAM_SUCCESS == retval) {
187                 retval = pam_start ("groupmod", pampw->pw_name,
188                                     &conv, &pamh);
189         }
190
191         if (PAM_SUCCESS == retval) {
192                 retval = pam_authenticate (pamh, 0);
193         }
194
195         if (PAM_SUCCESS == retval) {
196                 retval = pam_acct_mgmt (pamh, 0);
197         }
198
199         (void) pam_end (pamh, retval);
200         if (PAM_SUCCESS != retval) {
201                 fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
202                 fail_exit (1);
203         }
204 #endif
205 }
206
207 static void fail_exit (int code)
208 {
209         if (group_locked) {
210                 if (gr_unlock () == 0) {
211                         fprintf (stderr,
212                                  _("%s: unable to unlock group file\n"),
213                                  Prog);
214                 }
215         }
216         exit (code);
217 }
218
219 void main (int argc, char **argv) 
220 {
221         char *name;
222         struct group *grp;
223
224         /*
225          * Get my name so that I can use it to report errors.
226          */
227         Prog = Basename (argv[0]);
228
229         (void) setlocale (LC_ALL, "");
230         (void) bindtextdomain (PACKAGE, LOCALEDIR);
231         (void) textdomain (PACKAGE);
232
233         process_flags (argc, argv);
234
235         if (NULL == thisgroup) {
236                 name = whoami ();
237                 if (!list && (NULL == name)) {
238                         fprintf (stderr, _("%s: your groupname does not match your username\n"), Prog);
239                         fail_exit (EXIT_NOT_PRIMARY);
240                 }
241         } else {
242                 name = thisgroup;
243                 if (!list && !isroot ()) {
244                         fprintf (stderr, _("%s: only root can use the -g/--group option\n"), Prog);
245                         fail_exit (EXIT_NOT_ROOT);
246                 }
247         }
248
249         if (!list) {
250                 check_perms ();
251
252                 if (gr_lock () == 0) {
253                         fprintf (stderr,
254                                  _("%s: unable to lock group file\n"), Prog);
255                         fail_exit (EXIT_GROUP_FILE);
256                 }
257                 group_locked = true;
258         }
259
260         if (gr_open (list ? O_RDONLY : O_RDWR) == 0) {
261                 fprintf (stderr, _("%s: unable to open group file\n"), Prog);
262                 fail_exit (EXIT_GROUP_FILE);
263         }
264
265         grp = (struct group *) gr_locate (name);
266
267         if (NULL == grp) {
268                 fprintf (stderr, _("%s: `%s' not found in /etc/group\n"),
269                          Prog, name);
270                 fail_exit (EXIT_INVALID_GROUP);
271         }
272
273         if (list) {
274                 members (grp->gr_mem);
275         } else if (NULL != adduser) {
276                 if (is_on_list (grp->gr_mem, adduser)) {
277                         fprintf (stderr,
278                                  _("%s: user `%s' is already a member of `%s'\n"),
279                                  Prog, adduser, grp->gr_name);
280                         fail_exit (EXIT_MEMBER_EXISTS);
281                 }
282                 grp->gr_mem = add_list (grp->gr_mem, adduser);
283                 gr_update (grp);
284         } else if (NULL != deluser) {
285                 if (!is_on_list (grp->gr_mem, adduser)) {
286                         fprintf (stderr,
287                                  _("%s: user `%s' is not a member of `%s'\n"),
288                                  Prog, deluser, grp->gr_name);
289                         fail_exit (EXIT_NOT_MEMBER);
290                 }
291                 grp->gr_mem = del_list (grp->gr_mem, deluser);
292                 gr_update (grp);
293         } else if (purge) {
294                 grp->gr_mem[0] = NULL;
295                 gr_update (grp);
296         }
297
298         if (gr_close () == 0) {
299                 fprintf (stderr, _("%s: unable to close group file\n"), Prog);
300                 fail_exit (EXIT_GROUP_FILE);
301         }
302
303         fail_exit (EXIT_SUCCESS);
304 }
305