]> granicus.if.org Git - shadow/blob - src/groupmems.c
* src/groupmems.c: Use xstrdup() rather than strdup().
[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 (0 == strcmp (usr->pw_name, grp->gr_name)) {
89                 return xstrdup (usr->pw_name);
90         } else {
91                 return NULL;
92         }
93 }
94
95 static void members (char **members)
96 {
97         int i;
98
99         for (i = 0; NULL != members[i]; i++) {
100                 printf ("%s ", members[i]);
101
102                 if (NULL == members[i + 1]) {
103                         printf ("\n");
104                 } else {
105                         printf (" ");
106                 }
107         }
108 }
109
110 static void usage (void)
111 {
112         fputs (_("Usage: groupmems -a username | -d username | -D | -l [-g groupname]\n"), stderr);
113         fail_exit (EXIT_USAGE);
114 }
115
116 /*
117  * process_flags - perform command line argument setting
118  */
119 static void process_flags (int argc, char **argv)
120 {
121         int arg;
122         int option_index = 0;
123         static struct option long_options[] = {
124                 {"add", required_argument, NULL, 'a'},
125                 {"delete", required_argument, NULL, 'd'},
126                 {"group", required_argument, NULL, 'g'},
127                 {"list", no_argument, NULL, 'l'},
128                 {"purge", no_argument, NULL, 'p'},
129                 {NULL, 0, NULL, '\0'}
130         };
131
132         while ((arg = getopt_long (argc, argv, "a:d:g:lp", long_options,
133                                    &option_index)) != EOF) {
134                 switch (arg) {
135                 case 'a':
136                         adduser = xstrdup (optarg);
137                         ++exclusive;
138                         break;
139                 case 'd':
140                         deluser = xstrdup (optarg);
141                         ++exclusive;
142                         break;
143                 case 'p':
144                         purge = true;
145                         ++exclusive;
146                         break;
147                 case 'g':
148                         thisgroup = xstrdup (optarg);
149                         break;
150                 case 'l':
151                         list = true;
152                         ++exclusive;
153                         break;
154                 default:
155                         usage ();
156                 }
157         }
158
159         if ((exclusive > 1) || (optind < argc)) {
160                 usage ();
161         }
162
163         /* local, no need for xgetpwnam */
164         if (getpwnam (adduser) == NULL) {
165                 fprintf (stderr, _("%s: user `%s' does not exist\n"),
166                          Prog, adduser);
167                 fail_exit (EXIT_INVALID_USER);
168         }
169
170 }
171
172 static void check_perms (void)
173 {
174 #ifdef USE_PAM
175         pam_handle_t *pamh = NULL;
176         int retval = PAM_SUCCESS;
177         struct passwd *pampw;
178
179         pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
180         if (NULL == pampw) {
181                 retval = PAM_USER_UNKNOWN;
182         }
183
184         if (PAM_SUCCESS == retval) {
185                 retval = pam_start ("groupmod", pampw->pw_name,
186                                     &conv, &pamh);
187         }
188
189         if (PAM_SUCCESS == retval) {
190                 retval = pam_authenticate (pamh, 0);
191         }
192
193         if (PAM_SUCCESS == retval) {
194                 retval = pam_acct_mgmt (pamh, 0);
195         }
196
197         (void) pam_end (pamh, retval);
198         if (PAM_SUCCESS != retval) {
199                 fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
200                 fail_exit (1);
201         }
202 #endif
203 }
204
205 static void fail_exit (int code)
206 {
207         if (group_locked) {
208                 if (gr_unlock () == 0) {
209                         fprintf (stderr,
210                                  _("%s: unable to unlock group file\n"),
211                                  Prog);
212                 }
213         }
214         exit (code);
215 }
216
217 void main (int argc, char **argv) 
218 {
219         char *name;
220         struct group *grp;
221
222         /*
223          * Get my name so that I can use it to report errors.
224          */
225         Prog = Basename (argv[0]);
226
227         (void) setlocale (LC_ALL, "");
228         (void) bindtextdomain (PACKAGE, LOCALEDIR);
229         (void) textdomain (PACKAGE);
230
231         process_flags (argc, argv);
232
233         if (NULL == thisgroup) {
234                 name = whoami ();
235                 if (!list && (NULL == name)) {
236                         fprintf (stderr, _("%s: your groupname does not match your username\n"), Prog);
237                         fail_exit (EXIT_NOT_PRIMARY);
238                 }
239         } else {
240                 name = thisgroup;
241                 if (!list && !isroot ()) {
242                         fprintf (stderr, _("%s: only root can use the -g/--group option\n"), Prog);
243                         fail_exit (EXIT_NOT_ROOT);
244                 }
245         }
246
247         if (!list) {
248                 check_perms ();
249
250                 if (gr_lock () == 0) {
251                         fprintf (stderr,
252                                  _("%s: unable to lock group file\n"), Prog);
253                         fail_exit (EXIT_GROUP_FILE);
254                 }
255                 group_locked = true;
256         }
257
258         if (gr_open (list ? O_RDONLY : O_RDWR) == 0) {
259                 fprintf (stderr, _("%s: unable to open group file\n"), Prog);
260                 fail_exit (EXIT_GROUP_FILE);
261         }
262
263         grp = (struct group *) gr_locate (name);
264
265         if (NULL == grp) {
266                 fprintf (stderr, _("%s: `%s' not found in /etc/group\n"),
267                          Prog, name);
268                 fail_exit (EXIT_INVALID_GROUP);
269         }
270
271         if (list) {
272                 members (grp->gr_mem);
273         } else if (NULL != adduser) {
274                 if (is_on_list (grp->gr_mem, adduser)) {
275                         fprintf (stderr,
276                                  _("%s: user `%s' is already a member of `%s'\n"),
277                                  Prog, adduser, grp->gr_name);
278                         fail_exit (EXIT_MEMBER_EXISTS);
279                 }
280                 grp->gr_mem = add_list (grp->gr_mem, adduser);
281                 gr_update (grp);
282         } else if (NULL != deluser) {
283                 if (!is_on_list (grp->gr_mem, adduser)) {
284                         fprintf (stderr,
285                                  _("%s: user `%s' is not a member of `%s'\n"),
286                                  Prog, deluser, grp->gr_name);
287                         fail_exit (EXIT_NOT_MEMBER);
288                 }
289                 grp->gr_mem = del_list (grp->gr_mem, deluser);
290                 gr_update (grp);
291         } else if (purge) {
292                 grp->gr_mem[0] = NULL;
293                 gr_update (grp);
294         }
295
296         if (gr_close () == 0) {
297                 fprintf (stderr, _("%s: unable to close group file\n"), Prog);
298                 fail_exit (EXIT_GROUP_FILE);
299         }
300
301         fail_exit (EXIT_SUCCESS);
302 }
303